The bot revolution is not only about artificial intelligence. A bot can be a tool in your messenger with a simple chat interface that can be used to extend the functionality of sites or services or can even be a standalone application. Bots are cheaper to develop and easier to install, and another great feature is that messengers can be used on every type of device—laptops, smartphones, and tablets. That's why everybody is crazy about bots now.
And the biggest messenger with an open bot API is Telegram.
What We Are Going to Do
In this article we will create a simple stopwatch Telegram bot. I will show you how to create your bot, connect with analytics, write some code, and finally add your bot to a bot store.
By the way, I've already prepared a demo, so you can test it just by adding @stopwatchbot to your Telegram contact list.
Create a Bot With BotFather
The first step to creating a bot is to register the account of your bot in Telegram. And there is a bot for that, called the BotFather. Just add it to your contact list and you'll be able to create and set up Telegram bots, just by typing the /newbot command and following the instructions of BotFather.
![]() |
Later you can use BotFather to add descriptions or photos to the profiles of your bots, regenerate tokens, set lists of commands to use, delete accounts, and so on. To get a full list of commands, just type /help in a chat to get a list of BotFather's commands.
Connect to Botan Analytics
There is no built-in analytics in the Telegram Bots API, but it's important to know how many users you have, how they act, and which commands they trigger more. Of course, we can collect this information using our own engine, but if we want to focus on bot functionality, not metrics, we just need to use an out-of-the-box solution.
And there is a simple tool to connect your bot to analytics, called Botan. It's based on Yandex AppMetric and completely free. Using Botan, you can segment your audience, get information about user profiles, get the most used command, and get beautiful graphs right in your messenger, like this:
![]() |
![]() |
Create and Register an SSL Webhook
In Telegram there are two ways to get messages from your users: long polling and webhooks.
![]() |
If you'd like to make sure that the Webhook request comes from Telegram, we recommend using a secret path in the URL, e.g. https://www.example.com/<token>. Since nobody else knows your bot‘s token, you can be pretty sure it’s us.If your SSL certificate is trusted, all you need to do is open this URL in your browser:
- https://api.telegram.org:443/bot[token]/setwebhook?url=[webhook]
- openssl req -newkey rsa:2048 -sha256 -nodes -keyout /path/to/certificate.key -x509 -days 365 -out /path/to/certificate.crt -subj "/C=IT/ST=state/L=location/O=description/CN=yourdomain.com"
- sudo ufw allow 443/tcp
- curl \
- -F "url=https://yourdomain.com/path/to/script.php" \
- -F "certificate=/path/to/certificate.key" \
- "https://api.telegram.org/bot[token]/setwebhook"
- {"ok":true,"result":true,"description":"Webhook was set"}
Build a Database
Now we need to build a database for our timers. What do we need to store in it? When a user commands the stopwatch to start, we will take the ID of the chat and save a row with the chat ID and current Unix time, which is the number of seconds between now and the start of Unix Epoch, which is 1 January 1970 at UTC. Consequently, we will save a row with the chat ID and integer timestamp of the current Unix time.
To show the current stopwatch time, we will get the saved timestamp and compare it with the current timestamp. The difference will be the current time in seconds. If the user stops the timer, we will simply delete the row with the current chat ID.
So let's create a database and table to store the stopwatch information:
- CREATE TABLE IF NOT EXISTS `stopwatch` (
- `chat_id` int(10) unsigned NOT NULL,
- `timestamp` int(10) unsigned NOT NULL,
- PRIMARY KEY (`chat_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
If you found this post interesting, please follow and support us.
Suggest for you:
Learning PHP 7: From the Basics to Application Development
The Complete PHP 7 Guide for Web Developers
Up to Speed with PHP 7
Learn PHP 7 This Way to Rise Above & Beyond Competion!
The Complete PHP with MySQL Developer Course (New)




No comments:
Post a Comment