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!
Quick Overview
In this guide, we’ll walk you through setting up a simple chatbot using Botman in a Laravel 10 project. You'll begin by creating a new Laravel project, then install the Botman package along with the Web driver to enable chatbot interactions on your website. After configuring caching and setting up the web driver, you’ll create routes and update the view to embed the chatbot interface. You’ll also generate a controller and set up methods to handle incoming messages, allowing the bot to respond to simple commands like “hi.” Finally, you'll test your chatbot by running the Laravel development server and interacting with the bot.
Step # 1 : Set Up a New Laravel Project.
Start by initializing a new Laravel project named botman. If you have Laravel installed globally, you can execute the following command.
laravel new botman
Alternatively, if you're using Composer, you can create the project by running.
composer create-project laravel/laravel --prefer-dist botman
Executing either command will generate a new Laravel project with all the default files and configurations necessary to kickstart your development.
Step # 2 : Set Up Your Project Environment.
First, open a terminal (like Git Bash) and navigate to the root folder of your newly created Laravel project.
cd c:xampp/htdocs/botman
Once you're in the project directory, install the necessary dependencies by running.
npm install && npm run dev
This will also start the Laravel Vite development server to compile the front-end assets. Open a new terminal window or tab (while keeping the Vite server running) to continue with any additional Laravel commands.
Step # 3 : Install the Botman Package.
To begin integrating Botman into your Laravel project, you need to install the core Botman package. This package provides the essential functionalities for building your chatbot. Run the following command in your terminal.
composer require botman/botman
This command will download and install the Botman package, making it available for use in your project.
Step # 4 : Install the Botman Web Driver.
Botman supports multiple messaging platforms through different drivers. In this step, we will install the Botman Web Driver, which enables the chatbot to interact with a web-based interface (like a simple website). Use the following command to install the driver.
composer require botman/driver-web
Once installed, this driver will allow you to handle chatbot interactions through web requests, enabling real-time communication between users and your chatbot on your site.
Step # 5 : Set Up Cache Configuration for Botman.
To optimize the performance of your chatbot, it's important to configure caching for both conversations and user data. Begin by creating a new folder called Botman within your config directory. Inside this folder, create a file named config.php and add the following configuration code.
<?php
return [
'conversation_cache_time' => 40,
'user_cache_time' => 30,
];
?>
In this configuration, conversation_cache_time determines the cache duration for conversation data (in minutes), while user_cache_time sets the cache time for user-specific data (in minutes), helping to improve the responsiveness of your chatbot by minimizing redundant data retrieval.
Step # 6 : Configure the Botman Web Driver.
Now, you'll configure the driver that Botman will use to handle web-based interactions. Inside the newly created Botman folder, create a file named web.php. In this file, add the following configuration.
<?php
return [
'matchingData' => [
'driver' => 'web',
],
];
This configuration tells Botman to use the web driver for handling incoming chatbot interactions from the web interface, enabling seamless communication with users on your site.
Step # 7 : Set Up the Route.
First, import the BotManController class.
use App\Http\Controllers\BotManController;
Next, create the route that will listen for both GET and POST requests at the /botman URL, directing them to the handle method in the BotManController.
Route::match(['get', 'post'], '/botman', 'App\Http\Controllers\BotManController@handle');
Step # 8 : Update the View.
To set up the BotMan chatbot interface, replace the contents of your welcome.blade.php file with the following HTML.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel 10 - Botman Chatbot</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 ALL ABOUT LARAVEL"
};
</script>
<script src="https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js"></script>
</html>
This setup will embed the BotMan chatbot interface on your page, ready to interact with users.
Step # 9 : Generate and Set Up the Controller.
To get started, generate the BotManController using the following command.
php artisan make:controller BotManController
Once the controller is created, open BotManController.php and update it 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);
});
});
}
}
The handle() method listens for incoming messages from the user. When the user sends hi, it triggers the askName() method to initiate a conversation, prompting the user for their name. If the user sends any other message, the bot replies with a prompt to say hi. The askName() method then asks the user for their name and responds with a personalized greeting. This setup establishes basic chatbot functionality where the user can start a conversation by saying hi, and the bot will follow up by asking for their name.
Step # 10 : Test BotMan.
Now, let's test the chatbot functionality. Begin by starting the Laravel development server with the following command.
php artisan serve
Once the server is running, open your browser and go to the URL: 127.0.0.1:8000.When the user says hi or Hi, the bot should respond accordingly.
If the user sends any other message, the bot will prompt them to say hi.
Conclusion
By following this guide, you’ve successfully set up a simple chatbot in your Laravel 10 project using Botman. Your chatbot is now fully functional, capable of handling user interactions like greeting commands and collecting basic information. With the chatbot in place, you can focus on expanding its capabilities, such as adding more complex conversations, integrating with external services, or customizing its responses. As your project evolves, consider exploring more advanced Botman features and other Laravel tools to further enhance your application.
For more details, refer to the official Laravel and Botman documentation.
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