How do I have my Bot respond with arguments?
I've reached out to @BotSupport with the same question, and he/they/it responded swiftly with the following answer:
Hi, at the moment it is not possible to highlight parameters of a command. I any case, you may can find a workaround if you use correct custom keyboards ;) — @BotSupport
Custom keyboards may be an option for someone, but not for me. The solution I've gone for is to give the command as /info123
. As the bot receives all /
commands, I check if the received command starts with info
, and if so, I remove the info
part. I convert the remaining string/int to arguments, and pass that along to the relevant command.
If you mean to pass the 123
as an argument for your command info
and if you happen to use the python-telegram-bot, then here's how you do it:
dispatcher.add_handler(CommandHandler('hello', SayHello, pass_args=True))
According to the documentation: pass_args
Determines whether the handler should be passed the arguments passed to the command as a keyword argument called args
. It will contain a list of strings, which is the text following the command split on single or consecutive whitespace characters. Default is False.
you can use RegexHandler() to do this.
Here is an example
def info(bot, update):
id = update.message.text.replace('/info_', '')
update.message.reply_text(id, parse_mode='Markdown')
def main():
updater = Updater(TOKEN)
updater.dispatcher.add_handler(RegexHandler('^(/info_[\d]+)$', info))
updater.start_polling()
Usage
The command /info_120
will return 120
and /info_007
will return 007
UPDATE
for newer versions, you may use this method instead!
MessageHandler(Filters.regex(r'pattern'), callback)