首页 > 解决方案 > 如何在使用 python 的电报 bot api 中使用 ForceReply?

问题描述

我希望我的机器人向用户发送图像,而用户只需回复该特定图像。并将他的回复作为字符串存储在变量中。所以使用sendPhoto的方法以及一个名为 files 的字典,其中包括photo具有图像 URL 和reply_markup我设置为 ForceReply 的属性。一切都很好,但我无法看到强制回复。在我(机器人)发送图像后,我无法得到用户回复的内容。

files = {'photo': open("/home/preetham/Downloads/selenium/cap.jpg", 'rb'),
         'reply_markup':'ForceReply'}
status = requests.post("https://api.telegram.org/[MY BOT API HERE]/sendPhoto?chat_id=" + UserID, files=files)

上面的代码能够发送图像但不能强制回复。当用户在收到这张照片后键入回复时,如果我尝试抓取他回复的消息,我总是会收到上一条消息,但不会收到最近的消息,我使用了以下代码。

while True:
    try:
        js = requests.post("https://api.telegram.org/[MY BOT API HERE/getUpdates").json()
        reply = js['result'][-1]['message']['text']
        if len(reply) > 0:
            print('Reply received')
            break
        else:
            continue
    except:
        continue

如何对我发送的照片应用强制回复?我怎样才能得到那张照片的回复?

标签: python-3.xseleniumpython-requeststelegramtelegram-bot

解决方案


推荐阅读