Muliple message handlers not working, python-telegram-bot

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.

New contributor

MONSTER EMAD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật