forked from idooo/pancake-hipchat-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Library inside, minor improvements, release
- Loading branch information
Showing
7 changed files
with
152 additions
and
33 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Pancake Bot (HipChat version) | ||
Pancake chat bot (HipChat edition) | ||
============ | ||
|
||
Features: | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
boto | ||
requests | ||
python-simple-hipchat |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# https://github.com/kurttheviking/python-simple-hipchat | ||
# https://github.com/idooo/python-simple-hipchat | ||
|
||
try: | ||
from urllib.parse import urljoin | ||
from urllib.parse import urlencode | ||
import urllib.request as urlrequest | ||
except ImportError: | ||
from urlparse import urljoin | ||
from urllib import urlencode | ||
import urllib2 as urlrequest | ||
import json | ||
|
||
|
||
API_URL_DEFAULT = 'https://api.hipchat.com/v1/' | ||
FORMAT_DEFAULT = 'json' | ||
|
||
|
||
class HipChat(object): | ||
|
||
limits = {} | ||
|
||
def __init__(self, token=None, url=API_URL_DEFAULT, format=FORMAT_DEFAULT): | ||
self.url = url | ||
self.token = token | ||
self.format = format | ||
self.opener = urlrequest.build_opener(urlrequest.HTTPSHandler()) | ||
|
||
class RequestWithMethod(urlrequest.Request): | ||
def __init__(self, url, data=None, headers={}, origin_req_host=None, unverifiable=False, http_method=None): | ||
urlrequest.Request.__init__(self, url, data, headers, origin_req_host, unverifiable) | ||
if http_method: | ||
self.method = http_method | ||
|
||
def get_method(self): | ||
if self.method: | ||
return self.method | ||
return urlrequest.Request.get_method(self) | ||
|
||
def method(self, url, method="GET", parameters=None, timeout=None): | ||
method_url = urljoin(self.url, url) | ||
|
||
if method == "GET": | ||
if not parameters: | ||
parameters = dict() | ||
|
||
parameters['format'] = self.format | ||
parameters['auth_token'] = self.token | ||
|
||
query_string = urlencode(parameters) | ||
request_data = None | ||
else: | ||
query_parameters = dict() | ||
query_parameters['auth_token'] = self.token | ||
|
||
query_string = urlencode(query_parameters) | ||
|
||
if parameters: | ||
request_data = urlencode(parameters).encode('utf-8') | ||
else: | ||
request_data = None | ||
|
||
method_url = method_url + '?' + query_string | ||
|
||
req = self.RequestWithMethod(method_url, http_method=method, data=request_data) | ||
response = self.opener.open(req, None, timeout) | ||
|
||
info = response.info() | ||
self.limits = { | ||
'limit': int(info.getheaders('X-RateLimit-Limit')[0]), | ||
'remaining': int(info.getheaders('X-RateLimit-Remaining')[0]), | ||
'reset': int(info.getheaders('X-RateLimit-Reset')[0]) | ||
} | ||
|
||
response_data = response.read() | ||
|
||
return json.loads(response_data.decode('utf-8')) | ||
|
||
def list_rooms(self): | ||
return self.method('rooms/list') | ||
|
||
def message_room(self, room_id='', message_from='', message='', message_format='text', color='', notify=False): | ||
parameters = dict() | ||
parameters['room_id'] = room_id | ||
parameters['from'] = message_from[:15] | ||
parameters['message'] = message | ||
parameters['message_format'] = message_format | ||
parameters['color'] = color | ||
|
||
if notify: | ||
parameters['notify'] = 1 | ||
else: | ||
parameters['notify'] = 0 | ||
|
||
return self.method('rooms/message', 'POST', parameters) |