Build a simple chatbot in Laravel with BotMan and Dialogflow

  • Posted on: 4 July 2019
  • By: stillfinder

In this article we will learn how to build an assistant bot from scratch. We will use Laravel, Botman framework and Dialogflow for natural language processing. Our simple chatbot will help to find a TV show according to user's request.

Install BotMan

First you need to install BotMan Studio:
composer global require "botman/installer"

After that just install our project:
botman new assistant-bot

Create a DB for our project:
mysql -uroot -e "CREATE DATABASE assistantbot;"

And finally add DB settings to your .env file.

Setup dialogflow

1. Goto
2. Create an account with a gmail account, and “agree” to the terms & conditions.
3. Navigate Dialogflow console and create a new agent

4. Fill in all the required fields and click CREATE.

5. Save your API Key for future usage

Add dialogflow intents

1. Navigate Intents page and click on CREATE INTENT button

2. Set intent name «FindShow» and add training phrases «find tv show about dogs and cats», «Find me tv show about a girl»

Also, you will need to select "dogs and cats" and select @sys.any:any from the appeared dropdown. The same for "girl" in «Find me tv show about a girl».

3. Next, add action «findshow» to the intent

4. You can test how it works using right chatbox

Write the code

We will use service to find a TV show. All we need is to provide a search query and it will return a TV show.
The code is pretty easy to understand. For simplicity we'll put all code inside botman.php, for real project it's better to create a separate class or a conversation.

1. create a new Dialogflow middleware:

$dialogFlow = DialogFlow::create('dialog-api-key-here')->listenForAction();

Replace 'dialog-api-key-here' with the key that your saved earlier.

2. Apply the middleware globaly:


3. When 'findshow' action is fired we will extract user's query, request a TV show from and reply to a user with a result:

$botman->hears('findshow', function ($bot) {
    $extras = $bot->getMessage()->getExtras();
    $keyword = $extras['apiParameters']['any'];
    $url = '' . urlencode($keyword);
    $response = json_decode(file_get_contents($url));
    $bot->reply('That what I found: ' . $response->name);

The final botman.php code:

use App\Http\Controllers\BotManController;
use BotMan\BotMan\Middleware\Dialogflow;
$botman = resolve('botman');
$dialogFlow = DialogFlow::create('80461b1eed38401cb1ba55ea9b6d13ee')->listenForAction();
$botman->hears('findshow', function ($bot) {
    $extras = $bot->getMessage()->getExtras();
    $keyword = $extras['apiParameters']['any'];
    $url = '' . urlencode($keyword);
    $response = json_decode(file_get_contents($url));
    $bot->reply('That what I found: ' . $response->name);


Open your BotMan project in a browser and navigate to the tinker page:

Send to bot 'find tv show about cat' and check the result:


We created a very simple bot that can understand user's language using Dialogflow. As you can see, our request is different from what we setup in DialogFlow, but bot is able to understand user's input. It is just an example, and my goal was to highlight most important things. From here you are ready to build more and more features to your bot your own.