Skip to content

Instantly share code, notes, and snippets.

@justinvw
Last active December 26, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinvw/7092090 to your computer and use it in GitHub Desktop.
Save justinvw/7092090 to your computer and use it in GitHub Desktop.
Function that can be used to send a notification message to a HipChat room
import json
import requests
def send_message(auth_token, room, message, html=True, color='yellow',
notify=False):
"""Sends a notification message to a HipChat room.
For more information see HipChat's API V2 documentation:
https://www.hipchat.com/docs/apiv2/method/send_room_notification
:param auth_token: The Room Notification Token
:type auth_token: str
:param room: The name or id of the room
:type room: int or str
:param message: the HTML or text message to send
:type message: str
:param html: Determines if the format of the ``message`` is HTML
or plain text
:type html: bool
:param color: The background color for the message
:type color: str
:param notify: Whether or not this message should trigger a
notification for people in the room (sound, color, etc.)
:type notify: bool
"""
available_colors = ['yellow', 'red', 'green', 'purple', 'gray']
if color not in available_colors:
raise ValueError('\'%s\' is not a valid color, possible colors are: %s'
% (color, ', '.join(available_colors)))
url = 'https://api.hipchat.com/v2/room/%s/notification' % room
payload = {
'message': message,
'message_format': 'html' if html else 'text',
'color': color,
'notify': notify
}
r = requests.post(url, params={'auth_token': auth_token},
headers={'Content-Type': 'application/json'},
data=json.dumps(payload))
r.raise_for_status()
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment