I’m trying to make a basic Telegram chatbot but I’m running into a problem. Here’s the code I’m using:
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
def greet(update, context):
update.message.reply_text('Hello there!')
def assist(update, context):
update.message.reply_text('How can I help?')
def repeat(update, context):
update.message.reply_text(update.message.text)
def log_error(update, context):
logger.warning('Update "%s" caused error "%s"', update, context.error)
def run_bot():
updater = Updater('BOT_TOKEN_GOES_HERE', use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler('greet', greet))
dp.add_handler(CommandHandler('assist', assist))
dp.add_handler(MessageHandler(Filters.text, repeat))
dp.add_error_handler(log_error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
run_bot()
But when I run it, I get this error:
TypeError: __init__() got an unexpected keyword argument 'use_context'
What’s causing this and how can I fix it? I’m new to Telegram bots and could use some help.
I encountered a similar issue when I first started working with Telegram bots. The problem is likely related to the version of the python-telegram-bot library you’re using. In my experience, newer versions (13.0 and above) require a different approach.
Try modifying your code to use the Application builder pattern instead:
from telegram.ext import Application, CommandHandler, MessageHandler, filters
async def greet(update, context):
await update.message.reply_text('Hello there!')
# Update other handler functions similarly
if __name__ == '__main__':
application = Application.builder().token('YOUR_BOT_TOKEN').build()
application.add_handler(CommandHandler('greet', greet))
application.add_handler(CommandHandler('assist', assist))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, repeat))
application.run_polling()
This approach should work with the latest library version and resolve your issue. Remember to update your other functions to be asynchronous as well.
I’ve worked on several Telegram bots, and I can shed some light on your issue. The error you’re encountering is typical when using an outdated version of the python-telegram-bot library. To resolve this, you should first try updating the library to the latest version.
If you’re still facing issues after the update, consider switching to the more recent Application API. It’s more robust and offers better error handling. Here’s a quick example of how you could refactor your code:
from telegram.ext import Application, CommandHandler, MessageHandler, filters
async def greet(update, context):
await update.message.reply_text('Hello there!')
# Update other handlers similarly
def main():
application = Application.builder().token('YOUR_BOT_TOKEN').build()
application.add_handler(CommandHandler('greet', greet))
# Add other handlers
application.run_polling()
if __name__ == '__main__':
main()
This approach should resolve your issues and get your bot up and running smoothly.
hey, looks like ur using an older version of python-telegram-bot. try upgrading it with ‘pip install python-telegram-bot --upgrade’. that should fix the ‘use_context’ error. if not, try removing that parameter from the Updater. lmk if u need more help!