99 seconds to make bitcoin send you Telegram messages

Using Gitlab-CI with Telegram (Illustrated step by step)

type /help to get a list of available commands, we want to create a new bot using the command /newbot

Level : All levels

Requirements : Any web browser + your phone

[Unedited]

Here is the final instalment in this series, it’s written in away so that you don’t need to read the previous tutorials to complete this one.

Previously we’ve discussed how to create periodic jobs, then how to create a webhook using Gitlab-CI, we’ve also connected our bitcoin price webhook to IFTTT; the well known automation service.

But who needs IFTTT when we can send bitcoin price messages directly from Gitlab-CI to Telegram, chat, group or channel.

This is almost the same path you might want to take to have Gitlab-CI create and send messages to Facebook, Twitter etc.

Gitlab will GET the price of bitcoin , in Canadian dollars, when the price of bitcoin is between certain price levels, an alert will be generated in gitlab and sent as a Telegram message.

Let’s get you started

Here is what’re going to do at Telegram side:

  • We will create a Telgram bot and get it’s ID, the bot will relay the messages coming from Gitlab-CI
  • We will get your chat ID, so Telegram can send you messages

At Gitlab side :

  • Create a gitlab project with 2 files to retrieve bitcoin price, check if it’s at a certain price level, if so it will post it to Telegram
  • Select a daily schedule to run this on Gitlab-CI
  1. Create a free Telegram account : Signup for a Telegram account if you don’t already have one, you will need to confirm your phone number (15 seconds)
  2. Create a free Telegram bot and save its access token : (23 seconds)

You have 2 options here :

the following Telegram mobile app images are taken from a tutorial by Soham Kamani

type /help to get a list of available commands, we want to create a new bot using the command /newbot
type /help to get a list of available commands, we want to create a new bot using the command /newbot
type /help to get a list of available commands, we want to create a new bot using the command /newbot
type /help to get a list of available commands, we want to create a new bot using the command /newbot
type /help to get a list of available commands, we want to create a new bot using the command /newbot

3. Get your chat ID : for the bot to send messages to you, we need to get your chat ID (20 seconds)

Visit https://www.hurl.it (or use your favourite HTTP client)

in the box next to GET add the following URL, replacing MY_TELEGRAM_BOT_ACCESS_TOKEN with your actual bot access token we generated above in step 2

https://api.telegram.org/MY_TELEGRAM_BOT_ACCESS_TOKEN/getUpdates

type /help to get a list of available commands, we want to create a new bot using the command /newbot

If all went well, you should receive a response that contains private chat ID (between you and the bot) as follows

type /help to get a list of available commands, we want to create a new bot using the command /newbot
type /help to get a list of available commands, we want to create a new bot using the command /newbot

Great now that we have the access token, and the chat ID, let’s test them:

  • Go back to
  • Change the GET command in the drop down menu to POST

Paste the following URL in the text field

https://api.telegram.org/MY_TELEGRAM_BOT_ACCESS_TOKEN/sendMessage?chat_id=MY_CHAT_ID&text=hello

again replacing MY_TELEGRAM_BOT_ACCESS_TOKEN with your actually bot access token.

And replacing MY_CHAT_ID with your actually chat id described above.

type /help to get a list of available commands, we want to create a new bot using the command /newbot

If all goes as planned, you should receive your first message from your bot into your Telegram app with the world “hello”. Good job!

If the test was successful, move on to the next step, otherwise go through the above steps again and make sure that you followed each step.

If you have followed the instructions in one of these previous tutorials or the other and have created a project in gitlab, then you just need to go back to your project in Gitlab and take alook at step 6 below to edit the btc-price-alert.sh file, and look at step 7 to save your bot access token & chat id key as a Secret Variables at Gitlab , if this is your first run, no worries, we will go through the gitlab part again step by step.

here we go:

4. Sign in (or create a new account) at Gitlab.com (10 seconds)

5 . Create a new Project : Click on New Project button to create a new repo, in the name field type btc-webhook or any other name. (9 seconds)

type /help to get a list of available commands, we want to create a new bot using the command /newbot

Then save it By clicking on Create Project (1 second)

type /help to get a list of available commands, we want to create a new bot using the command /newbot
type /help to get a list of available commands, we want to create a new bot using the command /newbot

6. Create a btc-price-alert.sh file in this new project : Click on New File, copy and paste the following snippet into the btc-price-alert.sh file then click save (10 seconds)

type /help to get a list of available commands, we want to create a new bot using the command /newbot
#!/bin/bash
echo 'request Bitcoin price';
btc=$(curl https://min-api.cryptocompare.com/data/price?fsym=BTC\&tsyms=CAD)
echo 'removing all non digit from the response'
btc=${btc//[^0-9\.]/}
echo 'Bitcoin price is CA$ '"$btc"
echo 'removing decimals from the price'
btc=${btc%.*}
echo 'checking if the price within the defined range'
if [ "$btc" -ge 10000 -a "$btc" -lt 14000 ]; then 
        echo 'price is within range, will post to Telegram'
        curl -X POST \
        https://api.telegram.org/"$BOT_ACCESS_TOKEN"/sendMessage \
         -H 'Content-Type: application/json' \
         -d '{ 
         "chat_id": "'"$CHAT_ID"'", 
         "text": "Bitcoin price is CA$ '"$btc"'"
         }'
  
else echo 'Price is not within range, no alert posted this time'
fi

This will check if the price of bitcoin above CA$ 10,000 and less than CA$ 14,000 . feel free to customize it.

7. Save your access token and chat id as Secret Variables in Gitlab : now you will need the access token that you copied from step 2 and 3 above. (5 seconds)

type /help to get a list of available commands, we want to create a new bot using the command /newbot
type /help to get a list of available commands, we want to create a new bot using the command /newbot

8. Create another file, call it .gitlab-ci.yml file : (10 seconds) Click on the home menu icon (top left-) to go back to your project’s home page.

type /help to get a list of available commands, we want to create a new bot using the command /newbot
type /help to get a list of available commands, we want to create a new bot using the command /newbot
type /help to get a list of available commands, we want to create a new bot using the command /newbot
test:
script:
- bash btc-price-alert.sh

this is a simple commands, to run the bash file that we’ve created

Click on the Commite changes button, this will trigger it to build and run.

9. Schedule it to check price everyday : click on the CI/CD icon to expand the menu, select Schedules to set up a name and a timer for your webhook to trigger. (11 seconds)

type /help to get a list of available commands, we want to create a new bot using the command /newbot
type /help to get a list of available commands, we want to create a new bot using the command /newbot
type /help to get a list of available commands, we want to create a new bot using the command /newbot
type /help to get a list of available commands, we want to create a new bot using the command /newbot

Congrats! you’re done. if the price within the range you’ve specified, this webhook will post to trigger your Telegram bot to send you a Telegram message .

This job will run everyday as long as your free 2000/month build minutes do not run out.

I hope you have found this series of tutorials useful.

Leave a Reply

Your email address will not be published. Required fields are marked *