If you followed my previous article regarding setting up BotMan with Laravel and writing your first bot then awesome! If not then
check out part 1 here.
For part 2, we are now going to cover setting up our BotMan chatbot with Slack and how to integrate it
using the BotMan slack drivers.
Using BotMan with Slack
Installing Slack Drivers
You can check which drivers are installed by running php artisan botman:list-drivers. This will run a laravel
custom command which will detail which drivers are installed. If you have been following along from the last tutorial then
you will see that Web should be installed by default as seen below:

Our next step is to install the Slack drivers for BotMan. You can do so using the following command:
php artisan botman:install-driver slack
Upon completion you should see the following output:


Before we can setup our slack application, we need to have a public URL to our bot so that the Slack app
can utilize a WebHook in order to interact with our application. We will use ngrok to do this as it allows
us to expose our localhost using a secure HTTP tunnel connection.
You will have to firstly download Ngrok here. Once downloaded, unzip the application.
After installing Ngrok, you must create a tunnel and expose your localhost to the web. I am currently using
Apache and have a virtualhost for the helloworldbot that we have created. My local dev site is accessed on
helloworldbot.app so in order to expose that virtual host I run the following:
./ngrok http -host-header=rewrite helloworldbot.app:80
If you are just running on your localhost then you can use:
./ngrok http 80
Once ngrok is running you should see an output of the application similiar to the following:

If I now visit the URL http://e50788a3.ngrok.io then I will see our BotMan installation!

For further installation instructions or if you run into any issues then ask me in the comments
or consult the Ngrok documentation.
Setting up Slack
Creating Slack App
First thing's first is to create a Slack App. First visit the Slack API and create your application.
Simply enter a name and the workspace you will be using the bot on. The workspace in this case defines the slack group that you want to use the
Bot on.

Configuring your Slack App
The main elements of the app that we want to configure in order to use our Slack application is the following:
- Interactive Messages
- Event Subscriptions
- Bot User
Setting up Interactive Messages
You will need to enable interactive messages and then setup the Request URL
field
to be that of your Ngrok forwarding url and append /botman so that Slack can access our BotMan installation and logic or Botman controller in this case.

Setting up Event Subscriptions
In order for our app (bot) to listen to slack messages and respond when say a message is posted to a group
or a user is privately messaged we need to subscribe to those events. This allows our app to receive the messages
and reply to them. We will fill in our BotMan webhook and then listen to all messages on channels
and direct messages. Don't forget to fill in your WebHook URL.

Setting up our Bot User
We need to setup our Bot User in order to have a Slack user which users will respond to.
Simply fill in the bot details and save changes.

Finishing our Slack App configuration
The last step is to install the app to your workspace. Simply click Install App and this will
then generate some OAuth tokens that we will use in our BotMan app config.

Once you have installed the app, you should receive two OAuth tokens. We are only concerned with the Bot OAuth token for now.
Open your .env file in the helloworldbot project and add the following configuration key:
SLACK_TOKEN=<YOUR BOT OAUTH TOKEN>
If you look at config/botman/Slack.php you will see that the SLACK_TOKEN is fetched from your env file by default as seen below:
{% highlight php startinline %}
<?php
return [
'token' => env('SLACK_TOKEN'),
];
{% endhighlight %}
Testing our Slack and BotMan app
The final step is to test our app!
Before we test, remember the routes that we configured for our helloworldbot app as defined in our BotMan controller:
{% highlight php startinline %}
<?php
class BotManController extends Controller
{
/**
* Place your BotMan logic here.
*/
public function handle()
{
$botman = app('botman');
$botman->hears('hello', 'App\Http\Controllers\Listen\HelloBotCommands@handleSayHello');
$botman->hears("hello I'm {name}", 'App\Http\Controllers\Listen\HelloBotCommands@handleSayHelloWithName');
$botman->listen();
}
{% endhighlight %}
Now with that in mind, let's test our app! (note my bot is called tastedivebot but you can call yours whatever you wish)
I can simply add the bot to a group and then type hello and we will see our bot respond to the messages!

Wrapping Up
That's it. You have now configured the BotMan app that we made last tutorial with Slack and used Ngrok to do so.
Let me know in the comments below if you have any feedback or comments.
Happy Coding!
Comments