【问题标题】:Pretending that telegram bot is typing?假装那个电报机器人正在打字?
【发布时间】:2020-04-30 09:42:26
【问题描述】:

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

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

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

【问题讨论】:

    标签: bots telegram python-telegram-bot


    【解决方案1】:

    我强烈建议使用具有广泛 Wiki 的 python-telegram-bot 库。 code snippets 中描述了您想要的解决方案。

    您可以手动发送操作:

    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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 2019-09-16
      • 2018-07-22
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多