我正在使用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 ...')因此,正如你在上面的代码中所看到的,我需要检查消息3是否回复了消息2。但是,我无法获得消息2的ID,因为它是来自bot的传出消息(不是用户的传入消息,我可以获得它的ID)。那么,我如何才能做到这一点呢?
谢谢。
发布于 2016-09-11 12:24:21
您应该能够获取已发送消息的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'}https://stackoverflow.com/questions/39424071
复制相似问题