Search code examples
telegramtelegram-botpython-telegram-bot

Valid processing of concurrent replies in Telegram bot


I have a question-answer gameplay in my telegram bot and I have an issue in concurrent replies:

bot: question_1
player_1: answer_1_to_question_1
bot: answer correct, question_2
player_2: delayed answer to question_1  <--- problem
bot: invalid answer to question_2       <--- problem

So the problem is that player_2 sends a reply to first question, and because of the delay it arrives to bot when he expects answer to second question.

I would like to accept only first answer and ignore the second one, if it is for non-actual question. There is a reply_to_message structure in telegram message:

{'reply_to_message': {'from': {'username': u'Bot', 'first_name': u'', 'id': 1}, 'text': u'some text', 'chat': {'type': u'group', 'id': -2, 'title': u''}, 'date': 1487442200, 'message_id': 10351144}

I'm trying to save for each chat what message was send there last and ignore reply if it doesn't match last message. But the problem is that message_id for outgoing message is not available before I send it. And outgoing message text doesn't match text in reply (for example, they strip markup etc).

Is there a reliable way to understand is this incoming message a reply to latest outgoing message?


Solution

  • It is possible to read message_id of just after you send a message (for example, updated message is returned from sendMessage method of python-telegram-bot). So it is possible just to save it and compare with reply_to_message.message_id.