{{announcement.body}}
{{announcement.title}}

How to Send Telegram Notifications With Google Cloud Build (GCB)

DZone 's Guide to

How to Send Telegram Notifications With Google Cloud Build (GCB)

This article is a quick How-to after struggling with Google Cloud Build about how to send notifications when a build has failed or is successful.

· Cloud Zone ·
Free Resource

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

You may also like: Serverless Is the New Build Server: Google CloudBuild (Container Builder) via NodeJS

Google Cloud Build

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

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

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 mobile 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 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 BOT ( 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 a notification bot.

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 the GIT repository that you want to use as a demo project.

Then go into the Google Cloud Build section:

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

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.

topics

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

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 Node.js 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 it 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.

Plain Text




x


 
1
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:

Plain Text




xxxxxxxxxx
1


 
1
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-tool: 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 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 the internet and it will be probably the subject of another article.


Further Reading

Automatic Builds at Your Fingertips With GCP Cloud Build

How to Get Metrics for Advance Alerting to Prevent Trouble

The Easy Way to Write a Telegram Bot

Topics:
chatbot ,devops ,google ,google cloud ,google cloud functions ,telegram ,cloud

Published at DZone with permission of Sylvain Leroy . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}