Laravel 10 - BotMan Chatbot
Laravel 10 - BotMan Chatbot
In this tutorial, we will integrate Botman Chatbot into Laravel 10. This can be useful for automating user interactions, providing support, or gathering information efficiently.
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
Next, install the required dependencies and run the Laravel Vite development server for front-end assets:
Command : npm install && npm run dev
In a new terminal window or tab (while keeping the Vite server running), navigate to the same project directory to execute further Laravel command.
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;
Create 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 10 - 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 and email.
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.
0 Comments