假装电报机器人正在打字?

Pretending that telegram bot is typing?

如何让机器人假装它正在输入消息?

聊天机器人假装输入时会出现以下文字:

我使用 python aiogram 框架,但对原生 Telegram API 的建议也会有所帮助。

我郑重建议使用 python-telegram-bot 库,它有丰富的 Wiki。 code snippets.

中描述了您想要的解决方案

您可以手动发送操作:

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

或者创建一个装饰器,然后可以将其用于您希望在处理时显示该操作的任何函数:

from functools import wraps
from telegram import (ChatAction)

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.