0
  1. 听到后备都很好:
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. 有 VK 驱动程序的组内听到NOT WORKS,但后备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
});

我想做的一切——例如用于 VK 和 Telegram 的通用机器人,但我需要检查用户是否在目标平台的黑名单中,所以如果“驱动程序”类似于来自 botman 的“MatchingMiddleware”——我不明白为什么听到在“组”内不起作用,这是来自网站的官方示例: 官方 botman 视频教程截图

这个例子也不起作用:https ://botman.io/2.0/receiving#command-groups-drivers

在 VK Driver 网站上没有关于“组”的信息:https ://github.com/yageorgiy/botman-vk-community-callback-driver

4

1 回答 1

0

更新: 我检查了 Marcel 视频和他的工作流程,在 Botman 视频中很多次他在 Sublime Text 编辑器中使用了一些魔术键,所以我用谷歌搜索它,它是 PHP Companion,所以我通过工具安装了 Sublime Text 和 PHP Companion - >Command Pallete,然后单击 VkCommunityCallbackDriver,然后再次使用命令 PHP Companion:Find Use 的 Tools->Command Pallete,出现以下代码:

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

魔术起作用了,现在带有驱动程序的组方法起作用了,我仍然不明白为什么在文档或视频课程中的任何地方都没有说明这一点,就好像组方法不是主要方法一样。

我仍然不明白为什么它不能从“盒子”开始工作,因为我一直认为编码人员使用这种复杂的解决方案来轻松生活。但是经过 2 天的空搜索我累了,所以我根据 Botman 的官方视频教程https://beyondco.de/course/build-a-chatbot/middleware-system编写了自定义中间件来“听到” VK 驱动程序/匹配中间件

应用\中间件\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"]);
    }
}

路线\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');
});

如果有人知道为什么它会以这种方式工作,但不能在 Vanilla 中工作,我会很高兴看到解释。

于 2021-08-26T18:47:48.033 回答