Search code examples
pythontelegram-botpython-telegram-bot

basic telegram bot example


I am recreating the basic telegram bot example from here, but I am having a slight problem.

import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',level=logging.INFO)
logger = logging.getLogger(__name__)
# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
    """Send a message when the command /start is issued."""
    update.message.reply_text('Hi!')

def help(update, context):
    """Send a message when the command /help is issued."""
    update.message.reply_text('Help!')

def echo(update, context):
    """Echo the user message."""
    update.message.reply_text(update.message.text)

def error(update, context):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, context.error)

def main():
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    # Make sure to set use_context=True to use the new context based callbacks
    # Post version 12 this will no longer be necessary
    updater = Updater("YOUR TOKEN HERE",use_context=True)
    # Get the dispatcher to register handlers
    dp = updater.dispatcher
    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("help", help))
    # on noncommand i.e message - echo the message on Telegram
    dp.add_handler(MessageHandler(Filters.text, echo))
    # log all errors
    dp.add_error_handler(error)
    # Start the Bot
    updater.start_polling()
    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()
if __name__ == '__main__':
    main()

I am getting below error after executing the script:

$ python test1.py

Traceback (most recent call last):
  File "test1.py", line 64, in <module>
    main()
  File "test1.py", line 39, in main
    updater = Updater(TOKEN,use_context=True)
TypeError: __init__() got an unexpected keyword argument 'use_context'

Solution

  • The error is telling you that use_context is not a valid keyword argument for the Updater initializer. That argument is no longer supported in the 12th version of Python Telegram Bot.

    I guess you did pip install python-telegram-bot==12.0.0b1 --upgrade to install the library and it's installing the version 12.0.0b1.

    You can do again the installation by running the following commnads:

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

    2. Install the 11 version of the library: pip install python-telegram-bot==11

    Also, if you don't want to change your library version and keep using python-telegram-bot 12.0.0, you can just remove the use_context argument from the instantiation of the Updater class.

    This line:

    updater = Updater("YOUR TOKEN HERE",use_context=True)
    

    Would become:

    updater = Updater("YOUR TOKEN HERE")