In this tutorial you will learn about the Laravel 8 Botman Chatbot Tutorial Example and its application with practical example.
In this Laravel 8 Botman Chatbot Tutorial Example tutorial I will show you how to install or integrate botman chatbot in laravel 8 application. In this tutorial you will learn to integrate and install botman chatbot in laravel 8. In this article I will share example to integrate botman chatbot in laravel 8 application.
Laravel 8 Botman Chatbot Tutorial Example
In this step by step tutorial I will demonstrate you how to integrate botman chatbot in laravel 8 application.Please follow the instruction given below:
- Step 1 – Install Laravel 8 App
- Step 2 – Connecting App to Database
- Step 3 – Install Botman and Botman Driver
- Step 4 – Create Configuration File
- Step 5 – Add route
- Step 6 – Create Controller by Artisan Command
- Step 7 – Create Blade File
- Step 8 – Run Development Server
Step 1 – Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2 – Connecting App to database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name DB_USERNAME=Enter_Your_Database_Username DB_PASSWORD=Enter_Your_Database_Password |
Step 3 – Install Botman and Botman Driver
In this step we will install botman and botman driver in laravel. Run following command to install botman composer package:
Install Botman:
1 |
composer require botman/botman |
Install Botman Driver:
1 |
composer require botman/driver-web |
Step 4 – Create Configuration File
In this step we will create configuration file for driver and cache.
config/botman/config.php
1 2 3 4 |
return [ 'conversation_cache_time' => 40, 'user_cache_time' => 30, ]; |
config/botman/web.php
1 2 3 4 5 6 |
return [ 'matchingData' => [ 'driver' => 'web', ], ]; |
Now, run following command to migrate database schema.
1 |
php artisan migrate |
Step 5 – Add route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 |
Route::get('/', function () { return view('welcome'); }); Route::match(['get', 'post'], '/botman', 'BotManController@handle'); |
Step 6 – Create Controller by Artisan Command
Now, lets create a controller named BotManController using command given below –
1 |
php artisan make:controller BotManController |
Now, go to app/http/controller/BotManController.php. and update the following methods into your controller file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php namespace App\Http\Controllers; use BotMan\BotMan\BotMan; use Illuminate\Http\Request; use BotMan\BotMan\Messages\Incoming\Answer; class BotManController extends Controller { /** * Place your BotMan logic here. */ public function handle() { $botman = app('botman'); $botman->hears('{message}', function($botman, $message) { if ($message == 'hi') { $this->askName($botman); }else{ $botman->reply("write 'hi' for testing..."); } }); $botman->listen(); } /** * Place your BotMan logic here. */ public function askName($botman) { $botman->ask('Hello! What is your Name?', function(Answer $answer) { $name = $answer->getText(); $this->say('Nice to meet you '.$name); }); } } |
Step 7 – Create Blade File
In this step we will create blade view file. Go to resources/views directory and open file that named welcom.blade.php. Then put the following html and javascript code into this blade view file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>How to install Botman Chatbot in Laravel</title> <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet"> </head> <body> </body> <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/assets/css/chat.min.css"> <script> var botmanWidget = { aboutText: 'ssdsd', introMessage: "✋ Hi! I'm form yoursite" }; </script> <script src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js'></script> </html> |
Step 8 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |