-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicrotivity.py
More file actions
executable file
·73 lines (62 loc) · 2.54 KB
/
microtivity.py
File metadata and controls
executable file
·73 lines (62 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
import json, urllib, time, sys, traceback
from datetime import datetime
from slackclient import SlackClient
from pprint import pprint
import argparse
class Message:
# timestamp: datetime
# channel: string starting with "#"
# text: string
def __init__(self, timestamp, channel, text):
self.timestamp = timestamp
self.channel = channel
self.text = text
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
else:
return False
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return "Message(" + repr(self.timestamp) + ", " + repr(self.channel) + ", " + repr(self.text) + ")"
# Fetches the slack notifications tab of the spreadsheet and returns
# a list of Messages.
def getFromSpreadsheet():
spreadsheetFeed = "https://spreadsheets.google.com/feeds/list/1bM_2amIChF2cn29b3AyF1x5TVVy4e_95TvkbpnEVRFc/ojh9cqh/public/values?alt=json"
response = urllib.urlopen(spreadsheetFeed)
spreadsheetData = json.loads(response.read())
#pprint(spreadsheetData)
messages = []
for entry in spreadsheetData["feed"]["entry"]:
try:
timestamp = datetime.strptime(entry["gsx$timestamp"]["$t"], '%m/%d/%Y %H:%M:%S')
except ValueError:
timestamp = datetime.fromtimestamp(0)
channel = entry["gsx$channel"]["$t"]
text = entry["gsx$text"]["$t"]
messages.append(Message(timestamp, channel, text))
return messages
def sendToSlack(sc, messages):
for m in messages:
sc.api_call("chat.postMessage", channel=m.channel, text=m.text, username="workflowbot", icon_emoji=":robot_face:")
def main():
parser = argparse.ArgumentParser(description="Forward notifications from Google spreadsheet to Slack")
parser.add_argument('token', nargs=1, help="Slack authentication test token; to get yours, go to https://api.slack.com/docs/oauth-test-tokens")
args = parser.parse_args()
sc = SlackClient(args.token)
previousMessages = getFromSpreadsheet()
while True:
time.sleep(2)
try:
latestMessages = getFromSpreadsheet()
newMessages = [m for m in latestMessages if m not in previousMessages]
if len(newMessages) > 0:
print "sending new messages " + str(newMessages)
sendToSlack(sc, newMessages)
previousMessages = latestMessages
except:
traceback.print_exc()
# but keep running
main()