Chatbots have been a revolutionary addition to online businesses. They are becoming very popular and growing rapidly with many companies having their own bots. There are two types of bots, and the first type is the virtual assistant while the second one is the messaging apps.

In the case of virtual assistants who are powered by machine learning, they can easily keep the information and then learn the patterns of the user.

In the case of messaging apps, they are only capable of having interaction with the users by simply following pre-defined and programmed rules.

Here, we provide a comprehensive and detailed guide to build a chatbot with Laravel as Laravel development has picked up the pace in developing chatbots.

How to Build a Chatbot with Laravel?

In order to create a bot like a telegram bot, you can register the bot by calling it a @BotChild account.

Now you need to click on the /new bot command in order to create a new bot. For instance, you can create “Xchange Details Bot” with the username “XchangeDetailsBot”

create-a-bot

Token Access

After you have created the bot successfully, you need to get token access. Now, save the token into a secret place

Backend System

When it comes to building a backend system, you chose a Laravel framework. Then you need an extra package to easily create the bot. So, you can choose BotMan. It is essentially a framework agnostic PHP library that is designed to easily simplify the task of developing different innovative bots for different messaging platforms. It also has multiple support like messenger, telegram, slack, hipchat, nexmo, wechat as well as Microsoft bot framework. The BotMan comes with its own boilerplate project in the Laravel. The first thing to commit is to create a project by running the command.

In order to make sure that botman is run, first, check the bot with a command.

Now type test and then you will view a response named hello! From the bot.

Now, you have built the bot successfully. The bot logic for the particular botman tinker can easily be found in the file named routes/botman.php. In order to make the bot even more useful, you need to find a few public APIs. There are different websites that have a wide range of public APIs which you can use. If you feel any difficulty doing that then you can hire dedicated developers, who have rich and proven experience in development.

backend-system

 

Steps to Build Chatbot in Laravel

First Step

In the first step, we will install the Laravel 5.8 using the command provided below. There are multiple Laravel versions in chatbot like Laravel 5, Laravel 6, laravel 7, and Laravel 8 applications.  

 

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

 

Second Step

Next, we should install the Botman and Botman Drive. Both have to be installed via running a command. Below are the commands which are required to install botman composer package and also install botman web driver.  

 

Install Botman

 

composer require botman/botman

 

Install Botman Drive

 

composer require botman/driver-web

 

Third Step 

Creating Configuration File, Although these files are not necessary. But creating a configuration file for both driver and cache can eliminate any situation of bugs. So below are the config files for Botman and Botman drive. 

 

config/botman/config.php

 

<?php

 return [

 

   ‘conversation_cache_time’ => 40,

    ‘user_cache_time’ => 30,

];

 

config/botman/web.php

 

<?php

 return [

 

   ‘matchingData’ => [

       ‘driver’ => ‘web’,

   ],

];

 

Fourth Step

Creating routes is the next step in enabling Chatbot in Laravel. This is done by creating a route for Botman request. This is done in the Bot PHP file. 

 

routes/web.php

 

Route::get(‘/’, function () {

   return view(‘welcome’);

});

  

Route::match([‘get’, ‘post’], ‘/botman’, ‘[email protected]’);

 

Fifth Step

Next step is to create the Bot Controller. This will enlist the replies, questions or suggestions the bot will use while communicating with anyone. 

app/Http/Controllers/BotManController.php

<?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);

       });

   }

}

 

Sixth Step

We now just have to update the Botfile and Bot codes on the welcome balde file.It is added in the welcome.blade.php 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 5? – devtechnosys.ae</title>

       <link href=”https://fonts.googleapis.com/css?family=Nunito:200,600″ rel=”stylesheet”>

       <style>

           html, body {

               background-color: #fff;

               color: #636b6f;

               font-family: ‘Nunito’, sans-serif;

               font-weight: 200;

               height: 100vh;

               margin: 0;

           }

       </style>

   </head>

   <body>

   </body>

    <link rel=”stylesheet” type=”text/css” href=”https://cdn.jsdelivr.net/npm/[email protected]/build/assets/css/chat.min.css”>

   <script>

    var botmanWidget = {

        aboutText: ‘ssdsd’,

        introMessage: “✋ Hi! I’m form Devtechnosys.ae”

    };

   </script>

    <script src=’https://cdn.jsdelivr.net/npm/[email protected]/build/js/widget.js’></script>

    

</html>

 

And now we are good to go with a chatbot in our Laravel.

Finalization

Now, setup the file named .env and add the telegram token which you have already created. Open the file named BotManController.php, and in order to create a particularly new pattern, add hears method and then add the pattern you wish, and to make the response of your own pattern, add reply method.

Whenever you type hello to the bot, you will receive the response mentioned in the reply method. If you type any other word, then you won’t view anything.

BotMan essentially comes with its own fallback method, which is called in case any of the “hears” patterns don’t match. You will certainly find these particular features quite useful to add to your user guide on how to actually use the bot.

Read the Blog- Which is the Best PHP Web Development Framework?

In order to make the bot, even more, feel and act like a human, you can make it the words sending “typing…” indicators by simply add types method before the given reply method.

Sometimes you may require to capture any parameter from the input for the users, then in botman, you can easily define the command parameter too.

In order to enable your bot with multiple features, you can add more APIs. In order to get data from the APO, you can handle the API with the Guzzle library which already exists in the Laravel project. Now you need to create a new method right within the BotManControllerphp named getDetails() with the parameter that users provide.

Call getDetails method before the reply method. Once you have successfully done this, you need to run the development server. After this install ngrok in order to make your own server Now run the ngrok server on the given port 8000 which is the default development port of Laravel.

After the ngrok run, now connect the bot with the telegram hook. You need to make sure that you particularly choose the ngrok https since telegram bot only runs in the https.

Now the hook is successfully set up. Now type “Give me {Currency } details”.

Conclusion

With the advent of the latest technologies in order to enable the users to easily communicate with businesses, Chatbots have certainly made things quite easier. You can easily do laravel app development in order to develop a chatbot using Laravel in case you require a robust chatbot.

With this article, we have explained how you can develop such a bot with Laravel. Hire Laravel developers who have complete knowledge of developing a chatbot with Laravel if your business requires a chatbot for its different purposes.