假装telegram机器人在打字?

问题描述 投票:0回答:1

如何让机器人假装在打字?

当机器人假装在打字时,聊天中会出现以下文字。

enter image description here

我使用的是python 通行图 框架,但对原生Telegram API的建议也会很有帮助。

bots telegram python-telegram-bot
1个回答
1
投票

我严重建议使用 python-telegram-bot 库,它有一个广泛的Wiki。 你想要的解决方案在 代码段.

你可以手动发送动作。

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

或者创建一个装饰器,然后可以用于任何你希望在处理时显示该动作的函数。

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.
© www.soinside.com 2019 - 2024. All rights reserved.