Laravel 8.x Botman Chatbot Example Tutorial

In this lesson i will show you how you can setup Botman Chatbot in your Laravel application.BotMan is a framework agnostic PHP library that is designed to simplify the task of developing innovative bots for multiple messaging platforms, including Slack, Telegram, Microsoft Bot Framework, Nexmo, HipChat, Facebook Messenger, WeChat and many more.

In this laravel botman example tutorial you will learn from scratch. If you don't know how to add laravel chatbot then don't worry, you are a right place. I will show you step by step in this laravel chatbot tutorial.

So let's install botman in laravel 8. I will use fresh laravel app so that i can show you from scratch.

setup-botman-chatbot-in-laravel-app

Requirements

  1. PHP >= 7.1.3
  2. For BotMan Studio checkout Laravel's requirements

Getting Started

Now lets follow our project start to finis to complete How to Install Botman Chatbot in Laravel 5.

 

Step 1: Install Laravel 8

For getting fresh laravel app, open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

 

Step 2: Install Botman and Botman Driver

In this step we will install botman composer package and also install botman web driver. so we need to run following both command to install botman.

 

Install Botman:

composer require botman/botman

 

Install Botman Driver:

composer require botman/driver-web

 

Step 3: Create Configuration File

This step is not required to follow. But you can create configuration file for driver and cache. so let's create bot file on config folder and write code like as i gave you bellow:

config/botman/config.php

return [
    'conversation_cache_time' => 40,
    'user_cache_time' => 30,
];

 

config/botman/web.php

return [
   
    'matchingData' => [
        'driver' => 'web',
    ],
];

 

Step 4: Create Routes

Here, we need to add create routes for botman request. so open your "routes/web.php" file and add following route.

routes/web.php

Route::get('/', function () {
    return view('welcome');
});
    
Route::match(['get', 'post'], '/botman', 'BotManController@handle');

 

Step 5: Create Controller

In this step, we need to create one controller as BotManController. in this controller we need to write code of botman reply and conversation. you can also write your own logic latter.

 

Read also How to import and export csv file in laravel ?

 

app/Http/Controllers/BotManController.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 5: Update Blade File

In this file, we need to update some code on welcome balde file. we need to add botman widget in welcome.blade.php file.

resources/views/welcome.blade.php

 

Now we are ready to run our chatbot example with laravel 5.8 so run bellow command for quick run:

php artisan serve

 

Now you can open bellow URL on your browser:

http://localhost:8000

 

Hope it will work for your app. You can also check botman official documentation to know more about botman. 

 

#laravel #botman-chatbot #botman #laravel-package