Build a Telegram chatbot in Laravel with BotMan Studio 2.0
Before we start, make sure to have these things prepared:
Install BotMan Studio
The easiest way to install BotMan Studio is via the installer.
After that you can just install a new instance like that:
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.
When you click
Tinker you can immediately test your chatbot. Type
Hi and you should get a reply. This works because this behaviour comes with BotMan Studio. You will find the code for that in your
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.
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. In order to install the Telegram driver we can use another artisan command.
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
In order 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
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 in order 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 hood too.
To add this public URL to our bot we need to do 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.
botman endpoint of your BotMan application is already setup in BotMan Studio. This is why that works out of the box. You will find the code for that in you
Test it out
Next we can test if everything is working like 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
Additionally we can test the example conversation, which is built into BotMan Studio.
First custom message
And in order to write some chatbot functionality ourselves, we add a custom listener to the
You will see that this works like a charm as well.
Although it seems quite easy 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 setup your next Telegram bots. From here you are ready to build more and more features to your bot your own. So make sure to checkout the BotMan documentation to get a feeling of what is possible and learn new stuff.