Skip to content

Commit

Permalink
[Feature #145028655] Add sms notification when cron job is up or down.
Browse files Browse the repository at this point in the history
- Twilio integration for sending sms alerts.
- Added sms integration option to the integrations page.
  • Loading branch information
davidmukiibi committed May 31, 2017
1 parent ad65940 commit 4409462
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 2 deletions.
Empty file added __init__.py
Empty file.
4 changes: 3 additions & 1 deletion hc/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)
DEFAULT_TIMEOUT = td(days=1)
DEFAULT_GRACE = td(hours=1)
CHANNEL_KINDS = (("email", "Email"), ("webhook", "Webhook"),
CHANNEL_KINDS = (("sms", "SMS"), ("email", "Email"), ("webhook", "Webhook"),
("hipchat", "HipChat"),
("slack", "Slack"), ("pd", "PagerDuty"), ("po", "Pushover"),
("victorops", "VictorOps"))
Expand Down Expand Up @@ -167,6 +167,8 @@ def send_verify_link(self):

@property
def transport(self):
if self.kind == 'sms':
return transports.SMS(self)
if self.kind == "email":
return transports.Email(self)
elif self.kind == "webhook":
Expand Down
14 changes: 14 additions & 0 deletions hc/api/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import requests
from six.moves.urllib.parse import quote
from twilio.rest import Client

This comment has been minimized.

Copy link
@collinmutembei

collinmutembei May 31, 2017

Contributor

Build job 237888670 is failing with the error

ImportError: No module named django_twilio

from hc.lib import emails

Expand Down Expand Up @@ -89,6 +90,19 @@ def post(self, url, json, **kwargs):
def post_form(self, url, data):
return self.request("post", url, data=data)

class SMS(Transport):

def notify(self, check):
if check.status in ["up", "down"]:
message = "Hullo, your {} check is {}!".format(check.name, check.status)
for value in self.channel.value:
to = value
client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
response = client.messages.create(body=message, to=to, from_='(256) 998-9594')

if response.error_message:
return response.error_message


class Webhook(HttpTransport):
def notify(self, check):
Expand Down
1 change: 1 addition & 0 deletions hc/front/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
channel_urls = [
url(r'^$', views.channels, name="hc-channels"),
url(r'^add/$', views.add_channel, name="hc-add-channel"),
url(r'^add_sms/$', views.add_sms, name="hc-add-sms"),
url(r'^add_email/$', views.add_email, name="hc-add-email"),
url(r'^add_webhook/$', views.add_webhook, name="hc-add-webhook"),
url(r'^add_pd/$', views.add_pd, name="hc-add-pd"),
Expand Down
4 changes: 4 additions & 0 deletions hc/front/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ def remove_channel(request, code):

return redirect("hc-channels")

@login_required
def add_sms(request):
ctx = {"page": "channels"}
return render(request, "integrations/add_sms.html", ctx)

@login_required
def add_email(request):
Expand Down
7 changes: 6 additions & 1 deletion hc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
'djmail',

'hc.accounts',
'django_twilio',
'hc.api',
'hc.front',
'hc.payments'
'hc.payments',
)

MIDDLEWARE = (
Expand Down Expand Up @@ -139,6 +140,10 @@
SLACK_CLIENT_ID = None
SLACK_CLIENT_SECRET = None

# sms integration
TWILIO_ACCOUNT_SID = 'ACacce7eab5f5f5018d1c78ba0bfc36aa7'

This comment has been minimized.

Copy link
@collinmutembei

collinmutembei May 31, 2017

Contributor

These configurations are sensitive data, would be best if they were being loaded from environment variables. Look at this resource on how to remove them entirely from the repo.

TWILIO_AUTH_TOKEN = '4b813e4109fe5f91e91568d202de0463'

# Pushover integration -- override these in local_settings
PUSHOVER_API_TOKEN = None
PUSHOVER_SUBSCRIPTION_URL = None
Expand Down
Binary file added static/img/integrations/sms.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions static/js/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ $(function() {
webhook: "http://",
slack: "https://hooks.slack.com/...",
hipchat: "https://api.hipchat.com/...",
sms: "+2547793222",
pd: "service key"
}

Expand Down
9 changes: 9 additions & 0 deletions templates/front/channels.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
{% for ch in channels %}
<tr class="channel-row">
<td>
{% if ch.kind == "sms" %} SMS {% endif %}
{% if ch.kind == "email" %} Email {% endif %}
{% if ch.kind == "webhook" %} Webhook {% endif %}
{% if ch.kind == "slack" %} Slack {% endif %}
Expand Down Expand Up @@ -126,6 +127,14 @@

<h1 class="ai-title">Add More</h1>
<ul class="add-integration">
<li>
<img src="{% static 'img/integrations/sms.png' %}"
class="icon" alt="SMS icon" />

<h2>SMS</h2>
<p>Receive an sms when check goes up or down.</p>
<a href="{% url 'hc-add-sms' %}" class="btn btn-primary">Add Integration</a>
</li>
<li>
<img src="{% static 'img/integrations/slack.png' %}"
class="icon" alt="Slack icon" />
Expand Down
58 changes: 58 additions & 0 deletions templates/integrations/add_sms.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{% extends "base.html" %}
{% load compress humanize staticfiles hc_extras %}

{% block title %}Add mobile numbers to send your notifications to - healthchecks.io{% endblock %}


{% block content %}
<div class="row">
<div class="col-sm-12">
<h1>SMS</h1>

<p>Get an sms message when your check goes up or down.</p>

<p>
<strong>Tip:</strong>
Add multiple phone numbers, to notify multiple team members.
</p>

<p>
<strong>Confirmation needed.</strong>
After entering a phone number(s), healthchecks.io will send out a confirmation link.
Only confirmed numbers will receive notifications.
</p>

<h2>Integration Settings</h2>

<form method="post" class="form-horizontal" action="{% url 'hc-add-channel' %}">
{% csrf_token %}
<input type="hidden" name="kind" value="sms" />
<div class="form-group">
<label for="sms" class="col-sm-2 control-label">Phone number</label>
<div class="col-sm-3">
<input
id="sms"
type="text"
class="form-control"
name="value"
placeholder="2547793222">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Save Integration</button>
</div>
</div>
</form>
</div>
</div>


{% endblock %}

{% block scripts %}
{% compress js %}
<script src="{% static 'js/jquery-2.1.4.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
{% endcompress %}
{% endblock %}

0 comments on commit 4409462

Please sign in to comment.