Build a Telegram group bot in PHP
#chatbotsWorking with Facebook Messenger bots is great, but there is one thing still missing: group bots! This is why we will build a little Telegram bot today and use it inside the BurgerCrew group.
Preface
The first thing to keep in mind here is that a group chatbot can be very different from a 1:1 conversation chatbot. In groups, chatbots are often not a direct conversation partner but more a quiet assistant. They do not need to reply to every message. In fact, that would be very annoying :-) 😒 But they can help the group with certain tasks.
With my friends, we have a BurgerCrew
group on Telegram. This is where we chat all day about our favorite topic Burgers
. 🍔 Once a month we meet to grab one of them and drink some beers. One question we ask a lot is, how long it is until the next event. And this is the one thing our bot will do today. A Telegram command will trigger the bot and he will tell us how many days are left.
Telegram setup
We start by creating a Telegram bot. Follow the instructions here and copy the bot token, we will need that later. It is quite funny to create a bot with a bot, isn't it? :-)
Now that the bot is created you can find it through the Telegram contact search and start a conversation with it. Of course, it cannot reply yet, but we will change that soon.
Next, we add a command to our bot, which will trigger the message inside the Telegram group. They are like buttons everyone in the group can use to trigger a certain bot task. Creating a command can be achieved via the BotFather
bot. Just use the /setcommands
command inside your BotFather chat on Telegram.
BotMan setup
I am a big fan of the BotMan library and we will use it again today. Since I am working a lot with Laravel I will use BotMan Studio, which makes the setup super easy. It's a Laravel application with BotMan already built-in.
The easiest way to install BotMan Studio is via the installer. (Skip this, if you got it already installed)
composer global require "botman/installer"
After that, you can install a new instance and change directory.
botman new telegram-group-bot && cd telegram-group-bot
Inside the new directory, we then need to install the Telegram driver. Luckily there is a nice command for that as well.
php artisan botman:install-driver telegram
Last thing here is to add your Telegram bot token to your .env
file.
TELEGRAM_TOKEN=YOUR_TELEGRAM_BOT_TOKEN
Bring them together
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 the hood too.
To add this public URL to our bot we need to register
. There is another artisan command for that. Enter php artisan botman:telegram:register
and when asked, enter your public URL + "/botman". You should see a success message then.
To make sure everything is working out, type Hello
in your chat with your created bot. It should respond with Hi there :)
. This works because the BotMan Studio comes with a simple example which you can find in routes/botman.php
.
$botman->hears('Hello', function (BotMan $bot) {
$bot->reply('Hi there :)');
});
Finally some code
Now that we got everything set up, we are ready to create the logic behind our /countdown
Telegram command. Let's change the default example to listen to our command.
$botman->hears('/countdown', function ($bot) {
$firstName = $bot->getUser()->getFirstName();
$bot->reply('Hey ' . $firstName . ', next BurgerCrew event will start in 10 days.');
});
I am also grabbing the users' first name, to provide a more personal reply. In order to see all listed commands, you just need to type a slash.
And now try our command.
It should return our string and tell us the left days. Of course, this is not dynamic right now, but this is not what this article was about. The focus was to set up the Telegram bot.
Now that the bot is working we just need to add it to a group, like we would with a normal user. Then the slash command will be available there for everyone.
Conclusion
I hope I could show you today that it is really easy to start a Telegram group bot. With already one command it can be helpful to everyone in this group. Below you will see how I use this bot inside my BurgerCrew group. (German text) I also implemented a joke command that will tell you a Chuck Norris joke ;-) 🤠
But this is just one use-case. It is totally up to you how to use the bot inside a group. Be creative!