Botman Chatbot integration in Laravel 8 Tutorial

Share this Article
Reading Time: 5 minutes
1,283 Views

In several application you have seen some chats who replies very fast at anytime when you message you. It responds you immediately. This is actually handled by chatbots. To provide customer, clients 24*7 service, sometimes chatbots works.

Inside this article we will see botman chatbot integration in laravel 8. Botman is a composer package. After installation automatically it will provide the interface of chat and it’s internal functions.

We need to install botman and botman-driver composer package.

Here, we will see the step by step guide to work with Botman chatbot in laravel 8.

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


Install Botman and Botman Driver

To get the functions of chatbot, next we need to install botman into laravel setup.

Open project in terminal and run this command.

composer require botman/botman

After installation of this package, next run this command to install botman driver.

composer require botman/driver-web

Create Configuration File (Optional)

These steps are optional. These configuration file for driver and cache.

Let’c create a folder with name botman inside /config directory. Create two files inside /config/botman.

  • config.php
  • web.php

Open /config/botman/config.php

<?php

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

Open /config/botman/web.php

<?php

return [

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

Create Controller

Open project into terminal and run this artisan command to create controller class file.

$ php artisan make:controller BotManController

It will create a file with name BotManController.php at /app/Http/Controllers folder.

Open BotManController.php and write this complete code into it.

<?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);
        });
    }
}

Add Route

Open web.php from /routes folder. Add this route into it.

use App\Http\Controllers\BotManController;

//...

Route::match(['get', 'post'], 'botman', [BotManController::class, 'handle']);

Create Blade File

Open any of the view file from your application. I am opening the default view file i.e welcome.blade.php

Add this simple code into it. This code will creates a interface of Chatbox.

//...

<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: 'Write Something',
  introMessage: "✋ Hi! I'm form Online Web Tutor"
  };
</script>

<script src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js'></script>
  

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

URL: http://127.0.0.1:8000/

More in Chatbot conversation of Botman in Laravel 8, Click here to learn more.

We hope this article helped you to learn about Botman Chatbot integration in Laravel 8 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here