I’m encountering an unusual error while trying to set up a Telegram bot in Python. The error indicates that the Updater class does not accept the ‘token’ argument, which is surprising since I’ve come across this in numerous tutorials.
from telegram.ext import Updater, CommandHandler
def start_command(update, context):
update.message.reply_text('Hello! The bot is active.')
def run_bot():
# Create an Updater for the bot with the given token
bot_updater = Updater(token='my_bot_token', use_context=True)
# Retrieve the dispatcher
dp = bot_updater.dispatcher
# Register command handlers
dp.add_handler(CommandHandler('start', start_command))
# Begin polling
bot_updater.start_polling()
bot_updater.idle()
if __name__ == '__main__':
run_bot()
The error happens during the Updater creation, stating Updater.__init__() got an unexpected keyword argument 'token'. I’m puzzled because this worked without issues previously. Has the library been updated? What is the correct way to initialize the Updater now?
yeah, the new updates for python-telegram-bot v20+ changed a lot. they removed Updater and now it’s Application u gotta use. change your code to from telegram.ext import Application and then use Application.builder().token('your_token').build() - had the same problem last month.
This happens frequently with the updates in the python-telegram-bot library. The code provided is based on the older API from version 13.x and earlier. If you are using version 20 or higher, the library has undergone significant changes. You will need to replace Updater with Application and modify your code accordingly. Alternatively, if you prefer to maintain your current implementation, you can downgrade to version 13.15 using pip install python-telegram-bot==13.15, which will restore the Updater class. I’ve experienced this problem as well while updating my bot projects, so it’s a matter of whether you want to adapt to the new API or stick with the familiar tutorials.
Hit this same problem a few weeks back on a project I’d left alone for months. You’re dealing with version compatibility - your code’s written for the old v13.x API but you’ve probably got v20+ installed. What threw me was how much the initialization changed between versions. Skip the juggling between old and new syntax. Just check what version you actually have first with pip show python-telegram-bot. If you’re on v20+, migration’s not too bad once you get it, but you’ll need to update your handler registration and polling methods too since those also changed.