Experiencing issues launching Telegram bot in Python

Hey everyone! I’m having trouble getting my Telegram bot up and running. I’m using Python 3.12.0 and python-telegram-bot 21.3 in VSCode. When I try to start the bot, I get this error:

TypeError: Updater.__init__() got an unexpected keyword argument 'use_context'

Here’s a snippet of my code:

from telegram.ext import Updater, CommandHandler, MessageHandler

def greet(update, context):
    update.message.reply_text('Hi there! Use /change to modify settings.')

def change_settings(update, context):
    # Some code here

def main():
    updater = Updater("MY_TOKEN", use_context=True)
    dp = updater.dispatcher

    dp.add_handler(CommandHandler("greet", greet))
    dp.add_handler(CommandHandler("change", change_settings))

    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

I’ve tried looking for solutions online but no luck so far. Any ideas what might be causing this? Thanks in advance for your help!

I encountered a similar issue when upgrading to python-telegram-bot 21.x. The error you’re seeing is because the Updater class has been deprecated in newer versions. To resolve this, you’ll need to update your code to use the Application class instead. Here’s a quick modification that should work:

from telegram.ext import Application, CommandHandler

async def greet(update, context):
    await update.message.reply_text('Hi there! Use /change to modify settings.')

async def change_settings(update, context):
    # Some code here


def main():
    application = Application.builder().token("MY_TOKEN").build()
    
    application.add_handler(CommandHandler("greet", greet))
    application.add_handler(CommandHandler("change", change_settings))

    application.run_polling()

if __name__ == '__main__':
    main()

Make sure to use async functions for your handlers. This should resolve the TypeError you’re experiencing.

yo man, i had the same issue. the new version changed things up. try using Application instead of Updater. it worked for me. also, make ur functions async. like this:

async def greet(update, context):
await update.message.reply_text(‘Hi!’)

good luck bro!