Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a send_notification method for notification-only tokens (issue #46) to the HypChat class. #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion hypchat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import datetime
import six
from .requests import Requests, BearerAuth, HttpTooManyRequests
from .restobject import Linker
from .restobject import Linker, prepare_notification_data


class RateLimitWarning(Warning):
Expand Down Expand Up @@ -155,3 +155,12 @@ def get_user(self, id_or_email, **kwargs):

def get_emoticon(self, id_or_shortcut, **kwargs):
return self.fromurl('{0}/v2/emoticon/{1}'.format(self.endpoint, id_or_shortcut), **kwargs)

def send_notification(self, id_or_name, message, notify=False, format=None, **kwargs):
"""
Send a message to a room.
This version is for notification-only add-ons and tokens generated for that purpose.
"""
data = prepare_notification_data(message, notify, format, **kwargs)
url = '{0}/v2/room/{1}'.format(self.endpoint, id_or_name)
self._requests.post(url + '/notification', data=data)
28 changes: 19 additions & 9 deletions hypchat/restobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ def delete(self):
_at_mention = re.compile('@[\w]+(?: |$)')


def prepare_notification_data(message, notify=False, format=None, **kwargs):
"""Process arguments to build the request body
Cards not supported. see https://www.hipchat.com/docs/apiv2/method/send_room_notification for additional properties
Note that from needs to be passed as _from to avoid python keyword conflict
"""
properties = {'_from': 'from', 'attach_to': 'attach_to', 'color': 'color'}
if not format:
if len(_at_mention.findall(message)) > 0:
format = 'text'
else:
format = 'html'
data = {'message': message, 'notify': notify, 'message_format': format}
for prop, value in kwargs.items():
data[properties[prop]] = value if prop in properties.keys() else None
return data


class Room(RestObject):
def __init__(self, *p, **kw):
super(Room, self).__init__(*p, **kw)
Expand All @@ -147,18 +164,11 @@ def message(self, message):
data = {'message': message}
self._requests.post(self.url + '/message', data=data)

def notification(self, message, color=None, notify=False, format=None):
def notification(self, message, notify=False, format=None, **kwargs):
"""
Send a message to a room.
"""
if not format:
if len(_at_mention.findall(message)) > 0:
format = 'text'
else:
format = 'html'
data = {'message': message, 'notify': notify, 'message_format': format}
if color:
data['color'] = color
data = prepare_notification_data(message, notify, format, **kwargs)
self._requests.post(self.url + '/notification', data=data)

def topic(self, text):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

from distutils.core import setup, Command
from setuptools import setup, Command



class PyTest(Command):
Expand Down