You may have noticed some chats in various applications that respond to your messages right away. It responds to you right away. Actually, chatbots are in charge of this. Chatbots can be used to offer 24/7 customer care to clients.
This article demonstrates the integration of the Botman chatbot with Laravel 9. Botman is a package for composers. It will automatically give the chat interface and its internal functionality after installation.

Botman and the botman-driver composer package must be installed.

Here, we will see a step-by-step tutorial for using Laravel 9 and the Botman chatbot.

Table of Contents

      1. Laravel Installation
      2. Install Botman and Botman Driver
      3. Create Configuration File (Optional)
      4. Create Controller
      5. Add Route
      6. Create Blade File
      7. Application Testing

Laravel Installation

Using composer, we will construct a Laravel project. Therefore, please verify that composer is installed on your machine. If not, perhaps this guide can assist you in installing composer on your computer.

Here is the command to create a laravel project-

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

Laravel’s development server can be launched by:

php artisan serve

URL: http://127.0.0.1:8000

assuming your system already has Laravel installed.

Install Botman and Botman Driver

The next step is to integrate botman into your Laravel setup so you can use the chatbot’s features.

Run this command after opening a project in a terminal.

composer require botman/botman

Run this command to install the botman driver after installing this package.

composer require botman/driver-web

Create Configuration File (Optional)

These actions are not required. These driver and cache configuration files.

Let’s make a botman folder in the /config directory. Make two files in the /config/botman directory.

      • 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

Run this artisan command to generate a controller class file while the project is open in a terminal.

php artisan make:controller BotManController

A file called BotManController.php will be created in the /app/Http/Controllers folder.

Write the entire code into BotManController.php after opening 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 the /routes folder and find web.php. Then incorporate this route.

use App\Http\Controllers\BotManController;

//...

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

Create Blade File

Any view file from your application can be opened. I’m launching the welcome.blade.php default view file.

Add this straightforward code to it. This code will produce a Chatbox user interface.

//...

<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 Code Solution Stuff"
  };
</script>

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

Application Testing

Enter the command to launch the development server in the project’s terminal window.

php artisan serve
http://127.0.0.1:8000/

We hope that this post has given you a thorough understanding of how the Botman Chatbot is integrated into the Laravel 9 tutorial.

 


Recommended Posts

View All

Top Features You Must Learn to Master in MongoDB


MongoDB is a NoSQL database with a tonne of amazing features. These incredible qualities give this technology a distinctive and alluring look. These c...

How MongoDB Projection Improves Performance


MongoDB Projection Tutorial, How MongoDB Projection affects Query Performance, Example of Projection in MongoDB,Run Query With/without Projection

CRUD Operations in MongoDB


The conventions of a user interface that allow users to view, search, and alter portions of the database are referred to as CRUD operations MongoDB.

Learn What is MongoDB with Application and Features


MongoDB is a C++-based open source platform with a fairly simple setup process. It is a non-structured, document-oriented, cross-platform database. Hi...

MongoDB Tutorial for Beginners


MongoDB is a growing technology that is grabbing the attention of developers. Data is stored as documents in MongoDB, an open source document-oriented...