Laravel 8 Botman Chatbot Tutorial Example

Laravel 8 Botman Chatbot Tutorial Example

Laravel 8 botman chatbot example. In this tutorial, you will learn how to install  botman chatbot in laravel 8 app.

If you don’t know how to add laravel chatbot then don’t worry, you are a right place. So, this tutorial will help you step by step in this laravel chatbot tutorial.

How to Install Botman Chatbot in Laravel 8?

  • 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, open your terminal and execute the following command to install or download or install laravel 8 app:

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

Step 2 – Connecting App to database

After that, open “.env” file and update the database name, username and password in the env file:

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, open terminal and execute the following command on it to install botman composer package:

Install Botman:

composer require botman/botman

Install Botman Driver:

composer require botman/driver-web

Step 4 – 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',
    ],
];

Now run the following command

php artisan migrate

Step 5 – Add route

Next, open your “routes/web.php” file and add the following route:

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

Step 6 – Create Controller by Artisan Command

In this step, open your terminal execute the following command to create PostController.php file:

php artisan make:controller BotManController

This command will create PostController by the artisan command.

Next, go to app/http/controller/BotManController.php. and update the following methods into your controller file:

<?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, Visit resources/views directory and open file that named welcom.blade.php.

After that, update the following html and javascript code into this blade view file:

 <!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? - Tutsmake.org</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 Tutsmake.org"
        };
    </script>
  
    <script src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js'></script>
      
</html> 

Step 8 – Run Development Server

In this step, Execute the php artisan serve command on terminal to start server locally:

php artisan serve

Then open your browser and hit the following url on it:

Conclusion

In this tutorial, you have learned how to install botman chatbot in laravel 8 app.

Recommended Laravel Posts

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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