Hey guy’s am having a problem with python-telegram-bot, what am doing is a bot that you start by pasting url for any file you want or Youtube videos, but am having a problem where I have two message handlers with the filter filters.TEXT
, the first handler is for files like photos or documents to be downloaded, and the second one is for Youtube links to be downloaded with the pytube lib.
When having the first handler on the top of the other it works first as expected, but when the condition is not met it does not go to the next message handler, only stays in the first and nothing happens.
This is my my code:
import requests
from telegram import Update
from telegram.ext import CommandHandler, ContextTypes, Application, filters, MessageHandler
from io import BytesIO
from pytube import YouTube
class FileHandler:
def __init__(self):
self.text = None
async def download_upload(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
self.text = update.message.text
# print(f"User: {self.text}")
if "youtu.be" not in self.text:
file_buffer = BytesIO()
request = requests.get(self.text)
print(f"Status-code: {request.status_code}")
file_size = int(request.headers.get('Content-Length'), 0)
if request.status_code == 200:
print("Status-download: file good to download")
await update.message.reply_text("Please wait, if file is above 1MB it might take a bit because of some "
"network issues.")
file_buffer.write(request.content)
file_buffer.seek(0)
if self.photo_or_doc(self.text) == "photo":
await context.bot.send_photo(chat_id=update.message.chat_id, photo=file_buffer,
caption="This is your Photo"
" :)")
else:
await context.bot.send_document(chat_id=update.message.chat_id, document=file_buffer,
caption="This is your Document"
" :)")
else:
print(f"Error {request.status_code}")
def photo_or_doc(self, url):
response = requests.head(url)
file_type = response.headers.get("Content-Type", "")
if file_type.startswith("image/"):
return "photo"
else:
return "document"
class Youtube:
def __init__(self):
self.url = str
self.yt = None
async def youtube_video_download(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
self.url = update.message.text
print(f"User: {self.url}")
self.yt = YouTube(self.url).streams.get_lowest_resolution()
chat_id = update.message.chat_id
if "youtu.be" in self.url:
file_buffer = BytesIO()
file_buffer.write(self.yt.download())
file_buffer.seek(0)
await context.bot.send_video(chat_id=chat_id, video=file_buffer, caption="This is your youtube video :)")
class MainProgram:
def __init__(self):
self.Token = 'Bot-Token'
self.app = Application.builder().token(self.Token).build()
self.file = FileHandler()
self.Youtube = Youtube()
async def start_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("welcome to DownMasterBot,n"
"this bot is used to download files from photos, to documents,n"
"for more information about how to use the bot please use this /help")
async def help_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("This is the help menu:n"
"Commands:n"
"/start is used when first using the bot and it gives you an introduction to"
" what the bot do.n"
"/help give more details to how to use the bot, with listing all the functions "
"of the bot.n"
"--------------------------------------------------------------------------nn"
"Services:"
"Download any file you want from the internet using telegram for easy download"
" and storing of files. n"
"--------------------------------------------------------------------------nn"
"How to download:n"
"just by pasting the link/url of the file(not a redirect) it will be downloaded"
"using the telegram download manager.")
async def handle_errors(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
print(f"Update {update} caused error {context.error}")
def start(self):
print('Starting bot...')
# Command handlers
self.app.add_handler(CommandHandler("start", self.start_command))
self.app.add_handler(CommandHandler("help", self.help_command))
# Message handlers
self.app.add_handler(MessageHandler(filters.TEXT, self.file.download_upload))
self.app.add_handler(MessageHandler(filters.TEXT, self.Youtube.youtube_video_download))
# Errors
self.app.add_error_handler(self.handle_errors)
print('Polling...')
self.app.run_polling(poll_interval=5)
if __name__ == '__main__':
main = MainProgram()
main.start()
I read that there are handler callbacks or something like that, but I don’t know what they do, or what they even are.
I tried with if else statements, but couldn’t get from one handler to the other if the condition is not met.
MONSTER EMAD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.