ba0f3 / telebot.nim

Async Telegram Bot API Client implement in @Nim-Lang
MIT License
165 stars 24 forks source link

How can I catch an unknown command? #21

Closed facorazza closed 5 years ago

facorazza commented 5 years ago

I'd like to use a proc like this one in events.nim:

proc onUnknownCommand*(b: TeleBot, cb: CommandCallback) =
  b.commandCallbacks["-"].add(cb)

but I'm not sure how things are handled so I'm asking for help.

Perhaps, this new proc is not necessary. However, we must be sure not to catch commands already processed with onCommand().

ba0f3 commented 5 years ago

Well, we can pass them to a catch all handler, or pass them to onUpdate when no match command found.

facorazza commented 5 years ago

My idea would be to have a structure like this one:

bot.onUpdate(updateHandler)
bot.onCommand("command_a", commandAHandler)
bot.onCommand("command_b", commandBHandler)
bot.onCommand("command_c", commandCHandler)
bot.onUnknownCommand(unknownCommandHandler)

I guess if we did like so:

bot.onCommand("command_a", commandAHandler)
bot.onCommand("command_b", commandBHandler)
bot.onCommand("command_c", commandCHandler)
bot.onUpdate(updateHandler)

where the unknown command is caught as a regular message by onUpdate(), it would work as well, but it might be confusing to mix messages and commands into one proc when commands usually have their own.

Could we overload onCommand() to catch unknown commands? I would still prefer to use a separate proc though.