Pretending that telegram bot is typing?

自闭症网瘾萝莉.ら 提交于 2021-02-11 14:12:09

问题


How can I make a bot to pretend that it is typing a message?

The following text appears in the chat when the bot pretend to type:

I use the python aiogram framework but a suggestion for the native Telegram API would be also helpful.


回答1:


I seriously suggest using the python-telegram-bot library which has an extensive Wiki. The solution for what you want is described in code snippets.

You can manually send the action:

bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)

Or create a decorator which can then be used on any function you wish to show that action on whilst processing:

from functools import wraps

def send_typing_action(func):
    """Sends typing action while processing func command."""

    @wraps(func)
    def command_func(update, context, *args, **kwargs):
        context.bot.send_chat_action(chat_id=update.effective_message.chat_id, action=ChatAction.TYPING)
        return func(update, context,  *args, **kwargs)

    return command_func

@send_typing_action
def my_handler(update, context):
    pass # Will send 'typing' action while processing the request.


来源:https://stackoverflow.com/questions/61520440/pretending-that-telegram-bot-is-typing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!