From 7bb17cefade18d043c4d783d7467255d5e3f9464 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?=
Date: Wed, 30 Sep 2015 22:19:59 +0300
Subject: [PATCH] Add Slack integration
---
hc/api/models.py | 16 +++++++++++++++-
static/js/channels.js | 1 +
templates/front/channels.html | 7 +++++++
templates/front/welcome.html | 5 +++--
templates/slack_message.html | 8 ++++++++
5 files changed, 34 insertions(+), 3 deletions(-)
create mode 100644 templates/slack_message.html
diff --git a/hc/api/models.py b/hc/api/models.py
index 505c3636..b6f5b7c9 100644
--- a/hc/api/models.py
+++ b/hc/api/models.py
@@ -9,6 +9,7 @@ from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.db import models
+from django.template.loader import render_to_string
from django.utils import timezone
import requests
@@ -19,7 +20,7 @@ STATUSES = (("up", "Up"), ("down", "Down"), ("new", "New"))
DEFAULT_TIMEOUT = td(days=1)
DEFAULT_GRACE = td(hours=1)
CHANNEL_KINDS = (("email", "Email"), ("webhook", "Webhook"),
- ("pd", "PagerDuty"))
+ ("slack", "Slack"), ("pd", "PagerDuty"))
class Check(models.Model):
@@ -123,6 +124,19 @@ class Channel(models.Model):
pass
n.save()
+ elif self.kind == "slack":
+ text = render_to_string("slack_message.html", {"check": check})
+ payload = {
+ "text": text,
+ "username": "healthchecks.io",
+ "icon_url": "https://healthchecks.io/static/img/logo@2x.png"
+ }
+
+ r = requests.post(self.value, data=json.dumps(payload))
+
+ n.status = r.status_code
+ n.save()
+
elif self.kind == "pd":
if check.status == "down":
event_type = "trigger"
diff --git a/static/js/channels.js b/static/js/channels.js
index 8a39f72c..7ed85e34 100644
--- a/static/js/channels.js
+++ b/static/js/channels.js
@@ -2,6 +2,7 @@ $(function() {
var placeholders = {
email: "address@example.org",
webhook: "http://",
+ slack: "https://hooks.slack.com/...",
pd: "service key"
}
diff --git a/templates/front/channels.html b/templates/front/channels.html
index 703bf295..d975a4f6 100644
--- a/templates/front/channels.html
+++ b/templates/front/channels.html
@@ -21,6 +21,7 @@
{% if ch.kind == "email" %} Email {% endif %}
{% if ch.kind == "webhook" %} Webhook {% endif %}
+ {% if ch.kind == "slack" %} Slack {% endif %}
{% if ch.kind == "pd" %} PagerDuty {% endif %}
@@ -68,6 +69,7 @@
Email
Webhook
+ Slack
PagerDuty
@@ -103,6 +105,11 @@
down and will resolve it
when same check goes up
+
+ Healthchecks.io will post to a Slack channel when
+ a check goes
+ up or down .
+
diff --git a/templates/front/welcome.html b/templates/front/welcome.html
index 99c3acd2..03ba065a 100644
--- a/templates/front/welcome.html
+++ b/templates/front/welcome.html
@@ -222,8 +222,9 @@
- You can specify additional email addresses, webhooks
- and PagerDuty
+ You can specify additional email addresses, webhooks,
+ PagerDuty
+ and Slack
accounts to send notifications to.
diff --git a/templates/slack_message.html b/templates/slack_message.html
new file mode 100644
index 00000000..cfdd8afa
--- /dev/null
+++ b/templates/slack_message.html
@@ -0,0 +1,8 @@
+{% load humanize %}
+
+{% if check.status == "down" %}
+The check "{{ check.name_then_code }}" is DOWN.
+Last ping was {{ check.last_ping|naturaltime }}
+{% else %}
+The check "{{ check.name_then_code }}" received a ping and is now UP.
+{% endif %}