who*_*AmI 1 python telegram python-telegram-bot telegram-bot
我正在使用 Telepot 模块使用 python 创建电报机器人。我需要获取传出消息的消息 ID,以便能够检查用户是否会回复该消息。下面的一段代码阐明了我想要做什么:
import telepot
bot = telepot.Bot('Some Token')
def handle(msg):
chat_id = msg['chat']['id']
message_id = msg['message_id'] # I can get Id of incoming messages here
command = msg['text']
if command == '/command': # Message (incoming) 1 sent by user
bot.sendMessage(chat_id, 'Some message') # Message (outgoing) 2 sent by bot
elif ('''msg was in reply of message 2'''): # Message (incoming) 3 sent by user (MY PROBLEM IS HERE!!!)
# Do something
pass
bot.message_loop(handle, run_forever = 'Running ...')
Run Code Online (Sandbox Code Playgroud)
因此,正如您在上面的代码中所看到的,我需要检查消息 3 是否是对消息 2 的回复。但是,我无法获取消息 2 的 ID,因为它是来自机器人的外发消息(不是用户的传入消息,其中我可以得到它的ID)。那么我怎样才能做到这一点呢?
谢谢。
您应该能够获取message_id
已发送的消息:
>>> import telepot
>>> from pprint import pprint
>>> bot = telepot.Bot('TOKEN')
>>> sent = bot.sendMessage(9999999, 'Hello')
>>> pprint(sent)
{u'chat': {u'first_name': u'Nick', u'id': 9999999, u'type': u'private'},
u'date': 1473567584,
u'from': {u'first_name': u'My Bot',
u'id': 111111111,
u'username': u'MyBot'},
u'message_id': 21756,
u'text': u'Hello'}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4052 次 |
最近记录: |