如何在 telepot 模块中获取传出消息 ID?
How to get an outgoing message ID in telepot module?
我正在使用 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,因为它是来自机器人的外发消息(不是用户传入的消息,我可以获得它的 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'}
我正在使用 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,因为它是来自机器人的外发消息(不是用户传入的消息,我可以获得它的 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'}