【问题标题】:Python Telegram Bot Send message with actionPython Telegram Bot 发送带有动作的消息
【发布时间】:2018-09-21 15:01:06
【问题描述】:

我在 python 3 中使用 python 电报机器人库,我写了这段代码:

from telegram import *
from telegram.ext import *
bot.send_chat_action(chat_id=update.message.chat_id , 
action = telegram.ChatAction.TYPING)
bot.sendMessage(chat_id=update.message.chat_id, text="Hi")

此代码将正确向客户端发送消息,但我希望当机器人向客户端发送消息时,客户端屏幕上方(在电报信使中)显示但为 Typing...。 我在 sendMessage 方法中使用action = telegram.ChatAction.TYPING 作为选项, 但它不起作用,我找不到我的问题。 有指南吗?

【问题讨论】:

  • 什么是法规??
  • 当您在电报中输入一些文本时,将显示“正在输入...”文本。

标签: python python-3.x bots telegram telegram-bot


【解决方案1】:

问题是库,写import telegram而不是from telegram import *

【讨论】:

    【解决方案2】:

    为确保ChatAction 对用户可见,您需要在调用ChatAction 方法后延迟几秒钟:

    from telegram import *
    from telegram.ext import *
    from time import sleep
    from random import random
    
    bot.sendChatAction(chat_id=update.message.chat_id , 
    action = telegram.ChatAction.TYPING)
    
    sleep(random() * 2 + 3.)
    
    bot.sendMessage(chat_id=update.message.chat_id, text="Hi")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多