1
votes

I created a Python Telegram bot and registered two command handlers. The issue is that the hello command handler is not working.

I tried changing the group of that handler to group=2, still hello does not get invoked when I use /rtd. Unable to figure out the issue.

def mybot():
  print("dispatcher created.")
  updater = Updater(token=my_token)
  dispatcher = updater.dispatcher

  dispatcher.add_error_handler(error_callback)
  dispatcher.add_handler(CommandHandler('start', start))
  dispatcher.add_handler(CommandHandler('rtd', hello, pass_args=True))
  dispatcher.add_handler(MessageHandler(Filters.command, unknown))

  print("handlers added.")
  updater.start_polling()
  updater.idle()
  pass

def hello(bot, update, cmd):
    print("hello handler", cmd)
    pass

def start(bot, update):
  bot.sendMessage(chat_id=update.message.chat_id, text="bot start!")
  pass

def unknown(bot, update):
    bot.send_message(chat_id=update.message.chat_id, text="unknown stuff.")
    pass

def error_callback(bot, update, error):
    try:
        raise error
    except TelegramError:
        print("Telegram Error")

if __name__ == '__main__':
    print("bot started.")
    mybot()
1
Two considerations. 1. hello function use print instead of bot.send_message, where you are expecting the output? 2. I see some indent issue in your code. That's right? - Stefano Giraldi

1 Answers

1
votes

The problem here is that you named the parameters of the hello function prototype wrong. The name of the parameter that passes the command arguments must be args. You named it cmd instead.

See the documentation in this regard: Screenshot of pass_args parameter documentation

Source: https://ptb.readthedocs.io/en/latest/telegram.ext.commandhandler.html

This behaviour is the same for all pass_* handler arguments.