How to send Telegram Notifications with Google Cloud Build (GCB)

This article is a quick HOW-TO after struggling with Google about how to send notifications when a has failed or is successful. Since I did not want to use and the heavy client, I give a try in this post with Telegram and discover the magical world.

Google Cloud Build

Cloud Build is a service that executes your builds on Google Cloud Platform infrastructure. Cloud Build can import from Google Cloud Storage, Cloud Source Repositories, , or Bitbucket, execute a build to your specifications, and produce artifacts.

Google Cloud Build is rather a cheap alternative to Jenkins, Travis- and Circle if you have private projects. While the other solutions requires a quite expensive subscription, you pay GCB according the used build time. If you do not commit frequently or the build is a short task, it is a really fair solution. And extra bonus, the first 120 build minutes are free.

Google Cloud Build
Google Cloud Build

As you see, the Google Cloud Build interface is not as attractive as the other competitors. That’s why I wanted at the bare minimum to receive some notifications.

There is no built-in process therefore I had to build my own. GCB recommends Slack however my choice is in favor of push messages with Telegram.

Telegram

Telegram is a cloud-based instant messaging and voice over IP service. Users can send messages and exchange photos, videos, stickers, audio and files of any type. Default Messages and media in Telegram are encrypted when stored on its servers.

Telegram running on an iPhone X (wikipedia)
Telegram running on an iPhone X (wikipedia)

Telegram Bots

Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to our bot API.

What can do a ( extracted from Telegram website) :

 

  • Get customized notifications and news.

  • Integrate with other services.

  • Accept payments from Telegram users.

  • Create custom tools.

  • Build single- and multiplayer games.

  • Build social services.

How to send notifications with GCB

Here is a diagram to explain how to implement such notification bot.

Google Cloud Telegram implementation diagram
Google Cloud Telegram implementation diagram

The diagram does not reveal the implementation order. Here is the order that I followed.

Create a Job with GCB

The first step is to identify your GIT repository that you want to use as demo project.

Then go into the Google Cloud Build section :

Google Cloud Build access
Google Cloud Build access

You have two available actions, Connect to repository and Create a Trigger once your GitHub repository has been connected.

GCB Connect Repository
GCB Connect Repository

Check the presence of the subject

You should have a topic created by GCB indicating that the build messages are sent into a dedicated queue.

Create the Telegram BOT

Now it is time for the fun part of the tutorial.

You will have to install first the Telegram application on your mobile phone and to follow this tutorial to create your BOT. Just talk to BotFather

BotFather
BotFather

After the creation of the BOT, store in your password manager, the Telegram TOKEN.

Create the Cloud Function

In this step, we will finally create our Telegram notification chatbot. The program will be coded in and deployed as a Cloud Function in Google Cloud.

Why a Cloud Function ? Because we do not want to allocate a full-time virtual machine to process only during a few seconds the Google Build messages.

The code is available there and the explanation is pretty straightforward : https://github.com/sleroy/gcb-telegram-build

Deploying the Cloud Function

To deploy the Cloud function, you need to use gcloud.

The cloud function is only publishing in a  single GROUP_ID.

To obtain a Telegram GROUP Id, you need to add the Telegram bot by inviting into a group. Then you need to query a special endpoint to get the list of all subscriptions for this bot.

Here is the Q/A about it : https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id

The deployment instruction to push the cloud function. If everything is OK, the cloud function will be deployed.

gcloud functions deploy subscribeTelegram --trigger-topic cloud-builds --runtime nodejs10 --set-env-vars "TELEGRAM_TOKEN=token1,GROUP_ID=groupID"

Result

What you should obtain, is an output similar to that :

Deploying function (may take a while - up to 2 minutes)...done. 
availableMemoryMb: 256
entryPoint: subscribeTelegram
environmentVariables:
GROUP_ID: 'xxxxxxxxxx'
TELEGRAM_TOKEN: zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
eventTrigger:
eventType: google.pubsub.topic.publish
failurePolicy: {}
resource: projects/xxxxxxxx/topics/cloud-builds
service: pubsub.googleapis.com
labels:
deployment-: cli-gcloud
name: projects/xxxxxxxxx/locations/us-central1/functions/subscribeTelegram
runtime: nodejs10
serviceAccountEmail: xxxxxxxxxx@appspot.gserviceaccount.com
sourceUploadUrl: https://storage.googleapis.com/gcf-upload-us-central1-
status: ACTIVE
timeout: 60s
updateTime: '2019-12-10T21:44:59Z'
versionId: '14'


The Telegram notification program

Conclusion and further improvements

This bot is really simple and easy to do but it lacks of the main feature, to answer to commands. Here is a list of interesting commands I would like to add :

  • get the list of nth last commits
  • trigger a build
  • display the build logs (or the last nth lines)

For this, you need to implement a bot server (in node.js for example). There are plenty of examples on internet and it will be probably the subject of another article.

 

 

 

 

Next to read  Using a self-hosted Cloud IDE in 2018, my TOP 3.