How to Integrate Botman Chatbot Widget in Laravel 10 App

Last Updated on by in Laravel

In this tutorial, we will learn how to add or implement and use the Botman chatbot widget into Laravel application.

Chatbots are becoming increasingly prevalent for businesses as a more profound way to communicate with customers.

BotMan is a chatbot framework for Laravel that enables developers to add and configure chatbots for websites and applications quickly.

Laravel Botman is an open-source chatbot framework that allows web developers to create and deploy custom chatbots on their websites quickly.

With Laravel Botman, businesses can give automated customer service, gather user data, not only but also offer complex conversations with users.

In this post, we will not only discuss the benefits of using Laravel Botman, but we will also show you how to create a chatbot in Laravel using the Botman chatbot package and driver.

How to Add Chat Widget in Laravel 10 using Botman Chatbot

  • Create Laravel App
  • Connecting to Database
  • Add Botman Package with Botman Driver
  • Create Controller File
  • Create New Routes
  • Build View Template
  • Run App on Browser

Create Laravel App

Installing a Laravel application is an easy process. It demands basic knowledge of the command line and can be done in just a few steps.

You must execute the given command to install a Laravel application on your local machine.

composer create-project laravel/laravel --prefer-dist laravel-demo
cd laravel-demo

Connecting to Database

Connecting to a database is an essential part of any modern software development.

It allows developers to store and retrieve data from the database in order to create applications that are more efficient and reliable.

Hence, insert the given details in .env file.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=

For macOS MAMP PHP server; make sure to additionally include UNIX_SOCKET and DB_SOCKET right below the database details in .env file.

UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

Add Botman Package with Botman Driver

Installing the Botman package with the Botman Driver is an easy and straightforward process. With this package, developers can easily create powerful chatbots that are capable of responding to user input in a variety of ways.

Here are the commands that you have to run.

composer require botman/botman
composer require botman/driver-web

Create Controller File

Laravel controllers are a key component of the Laravel framework and provide the necessary logic to handle requests from the client.

We can conjugate related logic within a single class and can be extended to add custom functionality.

Generate new controller file.

php artisan make:controller BotManChatController

Open the app/Http/Controllers/BotManChatController.php and put the suggested code within the file:

<?php

namespace App\Http\Controllers;

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


class BotManChatController extends Controller
{

    public function invoke()
    {
        $botman = app('botman');
        $botman->hears('{message}', function($botman, $message) {
            if ($message == 'hello') {
                $this->askName($botman);
            }else{
                $botman->reply("Type 'hello' for demo ...");
            }
        });
        $botman->listen();
    }
   
    public function askInfo($botman)
    {
        $botman->ask('Hey There! How are you?', function(Answer $answer) {
            $ans = $answer->getText();
            $this->say('Whats up '.$ans);
        });
    }
}

Create New Routes

Laravel routes are the key to making applications in the Laravel framework function correctly.

They define how requests from a user’s browser are routed to the appropriate controller and action within the application.

We need to add the given code to the routes/web.php for making new custom URLs.

<?php

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

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/dashboard', function () {
    return view('home');
});

Route::match(['get', 'post'], '/botman-chat', 'BotManChatController@invoke');

Build View Template

Laravel view templates provide developers with an efficient way to create and maintain dynamic web applications.

They enable developers to quickly and easily create a powerful, feature-rich website with minimal effort. With Laravel view templates, developers can easily customize the look and feel of their websites, as well as add additional functionality.

In this step, we are going to create a new view template; you have to create the home.blade.php file inside the resources/views/ directory.

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Laravel Install Botman Chatbot Example</title>
    </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 src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js'></script>

    <script>
        var botmanWidget = {
            aboutText: 'Botman',
            introMessage: "Hello! How may we help you?"
        };
    </script>
   
</html>

Run App on Browser

To run a laravel server on our local machine, we must ultimately type and execute the given command.

php artisan serve

To get the profound immersive experience make sure to use the given url to access your favorite app on the browser.

http://127.0.0.1:8000/dashboard

How to Integrate Botman Chatbot Widget in Laravel App

Conclusion

In this guide, we have learned how to add a BotMan chatbot to the laravel application.

A botman chatbot is an incredible tool that allows Laravel developers to efficiently build bots capable of responding to user queries in real-time.