Search code examples
pythonpython-telegram-bot

AttributeError: 'Updater' object has no attribute 'dispatcher'


When I run this code:

from telegram.ext import *
import keys
    
print('Starting a bot....')
     
def start_commmand(update, context):
    update.message.reply_text('Hello! Welcome To Store!')

if __name__ == '__main__':
    updater = Updater(keys.token, True)
    dp = updater.dispatcher

    # Commands
    dp.add.handler(CommandHandler('start', start_commmand))

    # Run bot
    updater.start_polling(1.0)
    updater.idle()

I get this error:

Traceback (most recent call last):
  File "C:\Users\pc\PycharmProjects\telegram\main.py", line 11, in <module>
    dp = updater.dispatcher
AttributeError: 'Updater' object has no attribute 'dispatcher'

I attempted to resolve this issue by updating the library but the error remained.


Solution

  • You probably found an example for v13, but since a few days the v20 for python-telegram-bot is out. Now you have to build your application differently and you have to use async functions.

    This should work:

    from telegram.ext import *
    import keys
        
    print('Starting a bot....')
         
    async def start_commmand(update, context):
        await update.message.reply_text('Hello! Welcome To Store!')
    
    if __name__ == '__main__':
        application = Application.builder().token(keys.token).build()
    
        # Commands
        application.add_handler(CommandHandler('start', start_commmand))
    
        # Run bot
        application.run_polling(1.0)
    

    Also, here are some good examples for the python-telegram-bot library.