Search code examples
pythonbotstelegram

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


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 message\n"
                              "/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.


Solution

  • I used telegram.Bot class and then I passed it to the Updater like this:

    from telegram import Update, Bot
    from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext
    from telegram.ext import filters
    
    #defining your bot token here
    TOKEN = "YOUR_BOT_TOKEN"
    
    def start(update: Update, context: CallbackContext):
        update.message.reply_text("Welcome to your Telegram bot!")
    
    def help_command(update: Update, context: CallbackContext):
        update.message.reply_text("You requested help. Here are some available commands:\n"
                                  "/help - Show this help message\n"
                                  "/start - Start the bot")
    
    def handle_message(update: Update, context: CallbackContext):
        text = update.message.text
        if text == '/start':
            start(update, context)
        elif text == '/help':
            help_command(update, context)
    
    def main():
        #creating a bot instance
        bot = Bot(token=TOKEN)
    
        #creating an updater instance and pass the bot instance
        updater = Updater(bot=bot, use_context=True)
        dispatcher = updater.dispatcher
    
        #defining the command handlers
        dispatcher.add_handler(CommandHandler("start", start))
        dispatcher.add_handler(CommandHandler("help", help_command))
    
        #handling non-command messages using a filter
        dispatcher.add_handler(MessageHandler(filters.Text & ~filters.Command, handle_message))
    
        #starting the bot
        updater.start_polling()
        updater.idle()
    
    if __name__ == '__main__':
        main()