SVN bot for Telegram

Photo of an Amiibo Mario figure holding a paper with a telegram

Telegram is fucking awesome. I don't even see the point in discussing it. If you're ready to argue with this, you probably just haven't tried using it. We tried several messengers for company correspondence. In the end, we settled on Telegram. Slack is also fucking awesome but slower and fucking expensive. Plus, I still can't get used to the "per user/per month" payment model. So, Telegram. We just need to push notifications from the repository and Trello to it. I'll tell you about the first one now.

It's simple, like a banana:

  1. Register a bot with @BotFather
  2. Find out your chat ID in Telegram
  3. Create a post-commit hook in the repository
  4. Write 3 lines of code

Register the bot

You should have Telegram installed. Add BotFather. Write to him /newbot, first he'll ask for the bot's name, it will be displayed as the sender's name. Then the bot's username, like my_super_mega_bot, it must end with bot. We get a token in response, which we definitely save. Optionally, you can add a userpic /setuserpic, full and short description /setdescription /setabouttext.

Find out the chat ID

First, add the newly created bot to your work chat. Write any shit in the chat. Then enter in the browser https://api.telegram.org/bot<tokenwithoutanglebrackets>/getUpdates?offset=0 in response we'll get JSON in which we find the chat ID.

Post-commit hook

We use SVN and don't ask why. With other version control systems, the situation should be similar. Just google how to create a post-commit hook for your system. For SVN, in the repository folder there's a hooks folder (I mean the repository on the server, not your working copy if anything). In it, the post-commit file, we copy-paste there:

			
REPOS="$1"
REV="$2"
DIR="$REPOS/hooks"
LOG="$DIR/log.txt"

/FULL PATH TO REPOSITORY/hooks/telegrambot-posthook.py "$REPOS" "$REV" >> $LOG 2>&1 || exit 1				
			
		

We write three lines of telegrambot-posthook.py not forgetting to change token and chatid to our own. Here half of the imports probably aren't needed, but I'm too lazy to check which ones. We save this file in /full path to repository/hooks/telegrambot-posthook.py not forgetting to change the file owner to the needed one, for CentOS this is usually apache "chown apache:apache /full path to repository/hooks/telegrambot-posthook.py" and make the file executable "chmod +x /full path to repository/hooks/telegrambot-posthook.py"

			
#!/usr/bin/python

import sys
import subprocess
import urllib
import urllib2
import json
import telegram
from twx.botapi import TelegramBot, ReplyKeyboardMarkup

LOOK='svnlook'        # svnlook location
TOKEN = '123456789:AAFzCs7LbwfHIGiINUh74QKAMM7M1y8WYTO'           # Bot token
CHATID = '-12345678'    # ID of telegram chat

def run_look(*args):
        p = subprocess.Popen(' '.join([LOOK] + list(args)), stdout=subprocess.PIPE, shell=True, stderr=subprocess.STDOUT)
        out, err = p.communicate()
        return out

def get_commit_info(repo, revision):
        comment = run_look('log', repo, '-r', revision)
        print comment
        author = run_look('author', repo, '-r', revision)
        files = run_look('changed', repo, '-r', revision)
        payload = '#SVN ' + revision.rstrip('\n') + ' ' + author.rstrip('\n') + ' ' + comment.rstrip('\n')
        return payload

def main():
        payload = get_commit_info(sys.argv[1], sys.argv[2])
        bot = TelegramBot(TOKEN)
        bot.send_message(CHATID, payload)

if __name__ == '__main__':
        main()
			
		

That's basically it, now messages from the bot should come to the chat after each of your commits.