我正在使用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)。 那我怎么能实现呢?
感谢。
答案 0 :(得分:3)
您应该能够获得已发送消息的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'}