TypeError with Updater initialization in python-telegram-bot library version 20.0

I’m having trouble with my Discord bot code after upgrading to python-telegram-bot v20. When I try to run my bot, I get a TypeError saying that the Updater constructor is missing a required argument called ‘update_queue’.

Here’s the error message I’m getting:

Traceback (most recent call last):
  File "/home/user/projects/chatbot/bot_main.py", line 98, in <module>
    initialize_bot()
  File "/home/user/projects/chatbot/bot_main.py", line 89, in initialize_bot
    bot_updater = Updater("BOT-TOKEN-HERE")
TypeError: __init__() missing 1 required positional argument: 'update_queue'

My current code looks like this:

def initialize_bot():
    # Setup database connection
    database = sqlite3.connect('user_data.db')
    cursor = database.cursor()
    cursor.execute("""CREATE TABLE IF NOT EXISTS user_messages (
                        user_id INTEGER,
                        message TEXT,
                        timestamp TIMESTAMP
                    )""")
    cursor.execute("""CREATE TABLE IF NOT EXISTS blocked_users (
                        user_id INTEGER
                    )""")
    database.commit()
    database.close()

    # Define admin user IDs
    admin_list = [123456789, 987654321]

    # Initialize bot updater and handlers
    bot_updater = Updater("MY_TOKEN", use_context=True)
    dispatcher = bot_updater.dispatcher
    dispatcher.add_handler(CommandHandler("start", welcome_user))
    dispatcher.add_handler(CommandHandler("save", save_message))
    dispatcher.add_handler(CommandHandler("block", block_user))
    bot_updater.start_polling()
    bot_updater.idle()

What’s the correct way to fix this issue with the new version?

Yes, this issue arises when upgrading from older versions of the python-telegram-bot. The Updater class was removed in v20, and you need to use the Application class. Replace your Updater instantiation with Application.builder().token(“YOUR_TOKEN”).build(). Change any references from dispatcher to application, and call add_handler directly on the application object. Instead of start_polling() and idle(), use application.run_polling(). Here is the modified code:

from telegram.ext import Application, CommandHandler

def initialize_bot():
    # Your database setup remains unchanged
    
    application = Application.builder().token("MY_TOKEN").build()
    application.add_handler(CommandHandler("start", welcome_user))
    application.add_handler(CommandHandler("save", save_message))
    application.add_handler(CommandHandler("block", block_user))
    application.run_polling()

Additionally, ensure your handler functions utilize async/await syntax, as required in v20.

Had the exact same problem migrating from v13 to v20. Total pain because it’s not just swapping Updater for Application - you’ve got to change how handlers work too. In v20, context becomes the second parameter, so your functions need to go from def welcome_user(update, context) to async def welcome_user(update: Update, context: ContextTypes.DEFAULT_TYPE). Make sure you import Update and ContextTypes from telegram and telegram.ext. Also heads up - lots of methods that used to be sync now need await, so you’ll be adding await before stuff like context.bot.send_message(). The official migration guide on their GitHub breaks down all the breaking changes pretty well.

yeah, they completely removed the updater class in v20 - that’s why you’re getting the error. you’ll need to rewrite your handlers as async functions since that’s required now. also, make sure to import Application from telegram.ext instead of updater.