Search code examples
pythontelegramtelegram-botpython-telegram-bot

Unresolved attribute reference 'dispatcher' for class 'Updater'


I try to create a Telegram bot but I can't use property Update class.

import logging
from multiprocessing import Queue

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler

def main():
    update_queue = Queue()
    updater = Updater("API KEY", use_context=True, update_queue=update_queue)

    updater.dispatcher.add_handler(CommandHandler('start', start))
    updater.dispatcher.add_handler(CallbackQueryHandler(button))

    updater.start_polling()
    updater.idle()


if __name__ == '__main__':
    main()

Idle say that there is no dispatcher in Update class. Try update Telegram api, didn't help. Can't find another way to update bot


Solution

  • Since version 20.0 the Updater is only used to fetch updates, from the docs:

    Changed in version 20.0:

    • Removed argument and attribute user_sig_handler

    • The only arguments and attributes are now bot and update_queue as now the sole purpose of this class is to fetch updates. The entry point to a PTB application is now telegram.ext.Application.


    So if you want to add a handler, use an Application.

    An example, taken from the echo bot example:

    async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
        """Echo the user message."""
        await update.message.reply_text(update.message.text)
    
    
    def main() -> None:
        """Start the bot."""
        # Create the Application and pass it your bot's token.
        application = Application.builder().token("TOKEN").build()
    
        # on different commands - answer in Telegram
        application.add_handler(CommandHandler("start", start))
        application.add_handler(CommandHandler("help", help_command))
    
        # on non command i.e message - echo the message on Telegram
        application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
    
        # Run the bot until the user presses Ctrl-C
        application.run_polling()
    
    
    if __name__ == "__main__":
        main()