Send APK automatically to Telegram using Dart/Flutter

--

If you’re trying to improve the Continuous Delivery “CD” on your Flutter project, one nice tool to use is Telegram because you’re able to send APKs through it.

OK, but there is a small problem because initially the process probably will be manually (you will have to build the apk and, going through the folders of the project, you will find the APK and, finally, will send it to the chat in the Telegram…), but we, Developers, prefer automatic process than manually, right?

Right! In order to automate this process, me and my teammate, Victor Silva, created a bot that is able to send APKs files with only one command. You will run the build command and, in the final of the process, the apk will be sent to Telegram.

How can you do that as well? Easily! Just do the following steps!

Obs 1: Flutter version 2.5.0
Obs 2: Dart version 2.14.3

  1. Create the Telegram Bot:

1.1. Go to Telegram and search for @BotFather:

Click on the BotFather to start a chat

1.2. Type /newbot in the chat with BotFather:

1.3. Give it a name:

It’s nice to write it in spaced camel case, but it is up to you.

1.4. Give the bot an username following the snake case pattern:

There it is the why I said that is nice to write the name in spaced camel case, because now the unique difference between the username and the name is the spacements and the underlines!

1.5. Now, add the Bot in the group that you want to receive the APKs

Choose the one that has the same name as you set before in the bot creation process

2. Take the credentials:

2.1. First of all, go back to the chat with BotFather and take the Token right here:

As well said by the bot, keep it safe!

2.2. Now, access the following link changing the <YourBOTTOken> to the Token that you just got in Step 2.1:

https://api.telegram.org/bot<YourBOTToken>/getUpdates

Example: In my case, I would access this Link

https://api.telegram.org/bot5117900793:AAEjvocpAFQC75iSFypj7YtO61LHtz_ue-Q/getUpdates

2.3. Now, probably you are seeing something like that:

2.4. Don’t worry, just remove and add again the bot to the group. After that, access one more time the link with your bot token and you will see it:

The important thing here is to save the Chat ID because you’ll need it in the next steps.

3. Create the Dart file:

3.1. These are the imports:

Obs 1: We will use the dart_telegram_bot library!
Obs 2: We have to hide File from telegram_entities because we will want to use File from dart:io, and not from telegram_entities!

3.2. This is the core of the File:

An important detail is that the Chat ID and Token ID are in the .ENV file for security.

Obs 1: We removed the number of builds to improve the readability (it happens in the _returnAppVersion() auxiliary function).

Obs 2: The entire file will be available at the end of this article as an embed, so, feel free to copy and paste or even improve it!

4. Prepare the Commands:
Now you’ll have to write two commands, one to build the Apk and another to run the file that we just wrote in Step 3.2, and this command could be:

flutter build apk && dart run send_apk_telegram.dart

However, in order to make the code smaller, here we use the Makefile library, and the command becomes something like that:

make build-and-send-apk

But it doesn’t change the main point, which is to generate an Apk and Send it using the send_apk_telegram.dart file.

5. Now, send it!
Run the command and see how it works:

That’s it! Now, whenever you finish a version or need to generate a fast build of your current App, just save all the changes and run both build and send commands to receive an updated app!

One nice thing is that this solution exists in a lot of different environments, so, if you’re coming from Java, Javascript, or even Python environments, there are a sort of different telegram libraries. All you’ll have to do is reply the idea using their respective functions and their respective libraries.

The Code as an embed:

--

--

No responses yet