[Fixed] TypeError: Updater.__init__() got an unexpected keyword argument 'token' – Python

by
Ali Hasan
bots python pythontelegrambot

Quick Fix: When initializing the Updater, use bot instead of token. The correct way is updater = Updater(bot=bot, use_context=True). Also, remove the token argument from the Updater constructor call.

The Problem:

While using the telegram python library, I’m trying to initialize an Updater object with a bot token. However, I’m encountering a ‘TypeError: Updater.init() got an unexpected keyword argument ‘token”. How can I resolve this error and successfully use the token to set up the bot?

The Solutions:

Solution 1: Creating a Bot Instance

In the provided code, the error TypeError: Updater.__init__() got an unexpected keyword argument 'token' occurs because the Updater class is initialized with an unexpected keyword argument token. To resolve this error, you need to create a Bot instance and pass it to the Updater.

Here’s the solution:

  1. Import the Bot class from the telegram module.

    from telegram import Update, Bot
    
  2. Create a Bot instance using your bot token.

    bot = Bot(token=TOKEN)
    
  3. Initialize the Updater class with the created bot instance instead of providing the token directly.

    updater = Updater(bot=bot, use_context=True)
    
  4. The rest of the code remains the same.

With these changes, your code should run without the TypeError.

Solution 2: Use the `Bot` class and an `asyncio.Queue` instead

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).

Solution 3: Upgrade python-telegram-bot to the latest version.

The error message “TypeError: Updater.__init__() got an unexpected keyword argument ‘token'” indicates that you are using an outdated version of the python-telegram-bot library. In versions 20.0 and later, the Updater class no longer supports the ‘token’ and ‘use_context’ arguments. To resolve this issue, you should upgrade your python-telegram-bot library to the latest version using the following steps:

  1. Uninstall the current version of python-telegram-bot using the pip command:

    pip uninstall python-telegram-bot

  2. Install the latest version of python-telegram-bot using the pip command:

    pip install python-telegram-bot

  3. Once the installation is complete, you can run your code again, and the error should be resolved.

After upgrading to the latest version of python-telegram-bot, you will need to make some changes to your code to adapt it to the new API. The “token” and “use_context” arguments are no longer supported, so you will need to remove them from your code. Additionally, you will need to import the new Context class and use it in your handlers and dispatcher.

Q&A

What is causing this error?

You no longer need to specify token while creating Updater instance.

How to fix it?

You can downgrade python-telegram-bot to version 13.15 to eliminate this error.

Is there another fix besides downgrading python-telegram-bot version?

Yes, you can use Bot class first and then pass it to Updater.

Video Explanation:

The following video, titled "”How", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

This video provides further insights and detailed explanations related to the content discussed in the article.

– TypeError: _init_() got an …” description=”If you are facing error ‘TypeError: __init__() got an unexpected keyword argument ‘documents’ then this video is for you 🙂 Blog: …”]