How to create a botman chat application in Laravel 8

Many websites uses automatic bot reply to user query. The Bot automatically reply general queries to user inquiry. This will reduce work for customer executives.  

In this article, I will show you how to create Botman chat application using Botman 2.0 package. There are also third party integration available, but you cal also do it yourself.

Follow these steps to create Botman chat application.

Step 1: Create Laravel application

First of all we need to install Laravel application using bellow command, So open your terminal or CMD and run bellow command:

composer create-project laravel/laravel botman

Step 2: Install Botman package driver

In this step, we will install botman package using composer. We will also need to install botman web driver. so we need to run following command to install botman.

Install Botman package:

composer require botman/botman

Install Botman driver:

composer require botman/driver-web

Step 3: Create controller

In this step, we will create controller. Run the below command to create controller.

php artisan make:controlle BotmanController

We need some conversion reply, though you can write your own conversion. Create below methods into controller.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use BotMan\BotMan\BotMan;
use BotMan\BotMan\Messages\Incoming\Answer;

class BotmanController extends Controller
{
    /**
     * Place your BotMan converison.
     */
    public function enterRequest()
    {
        $botman = app('botman');

        $botman->hears('{message}', function($botman, $message) {
            if ($message == 'Hi! i need your help') {
                $this->askName($botman);
            } else {
                $botman->reply("Hello! how can i Help you...?");
            }  
        });

        $botman->listen();
    }

    /**
     * Place your BotMan converison.
     */
    public function askReply($botman)
    {
        $botman->ask('Hello! What is your Name?', function(Answer $answer) {
            $name = $answer->getText();

            $this->say('Nice to meet you '.$name);
        });
    }
}

Step 4: Create route

Now we need to create two routes into routes/web.php file. Open file and add the below two routes into it.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BotmanController;

Route::match(['get', 'post'], '/chatbox', [ChatBoxController::class.'enterRequest']);

Step 5: blade view file

You already have welcome.blade.php file at resources/views folder. We will use this blade for chatbox. So update that file as below:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Botchatbox Chat Application</title>
        <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/build/assets/css/chat.min.css">
        <script src='https://cdn.jsdelivr.net/npm/[email protected]/build/js/widget.js'></script>
        <style>
            html, body {
                background-color: #fff;
                color: #636b6h;
                font-weight: 100;
                height: 100px;
                margin: 10px;
            }
        </style>
        <script type="text/javascript">
            var botmanWidget = {
                aboutText: 'Hello there',
                introMessage: "I am Botman, Happy to talk with you!"
            };
        </script>
    </head>
    <body>
    </body>
</html>

Now your application is ready. Run laravel server php artisan serve and open http://localhost:8000/ into browser.

Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

Or

No Comment