Laravel 8 Botman Chatbot in this tutorial, I will show you how we can setup Botman Chatbot in your Laravel application.
Also, I will show you how to Install Botman and Botman Driver in laravel 8 app.
First of all, you need to follow this step.
Step 1: Install laravel 8 App
Step 2: Database Configuration in .env file
Step 3: Install Botman and Botman Driver
Step 4: Create Configuration File
Step 5: Create Controller
Step 6: Add Route
Step 7 – Create Blade File
Step 8 – Run Development Server
Step 1: Install laravel 8 App
We need to run command to create Laravel 8 projects.
composer create-project --prefer-dist laravel/laravel botman
cd botman
Step 2 : Connecting App to Database
Next step, we will set the database credentials in the application. Let’s open your project .env file and set the database credentials here.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=botman
DB_USERNAME=root
DB_PASSWORD=root@123
Step 3: Install Botman and Botman Driver
composer require botman/botman
Install Botman Driver:
composer require botman/driver-web
Step 4: Create Configuration File
This step is not required to follow. But you can create configuration file for driver and cache. so let’s create bot file on config folder and write code like as i gave you bellow:
Here, I am going to create configuration file for driver and cache. so let’s follow create bot file on config folder and write code like as bellow I have written.
config/botman/config.php
<?php
return [
'conversation_cache_time' => 40,
'user_cache_time' => 30,
];
?>
config/botman/web.php
<?php
return [
'matchingData' => [
'driver' => 'web',
],
];
Step 5: Create Controller
Now this step, I am going to create controller as BotManController. in this controller I will write code of botman reply and conversation.
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);
});
}
}
Step 6: Add Route
In this step, I will create routes for botman request. so open your “routes/web.php” file for write web services route.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BotManController;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
Route::match(['get', 'post'], '/botman', [BotManController::class, 'handle']);
Step 7 – Create Blade File
Just replace exiting code with bellow code
resources/views/welcome.blade.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Botman Chatbot in Laravel - Real Programmer</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: 'Write Something',
introMessage: "✋ Hi! I'm form Real Programmer"
};
</script>
<script src='https://cdn.jsdelivr.net/npm/botman-web-widget@0/build/js/widget.js'></script>
</html>
Now, Run Server
php artisan serve
Now check Routes in below.
http://127.0.0.1:8000/