📜 ⬆️ ⬇️

Sync CI Notifications with Telegram

Today I would like to share my experience of synchronization of Continuous Integration of servers (Bamboo, Jenkins, TeamCity, etc.) with a free instant messenger from Pavel Durov - Telegram.

This problem was wanted to be solved due to the fact that the programmers on our project did not react to notifications sent by mail, and our corporate messenger had telegrams. Experimental CI is our corporate Bamboo, from which these same notifications should come.

To organize this synchronization, we will use the BOT API from the telegram, the Gmail API from Google, as well as our ubuntu server on the Digital Ocean.

First we need to create a new bot in telegrams. For this, I recommend using clause 1 of this article: habrahabr.ru/post/262247 . After registration, we receive access data to our bot of the form: 112714817: AAEwZWoZ6X1Go76gt3_hUz9717ihNboXrnw
')
Be sure to remember this data - they will still be useful to us!



Next, we need to create a new chat, to which our bot will send notifications. We create and do not forget to invite the bot and your colleagues to it. Now we definitely need to find out the ID of this chat, for which we send any test message to the chat, and then in the browser or Surl (which is more convenient for someone) we drive in:
https://api.telegram.org/bot<TOKEN>/getUpdates 



From the server's response, we learn that our chat ID is “-35576913” (note that “-” is part of the ID, you can't lose it). Now we check that everything works correctly. To do this, try to send a test message from our bot:
 https://api.telegram.org/bot<TOKEN>/sendMessage?chat_id=-35576913&text=Test_Message 



Great: we received our test message, which means everything works as it should! Now smoothly go to the Gmail API. First we need to create a new project in console.developers.google.com . Then we give our project access to the Gmail API. And then we need to get JSON with the credits of our new project:





Great, almost everything is ready! Now I suggest using my python script. You can get it here: github.com/egorvas/ci_to_telegram/blob/master/ci_to_telegram.py

You will also need to install two third-party python modules:
 pip install google-api-python-client pip install requests 

You need to put the file with client_secret.json in the same folder as the script, and run it, after which the browser opens and prompts you to allow access to gmail mail.



I created a new gmail account for our project, to which I attached notifications with bamboo, but you can use the one already created. As your heart desires!

Do not forget to check that notifications from CI come to this mail. Now in the folder with the script, in addition to client_secret.json, should be the credentials folder. If so, then you did right.



Now we need to modify the script a bit, to do this, we change the TELEGRAM_BOT_CREDENTIALS and CHAT_ID variables to the ones we learned above. Now we try to execute the script, and, if everything is correct, then all unread letters should fall into the chat with your bot.

Briefly about what the script does. First, he finds all the unread letters in your mailbox, then he sends the text from them to you in telegrams, and then he makes these letters read.

The last step remains: we need to organize a recurring launch of this script. I used cron on our ubuntu server. We load our folder with scripts via ssh to the server and configure cron:

First, install cron:
 sudo apt-get update sudo apt-get install cron 

Then run the cron settings editor:
 crontab -e 

Add a repetition of our script every minute:
 * * * * python /usr/share/telegram/ci_to_telegram.py> 

We check that everything was saved correctly:
 crontab -l 

Now our script runs every minute. If there are no new letters in the mail, then he does nothing. Otherwise, it sends the text of the letter to you in a telegram and makes the letter read.

That's all. Thanks for attention. If you have questions - write, I will gladly answer.

Source: https://habr.com/ru/post/264985/


All Articles