假装电报机器人正在打字? [英] Pretending that telegram bot is typing?

查看:101
本文介绍了假装电报机器人正在打字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使机器人伪装成正在键入消息?

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:

我使用python aiogram 框架,但对本机Telegram API的建议也将有所帮助.

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

推荐答案

我认真建议使用具有广泛Wiki的 python-telegram-bot 库.代码段.

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.

您可以手动发送操作:

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.

这篇关于假装电报机器人正在打字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆