Search code examples
pythontelegramtelegram-botpython-telegram-bot

How to get an outgoing message ID in telepot module?


I'm using telepot module to create a telegram bot using python. I need to get the message Id of an outgoing message to be able to check if the user will reply to that message or not. The piece of code below clarifies what I want to do:

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 ...')

So as you can see in the code above I need to check if message 3 was in reply to message 2. However, I can't get the ID of message 2 because it's an outgoing message from bot (Not an incoming message by user which I can get it's ID). So how can I achieve that?

thanks.


Solution

  • You should be able to get message_id of the sent message:

    >>> 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'}