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