Last active
August 29, 2015 14:17
-
-
Save upa/f512d9fe1f60c07d91c6 to your computer and use it in GitHub Desktop.
track slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Send Trac ticket updates to Slack | |
# | |
# Written by Gregor Dorfbauer / [email protected] | |
# http://usersnap.com - Show, don't tell. | |
# | |
# | |
# Setup Steps: | |
# update your trac.ini | |
# #################### | |
# [notification] | |
# smtp_always_cc = slack@localhost | |
# #################### | |
# invoke with a .forward file of your slack user - content "|/home/slack/tracslack.py" | |
import re | |
import sys | |
import json | |
import requests | |
import email | |
import base64 | |
from urllib import urlencode | |
from urllib2 import urlopen | |
LOGFILE="/home/slack/log.txt" | |
#SLACK_URL="https://yourcompany.slack.com/services/hooks/incoming-webhook" | |
SLACK_URL="https://yourcompany.slack.com/services/hooks/incoming-webhook?token=your-slack-token" | |
#those are free to choose | |
SLACK_CHANNEL="#your-channel" | |
SLACK_USERNAME="trac" | |
SLACK_EMOJI=":ghost:" | |
############################################################################### | |
ofile = open(LOGFILE, "a") | |
firstempty = False | |
subject = "" | |
content = email.message_from_string(sys.stdin.read()) | |
payload = content.get_payload() | |
try: | |
payload = base64.decodestring(payload) | |
except: | |
pass | |
newcont = [] | |
omitheader = 2 | |
ofile.write("Payload: %r" % payload) | |
for line in payload.split("\n"): | |
ofile.write("line %r" % line) | |
#filter out header + lines (free to extend) | |
if omitheader == 2 and "-----+-----" in line: | |
omitheader -= 1 | |
continue | |
if omitheader == 1: | |
if "-----+-----" in line: | |
omitheader -= 1 | |
continue | |
if line in ("-- ", ""): | |
continue | |
newcont.append(line) | |
title = newcont.pop () | |
tracurl = newcont.pop () | |
ticketurlall = newcont.pop () | |
ticketurl = ticketurlall[13:len(ticketurlall) - 1] | |
ofile.write("\nticketurl=%s\n" % ticketurl) | |
# Reduce ticket url using bit.ly | |
username = 'your bitly account name' # use your Username/password | |
apikey = 'your bitly api key' | |
bitly_url = "http://api.bit.ly/v3/shorten?login=%s&apiKey=%s&longUrl=%s&format=txt" % (username, apikey, ticketurl) | |
result = urlopen(bitly_url).read() | |
ofile.write ("bitly result = %r\n" % result) | |
shorturl = "" | |
try : | |
bitlyresult = json.loads (result) | |
shorturl = bitlyresult["data"]["url"].encode('utf-8') | |
except : | |
shorturl = result | |
if len (newcont) > 0 : | |
if re.match (r'Comment', newcont[1]) or re.match (r'Changes', newcont[1]) : | |
newcont[0] = "*Ticket %s* : %s %s" % (newcont[0], newcont.pop (1), | |
shorturl) | |
else : | |
newcont[0] = "*Ticket %s* : Created: %s" % (newcont[0], shorturl) | |
for n in range (1, len (newcont)) : | |
newcont[n] = "> %s" % newcont[n] | |
ofile.write("newcont: %r" % newcont) | |
data = {"channel": SLACK_CHANNEL, | |
"username": SLACK_USERNAME, | |
"text": "%s\n"% ("\n".join(newcont)), | |
"icon_emoji": SLACK_EMOJI | |
} | |
ofile.write("got mail\n") | |
ofile.write(json.dumps(data,indent=2)) | |
r = requests.post(SLACK_URL, data={"payload":json.dumps(data)}) | |
ofile.write("r.status %r r.text %r " % (r.status_code, r)) | |
ofile.write("\n\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment