phpvkbotman

Botman Studio with VK Community Callback driver hears not works inside Group


  1. hears and fallback WORKS fine both:
use App\Http\Controllers\BotManController;
use BotMan\BotMan\Messages\Conversations\Conversation;
    
$botman = resolve('botman');
$botman->hears('Начать', function($bot) {
 $bot->startConversation(new OnboardingConversation);
}); //this works
$botman->fallback(function($bot) {
 $bot->startConversation(new OnboardingConversation);
}); //this works
  1. inside group with VK driver hears NOT WORKS, but fallback WORKS:
use App\Http\Controllers\BotManController;
use BotMan\BotMan\Messages\Conversations\Conversation;
    
$botman = resolve('botman');
$botman->group(['driver' => [VkCommunityCallbackDriver::class]], function($botman) {    
 $botman->hears('Начать', function($bot) {
  $bot->startConversation(new OnboardingConversation);
 }); //this NOT works
 $botman->fallback(function($bot) {
  $bot->startConversation(new OnboardingConversation);
 }); //this works
});

all I want to do - universal bot for VK and Telegram for example, but I need to check if user in blacklist in target platform, so if "Driver" is some similar to "MatchingMiddleware" from botman - I don't understand why hears not works inside of "Group", this is official example from website: official botman video tutorial screenshot

This example not works too: https://botman.io/2.0/receiving#command-groups-drivers

On VK Driver website there is no information about "Group": https://github.com/yageorgiy/botman-vk-community-callback-driver


Solution

  • UPDATE: I checked Marcel videos and his workflow, and a lot of times in Botman videos he used some magic keys in Sublime Text editor, so I google it, and it was PHP Companion, so I installed Sublime Text and PHP Companion through Tools->Command Pallete, and clicked on VkCommunityCallbackDriver, and then again Tools->Command Pallete with command PHP Companion:Find Use, and this code appear:

    use BotMan\Drivers\VK\VkCommunityCallbackDriver;
    use BotMan\Drivers\Telegram\TelegramDriver;
    

    the magic worked and now the group method with the driver works, I still don't understand why this is not indicated anywhere in the documentation or in video courses, as if the group method is not the main one.

    I am still don't understand why it's not works from "the box", because I always thought that coders using this complex solutions for easy life. But after 2 days of empty searching I am tired, so I wrote custom Middleware to "hears" VK Driver only, based on official video tutorials from Botman https://beyondco.de/course/build-a-chatbot/middleware-system/matching-middleware:

    App\Middleware\VkMatchingMiddleware.php

    <?php
    namespace App\Middleware;
    use BotMan\BotMan\BotMan;
    use BotMan\BotMan\Interfaces\Middleware\Matching;
    use BotMan\BotMan\Messages\Incoming\IncomingMessage;
    
    class VkMatchingMiddleware implements Matching
    {
        /**
    * Handle a captured message.
    *
    * @param IncomingMessage $message
    * @param callable $next
    * @param BotMan $bot
    *
    * @return mixed
    */
        public function matching(IncomingMessage $message, $pattern, $regexMatched)
        {
            return $regexMatched && isset($message->getExtras()["client_info"]);
        }
    }
    

    Routes\botman.php:

    <?php
    use App\Http\Controllers\BotManController;
    use BotMan\BotMan\Messages\Conversations\Conversation;
    use App\Middleware\VkMatchingMiddleware;
    
    $VkMatchingMiddleware = new VkMatchingMiddleware();
    
    $botman = resolve('botman');
    
    $botman->hears('Begin', function($bot) {
        $extras = $bot->getMessage()->getExtras();
        $bot->reply(print_r($extras, 1));
    })->middleware($VkMatchingMiddleware);
    
    $botman->fallback(function($bot) {    
        $bot->reply('fallback');
    });
    

    If someone knows why it works this way but not works from Vanilla, I will glad to see explanation.