Telegram Bot

This project is about telegram Bot, which wishes you good morning by sending you a message daily at 07:00 AM. To perform it we used telegram API with python as main language, crond for scheduling the execution of python scripts, Docker to encapsulate our project in a container, and two AWS services, the first one is ECR (Elastic Container Registry) for storing the docker image, and the second one, EC2 (Elastic Compute Cloud), in which the Docker container will run on an instance.


Python Scripts

If we want the bot sends us good morning daily, first we have to send it a message to the bot, this message must be the word /start.

To access the Telegram API, we need a token, which we obtained by creating the bot. Now we can interact with the API, we will use the /getUpdate method, which returns the messages that the bot has received during the day, so we need to save the chat_ids of those messages to send to people the good morning daily, the following script uses sqlite3 to do it.



Then we have the main.py file, which gets the chat_ids, that the previous script saved them into the local DB, after getting the chat_ids, it sends the message to the users.



Schedule the scripts with Crond

To schedule the execution of those python scripts we will use crond, which executes cron jobs that are scheduled in a crontab file, In this file, you must specify;

minute, hour, day_of_month, month, weekday, and command.


In the following crontab file, we are specifying to run every day the db.py file at 23:00 PM and the main.py file at 07:00 AM.

*why 23:00 PM? Well, we need to save the chat_ids before ends the day.


The code in a Docker Container

Then we create a Dockerfile to run the project in a container, in the following file we are using a python image, adding the Timezone America/Asuncion (the city where I live), working directory, copy of our files in the workdir, install dependencies, and run crond



Deploy the container on an instance

To deploy this container on an EC2 instance, we first need to store the image in some container image repository, for this project we will use Amazon ECR Service.

After pushing the image to ECR, we have to connect to the instance, here we are performing by ssh, Inside the instance, we have to perform some steps

1) Install Docker

2) Pull the image from ECR

3) Run the container



And finally, we have the result, our bot sends us a good morning message every day.


For this project we wanted to do some automation practice, so here we did it with telegram API but for example the Docker Container may have a process that is needed to run every specific hour in the day and with EC2 and contrab is a way to do it.