botman / driver-telegram

BotMan Telegram Driver
MIT License
88 stars 75 forks source link

Global commands are not working during the asking of a question #43

Closed pavlentij closed 2 years ago

pavlentij commented 6 years ago

I'm sending a text message with links (like /ovd_info_12) and with two buttons "Next Page" and "Subscribe".

Now the system is waiting for the click or for some text entered.

Is there a way that if text was entered then the system will process if through the global hearing rules? Because if user clicked on the "/ovd_info_12" then it's not processed by the globally defined hear rule.

        $buttons = [];
        $buttons[] = Button::create('Next Page')->value('ovd_page_2');
        $buttons[] = Button::create('Subscribe')->value('ovd_subscribe');

        $question = Question::create($text)
            ->callbackId('ovd_place')
            ->addButtons($buttons);

        $this->ask($question, function (Answer $answer) {

            if ($answer->isInteractiveMessageReply()) {
                if ($answer->getValue() == 'ovd_subscribe') {
                    $this->say('You are subscribed. Thanks.', ['parse_mode' => 'HTML']);
                }
            }
        }, ['parse_mode' => 'HTML']);`
christophrumpel commented 6 years ago

Hey @pavlentij ,

it is possible to skip / stop the conversation for certain incoming messages. Besides defining triggers for that inside your conversation, you can make them global like this:

$botman->hears('stop', function(BotMan $bot) {
    $bot->reply('stopped');
})->stopsConversation();

This way stop will trigger a listener from your botman route file even if you're in a conversation. Is this what you need?

pavlentij commented 6 years ago

Thanks for the quick reply.

Yes, I knew about the "stop" feature, but that's not exactly what I need.

Please see the screenshot - there are two same messages were sent with the same command.

The first one didn't work because it was recognized by the system as not interactive message reply for the previous message with buttons. But the second one was working perfectly because the previous conversation (question + answer) finished, and now the command was sent through the regular list of hears.

monosnap 2018-05-22 09-51-15
christophrumpel commented 6 years ago

Hmm so you want to make the first command work right? Is it part of the conversation or can the conversation be stopped after clicking the link? Do you want to continue the conversation after the first command? Then skip could be of use for your conversation. But I guess I am not fully understanding what your goal is / what is happening in your bot.

pavlentij commented 6 years ago

Our logic of the bot (https://t.me/SaveDniproBot) is:

  1. By sending message with command "/ovd" we are giving to user list of regions as buttons.
  2. After the selection of a region we are showing a message with 5 companies (name, address and command like /ovd_place_info_2018426679). And with two buttons "Show Next 5 Companies", "Subscribe".
  3. When user clicking on buttons everything is perfect.
  4. But user can click on the command inside of the message and we want to show the company info.
  5. Also if in the reply to the message from the step 2 a user will send again /ovd then we want to start the process again.

All hears for /ovd and for /ovd_place_info_2018426679 are defined in the botman.php.

Probably we have to catch these commands also in the callback function of the conversation.

pavlentij commented 6 years ago

I've found the way how to make it work. Maybe that's not a good solution but it's working.

There is a global config value "conversation_cache_time". If it's set to zero then the system is not storing it to the cache and during the answer for the conversation the system will not be able to find that conversation and will use global routing rules of the bot.

But for the proper for clicked buttons, we have to define all available answers for questions in the routings.

In my case all buttons have values like "/ovd_place_info_2018426679" and I have in the routes file:

$botman->hears('/ovd_place_info_([0-9]+)', BotManController::class.'@ovdPlaceConversation');