Interested how Laravel works under the hood? → Check my Laravel Core Adventures Course

Build a Telegram chatbot in Laravel with BotMan Studio 2.0

#chatbots

With BotMan it is easy to set up a Telegram chatbot. This week the new 2.0 version was released. We will check out how to set up a Telegram chatbot in this new version with BotMan Studio step by step.

Preparations

Before we start, make sure to have these things prepared:

Install BotMan Studio

The easiest way to install BotMan Studio is via the installer.

composer global require "botman/installer"

After that you can just install a new instance like that:

botman new botman-telegram

It is basically like the Laravel Installer. Your application is now already installed. When you use Laravel Valet you can directly check the homepage, botman-telegram.dev in my case. Here you will see the BotMan Studio welcome page.

Screenshot of the BotMan welcome page

When you click Tinker, you can immediately test your chatbot. Type Hi and you should get a reply. This works because this behavior comes with BotMan Studio. You will find the code for that in your routes/botman.php file.

$botman->hears('Hi', function ($bot) {
    $bot->reply('Hello!');
});
Screenshot showing BotMan Tinker

Configure BotMan Studio

Now that BotMan is installed we need to configure it to work with Telegram. When you use the BotMan artisan command php artisan botman:list-drivers you will see the installed driver.

Screenshot showing terminal output for BotMan list driver command

As you can see, by default only the web driver is installed. Since version 2.0 almost all drivers live in separate GitHub repositories, and you need to install the ones you need. To install the Telegram driver, we can use another artisan command.

php artisan botman:install-driver telegram

Next, to the driver, this will also add a config/botman/telegram.php config file. There you'll see that BotMan requires some data from your .env file.

   /*
   |--------------------------------------------------------------------------
   | Telegram Token
   |--------------------------------------------------------------------------
   |
   | Your Telegram bot token you received after creating
   | the chatbot through Telegram.
   |
   */
   'token' => env('TELEGRAM_TOKEN'), 

To connect our BotMan application to a Telegram bot, we need the Telegram token. You will receive it when you create a new bot on Telegram. Place it then in your .env file.

TELEGRAM_TOKEN=YOUR_TELEGRAM_BOT_TOKEN

Connect BotMan to your Telegram bot

Next, we will connect the bot to our application. Since I am using Laravel Valet for my local development, I can just type valet share to get a public URL for my application. Make sure to use the https one. If you are not using Valet, you can install ngrok which Valet uses under the hood too.

To add this public URL to our bot, we need to make a HTTP POST request to https://api.telegram.org/botYOUR-BOT-TOKEN/setWebhook and provide one field url which holds our public URL. Make sure to replace YOUR-BOT-TOKEN with your token. 👈

I am using the REST client Insomnia for that, but you can do that with CURL as well.

How to setup the Telegram bot webhook

The botman endpoint of your BotMan application is already set up in BotMan Studio. This is why that works out of the box. You will find the code for that in you routes/web.php file.

Route::match(['get', 'post'], '/botman', 'BotManController@handle'); 

Test it out

Next, we can test if everything is working as planned. So visit your Telegram bot, which you can find like any other users, and send a message with just Hi. You should again get a reply with Hello!.

Screenshot showing a message and a reply in Telegrram.

Additionally, we can test the example conversation, which is built into BotMan Studio.

Screenshot showing how to test the BotMan example conversation

First custom message

And to write some chatbot functionality ourselves, we add a custom listener to the routes/botman.php file.

$botman->hears('It just works', function(BotMan $botMan) {
   $botMan->reply('Yep 🤘');
}); 

You will see that this works like a charm as well.

Screenshot showing first custom bot message

Conclusion

Although it seems quite natural to setup BotMan Studio and Telegram you still need to be aware of a few things. I hope I could provide them here and this article helps you to set up your next Telegram bots. From here you are ready to build more and more features to your bot your own. So make sure to check out the BotMan documentation to get a feeling of what is possible and learn new stuff.

Do you enjoy my posts?

Sign up for my newsletter to receive updates on my latest content.

You will receive monthly updates on my latest articles and products. I do care about the protection of your data. Read my Privacy Policy.