Interested how Laravel works under the hood? → Check my Laravel Core Adventures Course

Setup a Facebook Messenger chatbot in Laravel

#

There is a new PHP library called BotMan that makes it super easy to setup a Facebook Messenger chatbot in Laravel. Let's do this.

Note: I wrote a new article about building a Facebook Messenger chatbot with Laravel and BotMan Studio 2.0.

Welcome BotMan

BotMan is framework agnostic PHP library for creating chatbots. It supports multiple messenger platforms like Telegram, Slack, Microsoft, Facebook and more. It is built by Marcel Pociot and it makes developing chatbots super easy. We are going to use it in Laravel to setup a Facebook Messenger chatbot.

BotMan ❤️ Laravel

BotMan works perfectly together with Laravel. Marcel has provided a BotMan Laravel Starter which we are going to use here.

Install the Laravel application

First we need to clone the repository and install the dependencies.

git clone git@github.com:mpociot/botman-laravel-starter.git
cd botman-laravel-starter
php composer install

And of course, like with every Laravel project, we create our environment config file and generate an application key.

mv .env.example .env  
php artisan key:generate  

Configuration

Now we are ready to copy our Facebook Messenger tokens to our .env file. We need the Page Access Token which is linked to the Facebook page you want to use and the Verify Token which is used to hook up this application and the Messenger application.

// ...inside .env file
FACEBOOK_TOKEN=your-facebook-page-token
TOKEN_VERIFY=your-wehbook-verify-token
FACEBOOK_APP_SECRET=your-facebook-app-secret

Also make sure you have these Facebook settings inside you config/services.php:


// ..
'botman' => [
	// ...
	'facebook_token' => env('FACEBOOK_TOKEN'),
	'facebook_app_secret' => env('FACEBOOK_APP_SECRET')
],
// ..

Your application needs to be available for the Facebook webhook. I am using Laravel valet to generate a public HTTPS URL.

valet share
Note: If you want to see how to setup a Facebook Messenger application, the webhook and where to find the tokens, you can look it up here

Chat

Now type test in your Messenger chat of the used page and you should get back "hello!". Also try out Start conversation and see an example of a BotMan conversation. This should look like this: BotMan Conversation

Awesome right? When you take a look at the botman.php file in the routes directory you will see why the two messages already worked out of the box.

<?php
use App\Http\Controllers\BotManController;
// Don't use the Facade in here to support the RTM API too :)
$botman = resolve('botman');

$botman->hears('test', function($bot){
    $bot->reply('hello!');
});
$botman->hears('Start conversation', BotManController::class.'@startConversation');

Checkout the BotMan documentation to learn more about the features.

Conclusion

Wasn't that easy? Now you got a chatbot running, try to create conversations yourself. This year will be full of chatbots and it is great to see more and more great tools like BotMan out there to help use building them.

Do you enjoy my posts?

Sign up for my newsletter to receive updates on my latest content.

You will receive monthly updates on my latest articles and products. I do care about the protection of your data. Read my Privacy Policy.