Build a Telegram group bot in PHP
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
FantasticBurger group on Telegram. This is where we chat all day about our favourite 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 till 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.
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.
Let’s start by getting the BotMan Laravel Starter:
Then add the bot token, you got while creating the Telegram bot, to your
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 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.
To make sure everything is working out, type
Hello in your chat with your created bot. It should response with
Hi there :). This works because the BotMan Laravel Starter comes with a simple example:
Finally some code
Now that we got everything setup, we are ready to create the logic behind our
/countdown Telegram command. Let’s change the default example to listen to our command.
I am also grabbing the users’s 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 setup 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.
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 FantasticBurger group. (German text) I changed his name to
Mr. Burger and also implemented a joke command that will tell you a Chuck Norris joke ;-) 🤠
Next time we will enhance this bot to reply automatically without the slash commands. In the meanwhile have fun building bots.