簡體   English   中英

Python Telegram Bot發送動作消息

[英]Python Telegram Bot Send message with action

我在python 3中使用python telegram bot庫,並編寫了以下代碼:

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")

這段代碼可以正確地向客戶端發送一條消息,但是我希望當機器人向客戶端發送一條消息時,在客戶端屏幕上方(在電報Messenger中)顯示但正在Typing... 我在sendMessage方法中使用action = telegram.ChatAction.TYPING作為選項,但它不起作用,也找不到我的問題。 有指導嗎?

問題是圖書館, from telegram import *改為寫import telegram

為了確保用戶可以看到ChatAction ,您需要在調用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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM