-
-
Save willwade/c8e5c186129db58aac77 to your computer and use it in GitHub Desktop.
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
#coding: utf-8 | |
import json | |
import requests | |
class Slack(object): | |
""" | |
References: | |
========== | |
* Incoming Webkooks: https://api.slack.com/ | |
* Bootstrap Colors: http://getbootstrap.com/css/#less-variables | |
""" | |
PRIMARY = '#428bca' # Blue | |
SUCCESS = '#5cb85c' # Green | |
INFO = '#5bc0de' # Light blue | |
WARNING = '#f0ad4e' # Orange | |
DANGER = '#d9534f' # Red | |
def __init__(self, webhook_url, channel=None): | |
self.webhook_url = webhook_url | |
self.channel = channel | |
def _send(self, data): | |
msg = {} | |
if self.channel: | |
msg['channel'] = self.channel | |
msg.update(data) | |
return requests.post(self.webhook_url, data=json.dumps(msg)) | |
def send(self, message): | |
return self._send({"text": message}) | |
def send_rich(self, pretext, color, author_name, author_link, author_icon, title, title_link, text, fields, fallback=None): | |
""" | |
:param color: Slack.{GOOD, WARNING, DANGER} | |
:param fields: list of {"title": "", "value": ""} | |
""" | |
return self._send({ | |
"attachments": [{ | |
"fallback": fallback if fallback else pretext, | |
"pretext": pretext, | |
"color": color, | |
"author_name": author_name, | |
"author_link": author_link, | |
"author_icon": author_icon, | |
"title": title, | |
"title_link": title_link, | |
"text": text, | |
"fields": fields | |
}] | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment