How do I have my Bot respond with arguments?

Viewed 27246

So I've built a Telegram bot, which can receive the following commands:

  • /list
  • /info 123

This works great, as I can catch /info and pass the additional arguments as ints. But, sadly, the Telegram clients don't see /info 123 as a complete command, but just the /info part. Is there a way to make it recognize the entirety of the command as the command?

I've tried Markdown-ing it: [/info 123](/info 123), but no joy. Is this possible?

6 Answers

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)

To get the argument of command you don't even need to use pass_args as said Moein you can simply get it from context.args look at Github page. So you can pass as many arguments as you want and you will get a list of arguments! Here is an example from Github.

def start_callback(update, context):
  user_says = " ".join(context.args)
  update.message.reply_text("You said: " + user_says)

...

dispatcher.add_handler(CommandHandler("start", start_callback))

ForceReply

Upon receiving a message with this object, Telegram clients will display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply'). This can be extremely useful if you want to create user-friendly step-by-step interfaces without having to sacrifice privacy mode.

a simple shot

enter image description here

In this case, a user should send a valid number with /audio command (e.g. /audio 3, if they forgot it, we can inform and force them to do so.

source:
https://core.telegram.org/bots/api#forcereply

This is a fairly rudimentary way of creating kwargs from user input.

Unfortunately, it does require the user to be aware of the fields that can be used as parameters, but if you can provide informative response when the user doesnt provide any detectable kwarg style messages then you could probably make a better experience.

As I say, extremely rudimentary idea, and would probably be achieved faster with the regex filters available. And this would be much more reliable when checking input from the user of the "pesky" variety.

The script relies on || delimiter preceeding the command and as is shown will trim any extra characters like new lines and spaces

You can remove the extra check for commit as this is provided in order to tell the bot that you want to save your input to the database explicitly.

def parse_kwargs(update):
    commit = False
    kwargs = {}
    if update.message:
        for args in update.message.text.split('||')[1:]:
            for kw_pair in args.split(','):
                key, value = kw_pair.split('=')
                if key.strip() != 'commit':
                    kwargs[key.strip()] = value.strip()
                elif key.strip() == 'commit' and value.strip().lower() == 'true':
                    commit = True
    return kwargs, commit
Related