BotMan 2.0 PHP Chatbot Framework

Published on by

BotMan 2.0 PHP Chatbot Framework image

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.

![](https://laravelnews.s3.amazonaws.com/images/book.png)

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.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project.

Visit No Compromises
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
All Green logo

All Green

All Green is a SaaS test runner that can execute your whole Laravel test suite in mere seconds so that you don't get blocked – you get feedback almost instantly and you can deploy to production very quickly.

All Green
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4 image

Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4

Read article
Basset is an alternative way to load CSS & JS assets image

Basset is an alternative way to load CSS & JS assets

Read article
Integrate Laravel with Stripe Connect Using This Package image

Integrate Laravel with Stripe Connect Using This Package

Read article
The Random package generates cryptographically secure random values image

The Random package generates cryptographically secure random values

Read article
Automatic Blade Formatting on Save in PhpStorm image

Automatic Blade Formatting on Save in PhpStorm

Read article
PhpStorm 2024.1 Is Released With a Integrated Terminal, Local AI Code Completion, and More image

PhpStorm 2024.1 Is Released With a Integrated Terminal, Local AI Code Completion, and More

Read article