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 @@
+ 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 @@ 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 %}