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 https://dialogflow.com
2. Create an account with a gmail account, and “agree” to the terms & conditions.
3. Navigate Dialogflow console and create a new agent
2ae3da6547.jpg

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

5. Save your API Key for future usage
2401dc6fb6.jpg

Add dialogflow intents

1. Navigate Intents page and click on CREATE INTENT button
a1436937d2.jpg

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

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».
0a06b2ecde.jpg

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

4. You can test how it works using right chatbox
b906fd5973.jpg

Write the code

We will use api.tvmaze.com 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:

$botman->middleware->received($dialogFlow);

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

$botman->hears('findshow', function ($bot) {
    $extras = $bot->getMessage()->getExtras();
    $keyword = $extras['apiParameters']['any'];
    $url = 'http://api.tvmaze.com/singlesearch/shows?q=' . urlencode($keyword);
    $response = json_decode(file_get_contents($url));
 
    $bot->reply('That what I found: ' . $response->name);
})->middleware($dialogFlow);

The final botman.php code:

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

Testing

Open your BotMan project in a browser and navigate to the tinker page:
4c92ea70b1.jpg

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

Conclusion

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.