How to make Chatbot application with Laravel 6

Laravel Chatbot

A Chatbot application is now essential part of a website or application in these days. The user interacts either with the support personnel or a chatbot that automatically picks the answers for the queries.

This Chatbot application uses Jquery, Ajax and Laravel 6 api. You can copy the codes and easily make your own chatbot.

Let’s start with the migration for two tables ‘clients’ and ‘chat_actions’. The first table will use multi client chatbot system where you can make subscribers to your chatbot application.

Clients table where a client is basically a user with client id and client secret.

Schema::create('clients', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('client_id');
$table->string('client_secret');
$table->enum('status',['0','1'])->default('0')->comment('0=Inactive 1=Active');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});

Chat action table where questions and answers are predefined. Your chatbot will pick answers from this table.

Schema::create('chat_actions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('client_id');
$table->text('query')->nullable();
$table->text('response')->nullable();
$table->enum('status',['0','1'])->default('0')->comment('0=Inactive 1=Active');
$table->timestamps();
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
});

Routes and Controllers

Route::post('initialize-chatbox', 'ApiChatboxController@initialize');
Route::post('get-response', 'ApiChatboxController@get_response');


php artisan make:controller Api/ChatboxController

Now Ajax code with script check whether a division for chat box exists or not. It will retrieve the chatbot code and render it on your webpage. It requires jquery so include it too.

var client_id = 'ANUPAMCHATBOX';
var client_secret = 'SECRETANUPAM';
var action_url = '{{url('api/'.'initialize-chatbox') }}';
$(document).ready(function(){
var find = $(document).find('#a_chat_area');
if (find.length==0)
{
$.ajax({
url: action_url,
type: 'POST',
dataType: 'json',
data: {client_id: client_id , client_secret:client_secret},
})
.done(function(res) {
if (res.status==false)
{
console.log(res.response);
}
else
{
$(document).find('body').append(res.response);
}
})
.fail(function() {
console.log("Failed to connect with Chatbox servers");
})
}
});

Now here is your backend code for rendering chatbox and responding to queries.

public function initialize(Request $request)
{
$client_id = $request->client_id;
$client_secret = $request->client_secret;

$client = Client::where('client_id',$client_id)->where('client_secret',$client_secret)->where('status','1')->first();

if ($client)
{
$chatbox = view('chatbox.chatbox',['client_id'=>$client_id]);
return ['status'=>true,'response'=>$chatbox->render()];
}
else
{
return ['status'=>false,'response'=>'Client is not available in our system'];
}
}

public function get_response(Request $request)
{
$client_id = $request->client_id;
$query = $request->question;
$client = Client::where('client_id',$client_id)->first();

$find_chat = ChatAction::where('client_id',$client->id)->where('query',$query)->orWhere('query','like','%'.$query.'%')->first();

if ($find_chat!=null)
{
return ['status'=>true,'text'=>$find_chat->response];
}
else
{
return ['status'=>true,'text'=>'For any query please contact us on support@blogexpo.live'];
}
}

Here is your chatbox in action. For the chatbox code please write us on support@blogexpo.live .
Chatbot boxWorking Chatbot
Conclusion:
This was a short attempt to make a chatbox. More corrections and improvisations are welcome. For any query please write to us.

Er. Anupam Kumar
Latest posts by Er. Anupam Kumar (see all)
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments