BotMan 2.0 PHP Chatbot Framework
Published on by Paul Redmond
BotMan is a framework agnostic PHP Chatbot framework designed to simplify the task of developing innovative bots for multiple messaging platforms, including Slack, Telegram, Microsoft Bot Framework, Nexmo, HipChat, Facebook Messenger, WeChat and many more.
BotMan 2.0 was released earlier this week with a bunch of improvements and exciting changes. From a project and code management perspective a couple of things happened:
- BotMan and the various repositories around BotMan moved to the BotMan GitHub Organization instead of
mpociot/botman
. - Each Driver (Slack, Telegram, etc.) is contained in a separate repository outside of the BotMan core code
- BotMan Studio bundles BotMan with the Laravel PHP Framework
Using the BotMan Chatbot Framework
On a basic level, a chat bot includes functionality to listen for messages, and respond to those messages. For example, if you ask your bot the weather, they might look up the weather from an API and then give you back the forecast.
Another way you can use BotMan is to listen for events. For example, you could greet a user when they join a channel or leave one.
From the Documentation, here’s how you can use BotMan from a web-accessible route in your application:
<?php use BotMan\BotMan\BotMan;use BotMan\BotMan\BotManFactory; $config = [ // Your driver-specific configuration]; // create an instance$botman = BotManFactory::create($config); // give the bot something to listen for.$botman->hears('hello', function (BotMan $bot) { $bot->reply('Hello yourself.');}); // start listening$botman->listen();
The above code is framework-agnostic, but the fluent API makes it simple to write a chatbot.
Advanced Topics
BotMan also supports advanced features, such as a middleware system, natural language processing (NLP), retrieving user information, and storage.
The middleware system has been expanded more in the 2.0 release, which allows you to hook into different parts of the ChatBot lifecycle. You could do powerful things like keep track of stats related to answered chats, and provide NLP on incoming messages. The available entry points for middleware are: sending
, received
, and heard
.
Using the middleware, BotMan provides built-in support for the api.ai NLP service. Middleware makes your bots smarter and able to process more than just static text. A good example where this feature could be useful is the /remind
feature, where you can use natural language to be reminded of something at a later time and date.
BotMan Studio
BotMan Studio—a packaged BotMan and Laravel application—provides testing tools, an out of the box web driver implementation, and additional tools like easier driver installation and configuration support.
BotMan studio can speed up development by providing a web driver implementation, which allows you to develop your chatbot locally and interact with it through a simple Vue.js chat widget, which lets you communicate with your bot without deploying it.
You can install drivers more easily with the artisan commands provided by BotMan studio:
# List available drivers$ php artisan botman:list-drivers # Install facebook$ php artisan botman:install-driver facebook
You can create new BotMan projects with the BotMan installer in a similar way that you create new Laravel projects:
$ botman new weatherbot
Testing in BotMan Studio
The testing helpers in BotMan studio are slick! Here’s a simple example of the helpers provided:
/* @test */$this->bot ->receives('Hi') ->assertReply('Hello!');
If you’ve used Laravel, you are familiar with the lovely testing helpers provided that make testing easier. Here’s a more complex conversation test example:
$this->bot ->receives('Hi') ->assertReplies([ 'Hello!', 'Nice to meet you. What is your name?', ])->receives('BotMan') ->assertReply('BotMan, that is a beautifule name :-)');
The concept of how to test a chat bot feel foreign to me, but I am impressed with how elegant and straightforward the testing helpers make testing a chat bot!
New Features in 2.0
From the project’s changelog, here’s the gist of what’s new in BotMan 2.0:
- Added ability to originate inline conversations.
- Moved each driver into their own repository.
- Facebook – Added support to send file and audio attachments.
- Telegram – Added support to send file, audio and location attachments.
- Added Kik driver.
- Added custom Attachment classes.
- Added support to listen for message service events.
- Changed the way middleware works in BotMan.
- Added support for Slack interactive menu messages.
- Added Facebook Referral driver.
- Allow replying to an existing thread for Slack drivers (#327).
- Added loadDriver method to BotMan.
- Added ability to use BotMan with a local socket.
Learn More
I am getting excited to write my chat bot! You can learn more by checking out the BotMan documentation and following the botman/botman GitHub project. You can also follow @botman_io and the creator @marcelpociot on Twitter.
Building a Chatbot with Laravel and BotMan
Learn how to build a chatbot from scratch using the framework you already know. We now have an ebook to walk you through building your own with Laravel and Botman.