Android app + Telegram bot

Nail Asadullin
4 min readMay 7, 2023

--

In today’s world, mobile devices have become an integral part of our daily lives, and their batteries are an essential component that determines how long we can use them. Wouldn’t it be great if our phones could alert us when the battery is running low, even if we’re not near them? In this article, I will guide you through the process of building an Android app that utilizes the Telegram Bot API to send a message to a designated chat when the battery level drops below a certain threshold. We will explore the basics of the Telegram Bot API, how to integrate it into your Android app, and how to utilize Android’s BroadcastReceiver to monitor the device’s battery level. By the end of this tutorial, you’ll have a functional app that can send you notifications when your phone’s battery is low.

First, we need to create a telegram bot. Here are the steps to create a Telegram bot:

  1. Open Telegram and search for the “BotFather” bot.
  2. Start a chat with the BotFather and type /newbotto create a new bot.
  3. Follow the prompts to enter a name and username for your bot. The username must end in “bot”.
  4. Once the bot is created, the BotFather will provide you with a token. This token is the key to accessing your bot’s API.
  5. Keep your bot token secure as it is essentially the password to your bot.

Congratulations! You have now created a Telegram bot.

Next, we need to get a chatIdof the conversation between you and your bot, so only you will receive battery info. To get chatId of a Telegram bot using a web browser, you can follow these steps:

  1. Open a web browser and go to the Telegram web app or desktop app at https://web.telegram.org/.
  2. Search for your bot by its username or display name in the search bar.
  3. Once you’ve found your bot, open it.
  4. Send a message to the bot by typing anything and hitting the send button.
  5. After sending the message, go back to your web browser and add /getUpdates to the end of the URL in the address bar. For example, if the URL is https://web.telegram.org/#/im?p=@your_bot, change it to https://web.telegram.org/#/im?p=@your_bot/getUpdates.
  6. Press enter to reload the page with the new URL, and you will see a JSON response with information about the message you just sent to the bot, including the chatId.

Note: You can also use a tool like Postman or Insomnia to send a GET request to the Telegram Bot API endpoint at https://api.telegram.org/bot<your-bot-token>/getUpdates to get the chatId.

Now it's time to create our Android App. Create empty activity app, call id BatteryBroadcastApp. After that, we need to start creating our Telegram API, which we will use for sending messages about battery info.

As you see we are going to keep this project simple, no ktor or retrofit is needed, since it is only 1 API call. To make calls you need to add a OkHttp3 library to your dependencies com.squareup.okhttp3:okhttp:4.10.0 . Also, I did an old Java trick with newInstance() to create a Singleton, just if you would like to use this API for anything else in this project, without using a di library.

You might wonder what is a Secret class. This is a file where you will put your bot token and chatId from the first steps of this tutorial. This is really important: keep it safe! Do not add it to any git repo, and do not send it anywhere! If someone knows that, they can use this info to take control of your bot or gather the information you send through it. Add it to .gitignore if you still want to keep your project in git.

Now we need to create a LowBatteryReceiver to gather info about the battery.

This is a really easy BroadcastReceiver that will send a message through a bot when the battery is low.

Okay! Its time to create a ForegroundService that will register our LowBatteryReceiver.

A foreground service is a type of service in Android that runs in the foreground and provides a persistent user notification, indicating that the service is running. Foreground services are useful for performing tasks that require long-running operations and cannot be interrupted, such as playing music, tracking location, or downloading large files. Running a foreground service ensures that the app remains visible to the user, and the system gives it a higher priority than background services, making it less likely to be killed by the operating system.

Almost there! The last thing we need to do is to create an Application class that will launch foreground service and update our AndroidManifest.xml with information about Service and our Application class.

And this is it!

Although we used a foreground service to keep our app running in the background, it is important to note that there is no way to guarantee that the app will stay in memory forever. Android system has mechanisms in place to prioritize and manage system resources, including memory and battery usage, to optimize device performance. Therefore, the battery level example we used in this tutorial is just one of many use cases for broadcasting information to a Telegram bot. With the help of the Telegram Bot API and the techniques demonstrated in this tutorial, you can create a wide variety of applications that utilize the bot as a server for receiving and processing information from your Android device.

--

--