connecting slack to botman in laravel on localhost

2019-07-07 19:59发布

问题:

This is my routes file in laravel. Am matching any url with /botman that calls a closure which, registers a slack driver for botman and listens to the message hello. In slack am trying to set the Request URL under event subscriptions using this http://127.0.0.1:8000/botman. I get "Your URL didn't respond with the value of the challenge parameter.". What am I missing? is it on my routes file? or the 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();
});

回答1:

You could try creating a tunnel to your local host using ngrok. It is likely that the connection fails because it requires an external endpoint. It will also have an error 500 because of this.

Sometimes, these services send a test request to the endpoint (expecting an OK response) before sending the payload. You could also check whether that is a requirement from the Slack side.



回答2:

You can not use localhost in your request URLs. Slack needs to open a request to your Slack app and this only works if your request URL can be reached from the Internet.

The recommended way to develop and run Slack apps locally is to use a secure VPN tunnel, like ngrok. This tool will create a virtual URL and internally handle the communication to your local machine in a secure way. Its a commercial tool, but there also is a free plan.

You also want to make sure that you have a webserver running on your local machine.

For more details on how to use ngrok with Slack check out the official tutorial: Using ngrok to develop locally for Slack.