Create A Botman chatbox Application in Laravel 6

In this example, I will explain to you how to create simple chatbox using Botman in your laravel project. We will install Botman with laravel composer. so you can easily use it.

There are lots bots available in the market but Botman is free and easily understand with laravel so we can use them. I always prefer Botman in laravel.

So let start our first step of our tutorial.


Step 1: Install a Laravel

In first step, we have set up to laravel 6. So run bellow command and get clean fresh laravel 6 application.

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

Step 2: Install Botman and Botman Driver

In this step, we have to install the Botman package and driver.

Install Composer of Botman

composer require botman/botman

Install Driver of Botman

composer require botman/driver-web

Step 3: Create Routes

Open your web.php file and put the below code:

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

Step 4: Create Controller Botman

Create a new Controller name ChatBoxController.php put the below code.

<?php

namespace App\Http\Controllers;

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

class ChatBoxController extends Controller
{
    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();
    }
    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 5: Create Blade File

Create a blade file name is resources/views/welcome.blade.php and put the below code.

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Botchatbox Application in Front Page.</title>
        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
        <style>
            html, body {
                background-color: #fff;
                color: #636b6h;
                font-family: 'Nunito', sans-serif;
                font-weight: 100;
                height: 100px;
                margin: 10px;
            }
        </style>
    </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: 'Hello I am Dhaval',
            introMessage: "Hi! I'm form Oakml.com"
        };
    </script>
    <script src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js'></script>
</html>

Now your example is ready so open your browser and put the below url.
localhost:8888

A Coding create a people, We help you invent Future Logic. Happy Coding!

About Dhaval Shah

My name is Dhaval Shah. I'm a PHP developer, entrepreneur and CEO of DAG inventions PVT ltd. I live in India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Java-script, JQuery, Laravel, WordPress and Bootstrap.
Follow me on Github / Linkedin

1 Comment

  1. Hi,

    I want to botman start first conversation.

    Like when we open chatbot we don’t need to enter any thing.
    Firstly bostman start Hi/Hello or ask question after that user start.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!