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
The Updater class takes two arguments,
bot
andupdate_queue
. You’ll need to create a telegram.Bot instance and an asyncio.Queue instance first:I’m not at all familiar with
Telegram
, and my understanding ofasyncio
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).Starting from python-telegram-bot 20.0, the
Updater
class no longer supports the argumentstoken
anduse_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:and then installing the version 13.15 with: