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:
- Send
/MyPhoto a_parameter
command to the bot - Send a photo to the bot
- Send another photo to the bot
- Send another photo to the bot
- Send
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
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.
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 andphoto
Question 1:
I think the best solution is to store /MyPhoto
update_id
for each user and compare it with uploaded photosupdate_id
.See the Telegram docs:
Question 2:
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.