在本地主机上的 laravel 中将 slack 连接到 botman

connecting slack to botman in laravel on localhost

这是我在 laravel 中的路线文件。正在将任何 url 与调用闭包的 /botman 匹配,该闭包为 botman 注册一个 slack 驱动程序并收听消息 hello。 在 slack 中,我正在尝试使用此 http://127.0.0.1:8000/botman 在事件订阅下设置 Request URL。我得到 "Your URL didn't respond with the value of the challenge parameter."。我错过了什么?它在我的路线文件中吗?或 url?

<?php
use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\Drivers\Slack\SlackDriver;
use BotMan\BotMan\Drivers\DriverManager;

Route::match(['get', 'post'],'botman', function () {

    DriverManager::loadDriver(SlackDriver::class);

    // Create BotMan instance
    $config = [
            'slack' => [
                'token' => '***slack Token***' //slack token
                ]
            ];
    $botman = BotManFactory::create($config);

    // give the bot something to listen for.
    $botman->hears('hello', function (BotMan $bot) {
        $bot->reply('Hello yourself.');
    });

    // start listening
    $botman->listen();
});

您可以尝试使用 ngrok 创建到本地主机的隧道。连接失败很可能是因为它需要外部端点。因此它也会出现错误 500。

有时,这些服务会在发送有效负载之前向端点发送测试请求(期待 OK 响应)。您还可以检查这是否是 Slack 方面的要求。

您不能在请求 URL 中使用 localhost。 Slack 需要打开对您的 Slack 应用程序的请求,这仅在您的请求 URL 可以从 Internet 到达时才有效。

在本地开发和 运行 Slack 应用程序的推荐方法是使用安全的 VPN 隧道,例如 ngrok。此工具将创建一个虚拟 URL 并以安全的方式在内部处理与本地计算机的通信。它是一个商业工具,但也有一个免费计划。

您还需要确保您的本地计算机上有网络服务器 运行ning。

有关如何将 ngrok 与 Slack 一起使用的更多详细信息,请查看官方教程:Using ngrok to develop locally for Slack