Last active
May 27, 2016 18:43
-
-
Save jmandel/b7732bab9a5b678e75c09ad3c12163f3 to your computer and use it in GitHub Desktop.
FHIR Zulip Bots
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/env python2.7 | |
from __future__ import print_function | |
import sys | |
import os | |
import optparse | |
import json | |
import random | |
import zulip | |
zulip_client = zulip.Client( | |
site='https://chat.fhir.org', | |
api_key=os.environ['ZULIP_API_KEY'], | |
email=os.environ['ZULIP_EMAIL']) | |
def handle_message(m): | |
if m['content'].lower().startswith("!weather boston"): | |
zulip_client.send_message({ | |
"type": m['type'], | |
"content": str('very rainy and cold'), | |
"subject": m['subject'], | |
"to": m['display_recipient'], | |
}) | |
if m['content'].startswith("!dice"): | |
zulip_client.send_message({ | |
"type": m['type'], | |
"content": str(random.choice(range(1,1+6))), | |
"subject": m['subject'], | |
"to": m['display_recipient'], | |
}) | |
zulip_client.call_on_each_message(handle_message) |
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
import StringIO | |
import unicodecsv | |
import requests | |
import zulip | |
import time | |
import os | |
login = {'username':'fhir_bot','password':os.environ['GFORGE_PASSWORD']} | |
session = requests.Session() | |
zulip_client = zulip.Client( | |
site='https://chat.fhir.org', | |
api_key=os.environ['ZULIP_API_KEY'], | |
email=os.environ['ZULIP_EMAIL']) | |
def read_issues(s): | |
s.post('http://gforge.hl7.org/gf/account/?action=LoginAction',data=login) | |
changes = s.get('http://gforge.hl7.org/gf/project/fhir/tracker/?action=TrackerQueryCSV&tracker_id=677&tracker_query_id=143') | |
reader = unicodecsv.reader(StringIO.StringIO(changes.text.encode("utf-8")), encoding='utf-8') | |
reader.next() | |
return { | |
int(row[0]): (int(row[0]), row[1], row[4], row[5]) for row in reader | |
} | |
def post_issue(issue): | |
zulip_client.send_message({ | |
"type": 'stream', | |
"content": "GF#%s: **%s** posted by `%s`" % ( | |
issue[0], | |
issue[1], | |
issue[2]), | |
"subject": "tracker-item", | |
"to": "committers", | |
}) | |
posted_issues = read_issues(session) | |
while True: | |
issues = read_issues(session) | |
for issue_number, issue in issues.iteritems(): | |
if issue_number > max(posted_issues.keys()): | |
post_issue(issue) | |
posted_issues.update(issues) | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment