Laravel 11 - BotMan Chatbot Integration
Laravel 11 - BotMan Chatbot Integration
In this tutorial, we’ll explore integrating BotMan Chatbot into Laravel 11, enhancing user engagement through automated conversations and providing seamless customer interactions.
If you're a video person, feel free to skip the post and check out the video instead!
Step # 1 : Create fresh Laravel project or use existing project.
Two commands to create fresh laravel project.
Global Command : laravel new botman
Or use
Non Global Command : composer create-project laravel/laravel --prefer-dist botman
Step # 2 : Access the project.
Open a terminal (e.g., Git Bash) and navigate to your Laravel project's root folder.
Git Bash : cd c:xampp/htdocs/botman
Step # 3 : Install Botman package.
Command : composer require botman/botman
Step # 4 : Install Botman driver.
Command : composer require botman/driver-web
Step # 5 : Configure cache for Botman.
Create a new folder inside the config directory named Botman, then create a new file named config.php and paste the following code.
<?php
return [
'conversation_cache_time' => 40,
'user_cache_time' => 30,
];
?>
Step # 6 : Configure driver for Botman.
Create a new file inside the Botman directory named web.php and paste the following code
<?php
return [
'matchingData' => [
'driver' => 'web',
],
];
Step # 7 : Create a route.
Import BotManController class.
use App\Http\Controllers\BotManController;
Crate route.
Route::match(['get', 'post'], '/botman', 'App\Http\Controllers\BotManController@handle');
This route will listen for both GET and POST requests at the /botman URL and pass them to the handle method in the BotManController
Step # 8 : Update View.
Replace the content of the welcome.blade.php file with the following HTML to set up the Botman chatbot interface.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel 11 - BotMan Chatbot - Code Shotcut</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: 'Start the conversation with Hi',
introMessage: "WELCOME TO CODE SHOTCUT"
};
</script>
<script src="https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js"></script>
</html>
Step # 9 : Create a controller.
Run the following command to generate the BotManController.
Command : php artisan make:controller BotManController
Once the controller is created, replace the content of BotManController.php with the following code.
<?php
namespace App\Http\Controllers;
use BotMan\BotMan\BotMan;
use Illuminate\Http\Request;
use BotMan\BotMan\Messages\Incoming\Answer;
class BotManController extends Controller
{
/**
* Handle the incoming messages from the Botman chatbot.
*/
public function handle()
{
$botman = app('botman');
// Listen for any message
$botman->hears('{message}', function($botman, $message) {
// Convert the message to lowercase to handle case insensitivity
$message = strtolower($message);
// If the user says 'hi', start a conversation to ask for their name
if ($message == 'hi') {
$this->askName($botman);
}
// For any other input, send a default message
else {
$botman->reply("Start a conversation by saying hi.");
}
});
$botman->listen();
}
/**
* Ask the user for their name when they say 'hi'.
*/
public function askName($botman)
{
// For fewer questions, you can use the inline conversation approach as shown below. Alternatively, use a dedicated conversation class for multi-step conversations
$botman->ask('Hello! What is your name?', function(Answer $answer, $conversation) {
// Capture the user's answer
$name = $answer->getText();
// Respond with a personalized message
$this->say('Nice to meet you, ' . $name);
//Continue inline conversation.
$conversation->ask('Can you advise about your email address.', function(Answer $answer, $conversation){
$email = $answer->getText();
$this->say('Email : '.$email);
});
});
}
}
Explanation :
handle(): This method listens for incoming messages. If the user sends 'hi', it triggers the askName() method to start a conversation. If the user sends anything else, it replies with a prompt to say 'hi'.
askName(): This method asks the user for their name and then responds with a personalized greeting.
This sets up basic chatbot functionality where the user can start a conversation by saying 'hi', and the bot will ask for their name.
Step # 10 : It's time to test.
Start the Laravel development server.
Command : php artisan serve.
Access below URL
127.0.0.1:8000
If a users says hi or Hi.
if a user says anything else.
Share this with friends!
To engage in commentary, kindly proceed by logging in or registering
Subscribe to Our Newsletter
Stay ahead of the curve! Join our newsletter to see what everyone’s talking about.
2 Comments
Great tutorial, thank you. I followed your instructions, I am getting error 404 page not found. Any advice?.
Hi Mohammed,
The 404 error indicates that Laravel is unable to locate the route you set up or that there might be a misconfiguration. I recommend reviewing each step again, and please let me know if anything is unclear. If you’re still unable to resolve the issue, feel free to email me at codeshotcut@gmail.com, and I’ll be happy to assist you further.