Created
April 24, 2015 15:32
-
-
Save hmmbug/aec6f5419ff02cb21fab to your computer and use it in GitHub Desktop.
simple python hipchat command
This file contains hidden or 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 python | |
# | |
# A mini hipchat client. Sends a message to a predetermined room. | |
# Requires HypChat (pip install hypchat) | |
# | |
# Mark Hollow. April 2015 | |
# | |
# Config file (hipchat.json), in JSON format: | |
# { | |
# "hipchat": { | |
# "token": "<your hipchat token>", | |
# "room": "<your hipchat room name>" | |
# } | |
# } | |
from hypchat import HypChat | |
from sys import version_info | |
from json import load as json_load | |
CONFIG_LOCATIONS = ['/etc', '.'] | |
if version_info < (2, 7, 9): | |
# Disables SSL cert verification errors for Python < 2.7.9 | |
import requests.packages.urllib3 as urllib3 | |
urllib3.disable_warnings() | |
class HipChatClient(object): | |
def __init__(self, msg, token=None, room=None): | |
self.read_config() | |
if token: | |
self.token = token | |
if room: | |
self.room = room | |
hc = HypChat(self.token) | |
room_obj = hc.get_room(self.room) | |
self.response = room_obj.message(msg) | |
def read_config(self): | |
for cfg_dir in CONFIG_LOCATIONS: | |
try: | |
with open('%s/hipchat.json' % cfg_dir) as fh: | |
config = json_load(fh)['hipchat'] | |
self.token = config['token'] | |
self.room = config['room'] | |
return # exit method | |
except IOError: | |
pass | |
raise IOError('Config file "hipchat.json" not found in %s' | |
% str(CONFIG_LOCATIONS)) | |
if __name__ == '__main__': | |
from sys import argv, stderr | |
def usage(): | |
stderr.write("Usage: %s \"Your message.\"\n" % argv[0]) | |
exit(1) | |
try: | |
HipChatClient(argv[1]) | |
except IndexError: | |
usage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment