laraveltelegramtelegram-bot

use ForceReply option to get input from user without reply interface in telegram


We know that there is ForceReply option to get input from user in telegram api.

Telegram said :

Upon receiving a message with this object, Telegram clients will display a reply interface to the user (act as if the user has selected the bot‘s message and tapped ’Reply'). This can be extremely useful if you want to create user-friendly step-by-step interfaces without having to sacrifice privacy mode.

Now I used irazasyed/telegram-bot-sdk to make my telegram bot. in that package to create a force_reply interface we should do like this :

$forceReply = Keyboard::forceReply(['force_reply' => true]);

$this->replyWithMessage([
    'text'         => 'Please enter your name ?',
    'reply_markup' => $forceReply
]);

Result is like :

enter image description here

But in usages in other bot for example PollBot that uses this option , when shows a question and want to get answer , reply interface does not show.

I want to do same.I know that if I set value of force_reply to false it done but I do not know after that how can I detect that text that user entered is related to which my question.

what do I do really ?


Solution

  • What I've done for my last bot is:

    I created a table for user history which holds some info about users like their username and chat_id, I also keep track of a user's last state in last_state column

    So when a user asks a question, I update the user's state field in DB with current state, Then upcoming answer is for the question in user's last state, Let me go with a short-simple example.

    A: Propmpt question x for user with chat_id n

    B: Update state of user whose chat_id is n equal to x in DB

    C: Get the answer

    D: Get chat_id and then fetch user's last_state from DB, the state indicates the question

    Now you know which question she's answering to