Created
May 20, 2016 08:11
-
-
Save tspycher/658f8a6e15abb6e3b86c8e2724d5ad7d to your computer and use it in GitHub Desktop.
Sends a message to HipChat and allows configuration by parameters
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
from hypchat import HypChat | |
import re | |
import sys | |
import argparse | |
import select | |
from hypchat.requests import HttpClientError | |
parser = argparse.ArgumentParser(description='Simply sends a message to a given room') | |
parser.add_argument('-t', '--token', action='store', dest="apitoken", default='xxxxxx') | |
parser.add_argument('-r', '--room', action='store', dest="room", default='rnd') | |
parser.add_argument('message', nargs='*', default=[]) | |
args = parser.parse_args() | |
apitoken = args.apitoken | |
roomname = args.room | |
message = " ".join(args.message) | |
if select.select([sys.stdin,],[],[],0.0)[0]: | |
message = "".join(sys.stdin.readlines()) | |
message = "<pre>%s</pre>" % message | |
try: | |
hc = HypChat(apitoken) | |
rooms = filter(lambda x: re.match(roomname, x['name'], re.IGNORECASE), hc.rooms()['items']) | |
except HttpClientError as e: | |
sys.stderr.write("Could not connect to HipChat Server (%s)" % e) | |
sys.exit(9) | |
if not rooms: | |
sys.stderr.write("Could not find your room %s" % roomname) | |
sys.exit(9) | |
room = rooms[0] | |
try: | |
room.notification(message=message, color="green", format='html', notify=True) | |
except HttpClientError as e: | |
sys.stderr.write("Could not send Message to HipChat Server (%s)" % e) | |
sys.exit(9) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment