Hello!
I want to interact with user and work with his keyboard answers.
My code:
$data = [];
$data['chat_id'] = $chat_id;
$data['text'] = 'Choose date:';
//2
$keyboard[] = ['Yesterday'];
$keyboard[] = ['Today'];
$keyboard[] = ['Tomorrow'];
$data['reply_markup'] = new ReplyKeyboardMarkup(
[
'keyboard' => $keyboard,
'resize_keyboard' => true,
'one_time_keyboard' => true,
'selective' => false
]
);
return Request::sendMessage($data);
After choosing the button, I see GenericmessageCommand.php output, but I want to get what button user pressed and create conditions on it.
How?
You can use Conversation for this, check surveycommand for examples.
@jacklul
Just tested survey, but it not works without DB. I set protected $need_mysql = false; and saw genericMessage again after the 1st question :disappointed:
Compare the text directly?
if (strpos($user_input, 'Today')) {
//something
}
It will be extremely unreliable to do something like this without database, unless you write something like conversation based on text files.
Compare the text directly?
You mean in GenericmessageCommand.php?
Sorry, I don't clearly understand how conversation works.
I can't use this without DB?
If command XXX initiates Conversation any text sent to bot (Genericmessage) is sent to XXX command.
To make bot know which command to use it needs to store it somewhere.
It is possible to rewrite Conversation.php to use text files instead of DB.
If command XXX initiates Conversation any text sent to bot (Genericmessage) is sent to XXX command.
Yep, that's what I looking for, but see my default genericmessage every time after selecting keyboard answer.
To make bot know which command to use it needs to store it somewhere.
Nope, with webhook all my commands works without store!
Only conversation not works and I can't understand why.
Nope, with webhook all my commands works without store!
Only conversation not works and I can't understand why.
I would say that conversation needs to maintain the state (at least, in which point of the conversation is the user right now, so the bot can accordingly offer the next question of the dialog in next iteration). If you don't maintain the state somewhere the bot will keep presenting the user the same question over and over.
@juananpe Thx for the explanation! :+1:
In case 0 I select username from DB, but in case 1 this username is empty. How to save values between cases and not in DB?
Also I have this line now:
if (empty($text) || !($text == 'Yesterday' || $text == 'Today' || $text == 'Tomorrow')) {
But what if I has 10 buttons? Do I need to add all 10 conditions to if statement?
You could select the username before you enter the switch statement, since you need it in different states anyway. Or you just fetch it in case 1 as well, since it would be fetched once anyway.
Regarding the buttons, you can work with an array:
$buttons = ['Yesterday', 'Today', 'Tomorrow', 'all others...'];
if (empty($text) || !in_array($text, $buttons)) {
You can just define the array once an reuse it wherever you need the same buttons.
@Sogl All working now?
If you still have a question about this, just ask 馃槂
@noplanman Yep, all is working in my production machine.
I'll put some lines here if I have questions.
Most helpful comment
I would say that conversation needs to maintain the state (at least, in which point of the conversation is the user right now, so the bot can accordingly offer the next question of the dialog in next iteration). If you don't maintain the state somewhere the bot will keep presenting the user the same question over and over.