skip to Main Content

I have this code which is suppose to look for /help command in telegram. So once you type /help in the telegram channel it will give you options. The code is as follows.

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

# Define your bot token here
TOKEN = "YOUR_BOT_TOKEN"


def start(update, context):
    update.message.reply_text("Welcome to your Telegram bot!")

def help_command(update, context):
    update.message.reply_text("You requested help. Here are some available commands:n"
                              "/help - Show this help messagen"
                              "/start - Start the bot")

def handle_message(update, context):
    text = update.message.text
    if text == '/start':
        start(update, context)
    elif text == '/help':
        help_command(update, context)

def main():
    # Initialize the Updater with your bot token
    updater = Updater(token=TOKEN, use_context=True)
    dispatcher = updater.dispatcher

    # Define the command handlers
    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(CommandHandler("help", help_command))

    # Handle non-command messages using a filter
    dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message))

    # Start the bot
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

However I am getting this error

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

Could you please advise how I can resolve this error.

2

Answers


  1. The Updater class takes two arguments, bot and update_queue. You’ll need to create a telegram.Bot instance and an asyncio.Queue instance first:

    from asyncio import Queue
    from telegram import Bot
    from telegram.ext import Updater
    
    bot = Bot(TOKEN, ...)
    update_queue = Queue()
    
    updater = Updater(bot, update_queue)
    

    I’m not at all familiar with Telegram, and my understanding of asyncio is limited, so that is the most help I can offer. If you aren’t sure how to use the queue, I would suggest looking at the documentation links for each of the packages (listed above).

    Login or Signup to reply.
  2. Starting from python-telegram-bot 20.0, the Updater class no longer supports the arguments token and use_context. The last version that supports these arguments is python-telegram-bot 13.15, so if you just want to use someone else’s code written for pre-20.0 versions without refactoring, you can downgrade your python-telegram-bot version by first uninstalling it with:

    pip uninstall python-telegram-bot
    

    and then installing the version 13.15 with:

    pip install python-telegram-bot==13.15
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search