Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Due Date Notifier - Notify VIA SMS (SendHub) when a bill is going to be due
#!/usr/bin/env python
# Due Date Notifier - Notify VIA SMS (SendHub) when a bill is going to be due
# * Requires a SendHub account (free) -
# * Has python package dependencies: simplejson, requests
# * Recommend putting this on a crob job running once a day
# I like my text messages at at 11am
# NOTE: Sendhub free accounts allow only 500 requests to the API per month.
# This should suffice assuming the amount of bills you pay arent loco.
# Author: Glen Zangirolami -
import simplejson
import requests
import logging
from datetime import datetime
log = logging.getLogger(__name__)
# List of bills in the following format:
# (bill_name, day_of_month, [notify_x_days_before, notify_x_days_before, ...])
# Example: ('Macys Credit Card', 5, [7,3,1]) will notify you that
# the Macys card is due 7, 3, and 1 day before the 5th of any month
('My Bill #1', 5, [7, 3, 1]),
('My Bill #2', 7, [7, 3, 1]),
('My Bill #4', 12, [7, 3, 1]),
# Phone numbers of people you want to send the text message to
# in the format "+12815555555"
SEND_SMS_TO = ["+12815555555"] # change me to a real number
# SendHub API settings
SENDHUB_USERNAME = '2815555555' # 10 digit number you signed up with
SENDHUB_API_KEY = 'xxxxxxx' # api key from settings page
def sendhub_request(endpoint, payload={}, _type='GET'):
"""Make a request to the sendhub API"""
# create the API url
url = '%s/%s/' % (SENDHUB_API_URL, endpoint)
validation = {
'api_key': SENDHUB_API_KEY,
if _type == 'GET':
# inject the api key, and username in the query string
log.debug('HTTP %s to %s with payload %s' % (_type, url, payload))
return requests.get(url, params=payload)
if _type == 'POST':
headers = {'content-type': 'application/json'}
log.debug('HTTP %s to %s with payload %s' % (_type, url, payload))
return None
def sendhub_get_contacts():
"""Get a list of contacts from the sendhub API"""
request = sendhub_request('contacts')
if request and request.status_code == 200:
data = simplejson.loads(request.content)
return data['objects']
return []
def sendhub_send_sms(data):
"""Send an SMS text"""
return sendhub_request('messages', data, 'POST')
def get_notifications():
"""Run through the dates and build a notification list"""
# placeholder for all notifications
notifications = []
# get the current day of the month
day_of_month =
for bill in BILLS:
# unpack the bill data
bill_name, bill_day, bill_notify = bill
# see if we need to send a notification
if day_of_month < bill_day:
days_until_due = bill_day - day_of_month
if days_until_due in bill_notify:
sms = "A reminder that your %s is due in %d days" % (
return notifications
def send_notifications(notifications):
"""Send the notifications to the receipients in SEND_SMS_TO"""
sendhub_contacts = sendhub_get_contacts()
contacts_to_send_to = []
# parse a list of contacts to send to, we aren't using
# groups from sendhub here although the code is easy enough
# to modify to use groups
for sendhub_contact in sendhub_contacts:
if sendhub_contact['number'] in SEND_SMS_TO:
if contacts_to_send_to:
for notification in notifications:
data = {
'contacts': contacts_to_send_to,
'text': notification
log.debug('Sending payload %s to sendhub' % data)
response = sendhub_send_sms(data)
if response.status_code != 201:
log.error('Could not send text message, HTTP Status %s' % (
log.debug('Could not find any contacts to send to')
if __name__ == '__main__':
notifications = get_notifications()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment