DEV Community

loading...

Create a Github Action that Send Everyday Covid19 Status Through Telegram Using Python

spiritbro1 profile image spiritbro1 Updated on ・3 min read

Introduction

Github action is cool feature from github that can be setup to send event when pushed , pulled or on schedule using cron in this tutorial i will tell you how to create a github action that use schedule event to send message to telegram about the situation of covid in the world which mean how much is getting infected, how much is recovered and how much is death through a picture. For the image of covid data i use an API from mathdroid you can check that out here.

https://twitter.com/mathdroid/status/1239049486158061569

Prequisites

  • telegram account

  • python 3.6 or newer python is ok.

  • sudo apt install python3-pip -y

  • apt-get install python3-venv

  • python3 -m venv env

  • source env/bin/activate.fish

  • mongodb database you can get one for free in mongodb atlas

  • for the code to send the telegram message i create it right here you can clone it

GitHub logo catflip / telecovid19

this repo run everyday to check the situation of covid19




Step 1 - create a telegram account

Before working with Telegram’s API, you need to get your own API ID and hash:

  1. Login to your Telegram account with the phone number of the developer account to use.

  2. Click under API Development tools.

  3. Create new application window will appear. Fill in your application details. There is no need to enter any URL, and only the first two fields (App title and Short name) can currently be changed later.

  4. Click on Create application at the end. Remember that your API hash is secret and Telegram won’t let you revoke it. Don’t post it anywhere!

Step 2 - create a secret

Secret is used for calling secret in our github account to our github action you can create secret by creating repo first and then click settings and create secret

secret

add a secret just like in the image that consist of

MONGODB_DATABASE=#database name
MONGODB_HOST=#mongodb URI from mongodb atlas dont forget to add this '?retryWrites=false' to the end of the URI
TELEGRAM_API_HASH=#telegram api hash that we get before also
TELEGRAM_API_ID=#your telegram api id that we get before
SEND_TO=#username of your account or just type 'me'
Enter fullscreen mode Exit fullscreen mode

if you develop in local you can also add this variable into file called .env

Step 3 - Let's create a github action

Create a folder called .github and inside that folder create a folder named workflows and inside that folder create yaml file with whatever name you want let's named it ci.yml for now.

yaml

Copy paste this yaml file

name: SCHEDULE

on: 
  push:
  schedule:
    - cron : '0 0 * * *'

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Set up Python 3.x
      uses: actions/setup-python@v2
      with:
        python-version: '3.x' 
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Send message
      run: python index.py
      env:
        MONGODB_HOST: ${{ secrets.MONGODB_HOST }}
        MONGODB_DATABASE: ${{ secrets.MONGODB_DATABASE }}
        TELEGRAM_API_ID: ${{ secrets.TELEGRAM_API_ID }}
        TELEGRAM_API_HASH: ${{ secrets.TELEGRAM_API_HASH }}
        SEND_TO: ${{ secrets.SEND_TO }}

Enter fullscreen mode Exit fullscreen mode

this yaml file basically tell us to run the code at midnight everyday or at push and send it to our username or your friend username in telegram

Conclusion

Now you know how to schedule application to run on a spesific time using gihub action disclaimer this is unlimited if you use public repo like me if you have a private repo you have 2000 minute free and if you want more you should pay you can see the price at github and you can also change the workflows to fit to your use case lets say every five minute and so on and you can also change the python code so it will fit to your use case also.

Discussion (1)

pic
Editor guide
Collapse
piyush profile image
Piyush Suthar

Just few days ago, I created an api similar to mathdro.id , covid-img.herokuapp.com and The code is open sourced on github github.com/PiyushSuthar/Covid-19-I...

Kind of self promotion 😁😬