Creating a Natural Language Processing Chatbot in Laravel with BotMan and Dialogflow

Chimeremze Prevail Ejimadu
3 min readFeb 21, 2023

--

Photo by DeepMind on Unsplash

Chatbots have become an increasingly popular tool for businesses looking to improve customer engagement and automate routine tasks. Laravel developers can take advantage of the BotMan framework, a PHP chatbot building platform, to create customized chatbots that interact with users through popular messaging channels such as Facebook Messenger, Slack, and Telegram.

In this post, we’ll walk through the steps involved in building a chatbot with Laravel and BotMan. We’ll provide code samples along the way to make it easy to follow.

Setting up a BotMan Chatbot Instance in Laravel

The first step in building a chatbot with Laravel and BotMan is to set up a BotMan chatbot instance in your Laravel application. To do this, we need to install BotMan using Composer:

composer require botman/botman

Next, we’ll create a new BotMan instance in our Laravel app by adding the following code to our routes/web.php file:

use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Middleware\Dialogflow;

$botman = BotManFactory::create();

$dialogflow = new Dialogflow('YOUR_CLIENT_ACCESS_TOKEN_HERE');
$botman->middleware->received($dialogflow);

$botman->hears('Hello', function (BotMan $bot) {
$bot->reply('Hello there!');
});

$botman->listen();

This code creates a new BotMan instance and sets up a middleware that connects to the Dialogflow natural language processing (NLP) platform. The hears method specifies how the chatbot will respond when it receives a message containing the word "Hello".

Creating Custom Conversations and Actions

Now that we have a basic chatbot set up, we can start creating custom conversations and actions to handle specific user inputs and requests. For example, we could create a conversation that helps users place an order for a pizza.

To do this, we can create a new class that extends the BotMan\BotMan\Messages\Conversations\Conversation class, and add the following methods:

public function askFlavor()
{
$this->ask('What flavor of pizza would you like?', function (Answer $answer) {
$this->bot->userStorage()->save(['flavor' => $answer->getText()]);
$this->askSize();
});
}

public function askSize()
{
$this->ask('What size would you like?', function (Answer $answer) {
$this->bot->userStorage()->save(['size' => $answer->getText()]);
$this->confirmOrder();
});
}

public function confirmOrder()
{
$user = $this->bot->userStorage()->find();
$this->say(sprintf('Thanks, your order for a %s %s pizza has been placed!', $user->get('size'), $user->get('flavor')));
}

This code creates a new conversation that prompts the user to select a pizza flavor, size, and confirm the order. The ask method prompts the user for a response, while the say method sends a message back to the user.

To initiate this conversation from our main BotMan instance, we can add the following code:

$botman->hears('order', function (BotMan $bot) {
$bot->startConversation(new OrderPizzaConversation());
});

This code listens for a message containing the word “order” and initiates the OrderPizzaConversation conversation.

Conclusion:

In this post, we’ve seen how Laravel developers can leverage the BotMan framework to build customized chatbots that interact with users through popular messaging platforms. With BotMan, it’s easy to create conversations that respond to user inputs and carry out specific actions, such as placing an order for a pizza.

BotMan also provides access to popular NLP platforms like Dialogflow, making it possible to create chatbots that can understand and respond to natural language inputs. This opens up a whole range of possibilities for businesses looking to improve customer engagement and automate routine tasks.

Overall, if you’re looking to build a chatbot with Laravel, BotMan is a great option to consider. Its ease of use and flexibility make it a powerful tool for creating chatbots that can help drive business growth and improve customer satisfaction.

Stay tuned!!! I will be back with some more cool Laravel tutorials in the next article. I hope you liked the article. Don’t forget to follow me 😇 and give some clap 👏. And if you have any questions feel free to comment.

Thank you.

--

--

Chimeremze Prevail Ejimadu

Laravel Developer + Writer + Entrepreneur + Open source contributor + Founder + Open for projects & collaborations. Hit FOLLOW ⤵