Unable to proceed with the telegram bot with error use_context

Hi Pros, i am a newby to create a bot in telegram.
then i got this error,
updater = Updater(BOT_TOKEN, use_context=True)
TypeError: init() got an unexpected keyword argument ‘use_context’

May i know how to solve this error?
here is my code. thanks.

# Install the library: pip install forex-python
import forex_python, telegram.ext, asyncio
from forex_python.converter import CurrencyRates
from telegram.ext import Updater, CommandHandler, MessageHandler, filters

# Replace with your bot's API token
BOT_TOKEN = "xxxxxxxxxxxxx"


async def convert_and_compare(update, context):
  # Extract user input (assuming message starts with amount and currency)
  try:
    message_text = update.message.text.strip()
    amount, currency = message_text.split(" ", 1)
    amount = float(amount)
  except (ValueError, IndexError):
    await update.message.reply_text("Invalid input. Please enter a valid amount and currency (e.g., 100 USD).")
    return

  # Check if currency is USD
  if currency.upper() != "USD":
    await update.message.reply_text("Please enter USD as the base currency (e.g., 100 USD).")
    return

  # Conversion logic
  target_currencies = ["MYR","SGD","GBP", "EUR"]
  converted_amounts = {}
  converter = CurrencyRates()
  for target_currency in target_currencies:
    converted_amounts[target_currency] = await converter.convert("USD", target_currency, amount)

  # Prepare message string
  message = "**Conversion Results:**\n"
  for currency, converted_amount in converted_amounts.items():
    message += f"{amount} USD is equivalent to {converted_amount:.2f} {currency}\n"

  message += "\n**MYR Comparison:**\n"
  message += "Currency | Amount (USD) | Equivalent in MYR\n"
  message += "---------|--------------|------------------\n"
  for currency, converted_amount in converted_amounts.items():
    if currency != "MYR":
      myr_equivalent = amount * await converter.convert(currency, "MYR", 1)
      message += f"{currency} | {converted_amount:.2f} | {myr_equivalent:.2f} MYR\n"
  message += f"MYR       | -             | {converted_amounts['MYR']:.2f} MYR\n"

  # Send message via Telegram bot
  await update.message.reply_text(message)


def main():
  updater = Updater(BOT_TOKEN, use_context=True)
  dispatcher = updater.dispatcher

  # Handle user messages
  dispatcher.add_handler(MessageHandler(filters.text, convert_and_compare))

  # Start the Updater (asynchronous)
  updater.start_polling()
  updater.idle()


if __name__ == "__main__":
  asyncio.run(main())

Did you replace this constant with your own unique token?

For the following line:

updater = Updater(BOT_TOKEN, use_context=True)

you can just enter it as:

updater = Updater(BOT_TOKEN)

since the default value is not being changed to a new value.

Apparently this is a popular project. Have you attempted following the link provided here
(from a thread from a few days ago):

yes i did replace the bot token to my own token, it shown another error…

  File "/Users/zx/Downloads/convert_tele.py", line 51, in main
    updater = Updater(BOT_TOKEN)
TypeError: __init__() missing 1 required positional argument: 'update_queue'

Can you provide line 51 from your code?

and my python telegram bot ver as below: -

Name: python-telegram-bot
Version: 21.1

Interesting. Ok, re-enter it as you had it before:

updater = Updater(BOT_TOKEN, use_context=True) # Default is use_context=True

Have you reviewed the documentation here:

https://docs.python-telegram-bot.org/en/v21.1/examples.contexttypesbot.html

Can you try again and provide a snapshot of the entire error?

here is the snapshot.

Ok, apparently, for the second argument, a queue needs to be entered.

From the bot documentation found here:
https://docs.python-telegram-bot.org/en/v21.1/telegram.ext.updater.html#telegram.ext.Updater.update_queue

A thread on this topic with a potential solution is found here:

From this thread, they suggest to update the code as:

from asyncio import queue

my_queue = queue.Queue()

# ...

def main():
    updater = Updater(bot=bot, update_queue=my_queue)

but this update queue is for bot 13.13 right?
i had tried with the queue, but still having another updater error:

Did you update your code to have:

from asyncio import queue  # Did you import this?

my_queue = queue.Queue() # Did you define the queue?

# ...

def main():
    updater = Updater(bot=bot, update_queue=my_queue)

What do you mean by this:

yeah i had define,

i mean the queue still valid for telegram bot v21?

From the bot website:

bot_version

For now, continue reading the documentation as provided in the previous posts.

Here is a related video on Youtube to get you started:

1 Like