DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Emulating the History Command Within a Bash Script
  • Git Bash (Or Git in Bash)?
  • SonarQube Analysis With Ginkgo on Mac
  • Create a Kubernetes Cluster With Centos

Trending

  • Streamlining Event Data in Event-Driven Ansible
  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  • Docker Model Runner: Streamlining AI Deployment for Developers
  • Recurrent Workflows With Cloud Native Dapr Jobs
  1. DZone
  2. Coding
  3. Languages
  4. A Simple Telegram Time Tracker Bot Creation

A Simple Telegram Time Tracker Bot Creation

Learn how to create a simple in Telegram with a primitive time tracker, a simple case of interaction with the Telegram API.

By 
Oleksandra Liubytska user avatar
Oleksandra Liubytska
·
Feb. 04, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
3.7K Views

Join the DZone community and get the full member experience.

Join For Free

Telegram nowadays is a convenient, easy-to-use, intuitive, and secure messenger — and of course we all love stickers. Besides the personal messaging, we often use group chats with family, friends, and colleagues. In addition to live ordinary users, there are also bots on Telegram. They are created to automate replies, meaning the bot responds to specific messages (commands) and performs certain actions. An action can be either a simple greeting in response or a chain of certain questions and answers to perform specific logic operations.

Personally, I, for instance, constantly use @vkmusic_bot_news chatbot. It’s a simple bot for music searching and listening. A user sends a message with the name of a composition or the author and the bot provides variants that match the request.

In my example, I asked for a song «show must go on» and received several variants to choose from. Having chosen the first variant, I received a track in the next message. It’s convenient that you can listen to music right in the chat with the bot. In such a way a bot can fully substitute a third-party app, like a player on your phone. You just search and listen to the music right away.


So, we can see that the Telegram chatbots are a rather convenient and multifunctional feature of the messenger. That’s why this article is devoted to a simple chatbot creation. I decided to make a primitive time tracker to show you an example. It’s the simplest case of interaction with the Telegram API.

Simple Telegram Chatbot Creation

So let’s start.

You can find the docs here: https://core.telegram.org/bots/api.

First, you should create and register the bot in the Telegram. Let’s find the all-bots Father @BotFather. It’s a bot for bot registration.

We inform him that we want to create our own chatbot:

Shell
 




xxxxxxxxxx
1


 
1
/newbot


With the next request, he asks to create a name for the bot. Since my bot will track time, I give him the corresponding name and write:

Shell
 




xxxxxxxxxx
1


 
1
timeTrackerBot


The next step is to create the bot’s username (it will be used for search @username). The condition is that the username must end in bot or _bot. I write:

Shell
 




xxxxxxxxxx
1


 
1
my_time_tracker_bot


It’s ready! We receive a token in response, which means that we have registered a new chatbot.

Development

To start, I decided to look at what libraries the internet offers to us. The variants are multiple. The full list of the proposed options can be found here. My choice was TelegramBotApiBundle for Symfony. We install it:

Shell
 




xxxxxxxxxx
1


 
1
composer require borsaco/telegram-bot-api-bundle


And proceed.

The bundle supports the work with several bots at a time; besides, there is the option of adjustment (sending only to the developer) and working through proxy. For the test example, we do not need much, so we remove what we do not need.

Shell
 




xxxxxxxxxx
1


 
1
config/packages/telegram.yaml


I also carried the token out into the variable APP_TELEGRAM_TOKEN в .env:

A Bit of Theory

Telegram API works in two modes: you can get updates from the server through getUpdates() method or adjust the webHook. The first variant is good because it’s extremely simple, but the drawback of it that you should constantly request the server whether there are any updates. The webHook variant allows you to not have to think about how to get the updates but to focus on their rendering. In this case, Telegram will send the updates itself to the URL we’ll indicate.

The advantage of the second approach is obvious.

To register a Webhook, we’ll do the following:

Shell
 




xxxxxxxxxx
1


 
1
$bot = $botService->getBot('timeTracker');
2
$bot->setWebhook(
3
    [
4
'url'=>'https://myWebSite.com/webhook'
5
]
6
);


Webhook in the url value is a route, which we’ll make a bit later.

$botService is an object of the service Borsaco\TelegramBotApiBundle\Service\Bot, which can be injected in any place of the project.

Important: Telegram API only supports sending to https!

Let’s now check our Webhook. If you send a message to our Telegram bot, it will send us such a dataset in response:

Shell
 




xxxxxxxxxx
1
26


 
1
{
2
  "update_id": 21406673,
3
  "message": {
4
    "message_id": 24,
5
    "from": {
6
      "id": 701891111,
7
      "is_bot": false,
8
      "first_name": "aleksei",
9
      "language_code": "ru"
10
    },
11
    "chat": {
12
      "id": 701891111,
13
      "first_name": "aleksei",
14
      "type": "private"
15
    },
16
    "date": 1580672814,
17
    "text": "/help",
18
    "entities": [
19
      {
20
        "offset": 0,
21
        "length": 5,
22
        "type": "bot_command"
23
      }
24
    ]
25
  }
26
}


We are interested in the part with the message text. In this case, it is clear that I sent the bot «/ help».

Naturally, he does not answer.

The presence of entities in the response indicates that the message was perceived by the bot as a command (everything that starts with a slash and is written in Latin is a command for Telegram).

Let’s create a controller. It will be the entry point for the requests from the Telegram API where to the updates will come.

For convenience, I put all the logic into the MessageProcessor service, where I pass the string received from Telegram.

The essence is simple. There are 4 commands: help, start, stop, report.

To the «help» message or any other one that does not fit into this list, the bot should return a message with a list of the commands available.

Each command presupposes specific behavior.

There are also two situations when the answer will tell the user that this action is prohibited at the moment (until the time tracker is stopped — you can’t start a new one. Accordingly, while there is no activity, you can’t stop anything either.)

I have added the following entities:

  1. User, which has the fields name, telegramId, and collection timeLines.
  2. TimeLine, which has a starting date and an end date startedAt, stopedAt.

That is, a user can have multiple timelines (which have a start time and a stop time).

We get a JSON string, and the first thing I do is decode the string and get the object.

Shell
 




xxxxxxxxxx
1


 
1
$response = \json_decode((string)$telegramUpdate);


Further on, I check if there is such a user in the database (the data about the user who wrote to the bot is taken from response. > message > from). If there is no such a user, we create it.

Since we want that «help» and «/ help» commands to be perceived by the bot in the same way, we translate the text of the message from the user into lowercase and delete the normal and the backslashes.

Shell
 




xxxxxxxxxx
1


 
1
$messageText = mb_strtolower($response->message->text);
2
$messageText = str_replace([’\\’, ’/’], ’’, $messageText);


Now we check the received command — if it’s included in the list of commands that we can process. And now, based on what we’ve got, we send a reply.

Shell
 




xxxxxxxxxx
1


 
1
$messageCommand = $this->isSupports($messageText) ? $messageText : false;


If it’s either a «start» or «stop» command, we create a TimeLine for this user or end the current one accordingly and inform the user about it.

If this is a «report», we calculate the time in all timeline for today and send the total number of hours and minutes to the user. We send the messages with the sendMessage method, which accepts an array of parameters. Required parameters are: chat_id and text.

A complete list of commands can be found here.

Since this is a test case, I limited myself to a simple switch / case for command selection.

Shell
 




xxxxxxxxxx
1
26


 
1
switch ($messageCommand) {
2
  case self::HELP_COMMAND :
3
     $this->bot->sendMessage(['chat_id' => $user->getTelegramId(),      'text' => self::ANSWERS[self::HELP_COMMAND]]);
4
     break;
5
  case self::START_COMMAND :
6
     if ($this->timelineService->doesActiveExist($user)) {
7
        $this->bot->sendMessage(['chat_id' => $user->getTelegramId(), 'text' => self::BAD_ANSWERS['existNotStoppedTimeLine']]);
8
        break;
9
     }
10
     $this->telegramService->startTimeForUser($user);
11
     $this->bot->sendMessage(['chat_id' => $user->getTelegramId(), 'text' => self::ANSWERS[self::START_COMMAND]]);
12
     break;
13
  case self::STOP_COMMAND :
14
     if ($this->timelineService->doesActiveExist($user)) {
15
        $this->bot->sendMessage(['chat_id' => $user->getTelegramId(), 'text' => self::ANSWERS[self::STOP_COMMAND]]);
16
        break;
17
     };
18
     $this->bot->sendMessage(['chat_id' => $user->getTelegramId(), 'text' => self::BAD_ANSWERS['timeLineNotFound']]);
19
     break;
20
  case self::REPORT_COMMAND :
21
     $timeForToday = $this->timelineService->getTodayTotalByUser($user);
22
     $this->bot->sendMessage(['chat_id' => $user->getTelegramId(), 'text' => \sprintf(self::REPORT_COMMAND, $timeForToday)]);
23
     break;
24
  default:
25
     $this->bot->sendMessage(['chat_id' => $user->getTelegramId(), 'text' => self::ANSWERS[self::HELP_COMMAND]]);
26
}


General view of the service is like this:

Thus, we’ve got a bot that can help you track the time in course of the day.

Conclusion

To draw a line, we can safely say that the Telegram API is quite functional and easy to master. A big advantage is good documentation and the ready-made libraries available for the usage.

Tracker (business software) Command (computing) shell IT

Published at DZone with permission of Oleksandra Liubytska. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Emulating the History Command Within a Bash Script
  • Git Bash (Or Git in Bash)?
  • SonarQube Analysis With Ginkgo on Mac
  • Create a Kubernetes Cluster With Centos

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!