skip to Main Content

I have a telegram bot like this:

  • Getting updates by webhook
  • Language: C# (I welcome answers with other languages too)
  • We have the following scenario for the user:

    1. Send /MyPhoto a_parameter command to the bot
    2. Send a photo to the bot
    3. Send another photo to the bot
    4. Send another photo to the bot

Base question:

What is the best way to make sure the sent photo by the user is right after sending /MyPhoto a_parameter command?

Some ways:

  • I can save every executed command by each user in the database and fetch the last executed command by the current user and compare it with /MyPhoto, if they are equal then i sure the user is sending a photo after /MyPhoto command.
  • Create a cache system to hold the last executed command by each user (Mix with db)

But if it is possible, I want prevent fetch the last executed command
from database to improve performance.

Do you know a better solution? For example using some thing in telegram bot API to keep last executed command as hidden in send/receive messages between user and bot .


I edited the question with adding steps 3 & 4 in the above scenario.

3

Answers


  1. There are similar questions on SO such as this one. Basically there is no way other than keeping the history (or in your case keeping only last command per user). You can use a simple hash table, with chat_id as the key to retrieve interaction with each user.

    Login or Signup to reply.
  2. This is exactly ForceReply button was made for. https://core.telegram.org/bots/api#forcereply

    1) Bot receives MyPhoto command and sends this message back to user with ForceReply keyboard.

    2) Bot receives new message with reply_to_message object containing user’s previous message and photo

    Login or Signup to reply.
  3. Question 1:

    What is the best way to make sure the sent photo by the user is right
    after sending /MyPhoto a_parameter command?

    I think the best solution is to store /MyPhoto update_id for each user and compare it with uploaded photos update_id.

    See the Telegram docs:

    The update‘s unique identifier. Update identifiers start from a
    certain positive number and increase sequentially. This ID becomes
    especially handy if you’re using Webhooks, since it allows you to
    ignore repeated updates or to restore the correct update sequence,
    should they get out of order.

    Question 2:

    Do you know a better solution? For example using some thing in
    telegram bot API to keep last executed command as hidden in
    send/receive messages between user and bot.

    Using InlineKeyboardMarkup with last executed command as callback data. When the user selects an inline button, you can fetch your callback data from its update.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search