szastupov / aiotg

Asynchronous Python library for building Telegram bots
MIT License
381 stars 44 forks source link

Handle text and commands at the same time #80

Closed Podidiving closed 4 years ago

Podidiving commented 4 years ago

Hi! I think, this framework is great, and I use it sometimes. Now I'm facing with next problem: I want my bot to have default text handler and also to have special handler for some commands For example, I have next code:

image

Now, if I'm sending my bot command /start, but instead of handle_start_command callback, it will execute handle_text callback. What's the problem, and how to overcome it?

szastupov commented 4 years ago

Hey there! Thanks for the good words. Try using @bot.default decorator, instead of @bot.handle("text"), it should do what you want. Note that default passes you the whole json payload, so to extract text do something like:

@bot.default
def echo(chat, message):
    return chat.reply(message["text"])
Podidiving commented 4 years ago

Thanks! Now it's working :)