-1

I am creating a telegram bot and when I run the code to try it, it shows this error: Exception has occurred: TypeError Updater.init() got an unexpected keyword argument 'use_context'

import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, filters

def handle_message(update, context):
    message = update.message.text
    
    # Implement your logic based on the message content
    if message == '/help':
        context.bot.send_message(chat_id=update.effective_chat.id, text="This is the help message.")
    elif message.startswith('/sayhello'):
        name = message.split()[1]  # Extract the name from the command
        context.bot.send_message(chat_id=update.effective_chat.id, text=f"Hello, {name}!")
    else:
        context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, I didn't understand that command.")


def start_command(update, context):
    # Send a welcome message
    context.bot.send_message(chat_id=update.effective_chat.id, text="Hello! I'm your Telegram bot.")

bot = telegram.Bot(token='blabla')
updater = Updater(bot= bot, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start_command))
dispatcher.add_handler(MessageHandler(filters.text, handle_message))

updater.start_polling()



I am using version 20.3 of python-telegram-bot library. I tried downgading to 20.2 and mamyother versions, nothing worked.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Does this answer your question? [TypeError: Updater.\_\_init\_\_() got an unexpected keyword argument 'use\_context'](https://stackoverflow.com/questions/72899473/typeerror-updater-init-got-an-unexpected-keyword-argument-use-context) – Giuseppe La Gualano Jun 18 '23 at 16:03

1 Answers1

0

The error message suggests that you are using an outdated version of the python-telegram-bot library. The use_context parameter was introduced in version 13.0 of the library, and it seems that your code is trying to use it with an older version.

To resolve this issue, you have a couple of options:

Upgrade the python-telegram-bot library: Update your python-telegram-bot library to version 13.0 or above, which supports the use_context parameter. You can use the following command to upgrade the library:

pip install --upgrade python-telegram-bot

After upgrading, the use_context parameter should be recognized by the Updater class.

Modify your code to remove the use_context parameter: If you prefer not to upgrade the library, you can modify your code to remove the use_context parameter. In earlier versions of the library, the use_context parameter was not present, so you can simply remove it from the Updater.init() function call. Your updated code might look like this:

from telegram.ext import Updater, CommandHandler

def start_command(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id, text="Hello, I'm your bot!")

updater = Updater('blabla')
dispatcher = updater.dispatcher
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)

updater.start_polling()

This modified code should work with older versions of the python-telegram-bot library.

Choose the option that best suits your needs, and make sure to adjust your code accordingly.

Inferno
  • 11
  • 4