public
Last active

Due Date Notifier - Notify VIA SMS (SendHub) when a bill is going to be due

  • Download Gist
due_dates.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#!/usr/bin/env python
# Due Date Notifier - Notify VIA SMS (SendHub) when a bill is going to be due
# * Requires a SendHub account (free) - http://sendhub.com
# * 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 - http://github.com/glenbot
# GIST: https://gist.github.com/4445947
import simplejson
import requests
import logging
from datetime import datetime
 
logging.basicConfig(level=logging.ERROR)
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
BILLS = (
('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
# https://www.sendhub.com/settings
SENDHUB_USERNAME = '2815555555' # 10 digit number you signed up with
SENDHUB_API_KEY = 'xxxxxxx' # api key from settings page
SENDHUB_API_URL = 'https://api.sendhub.com/v1'
 
 
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,
'username': SENDHUB_USERNAME
}
 
if _type == 'GET':
# inject the api key, and username in the query string
payload.update(validation)
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 requests.post(
url,
params=validation,
data=simplejson.dumps(payload),
headers=headers
)
 
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 = datetime.now().day
 
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" % (
bill_name,
days_until_due
)
notifications.append(sms)
 
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:
contacts_to_send_to.append(sendhub_contact['id'])
 
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' % (
response.status_code
))
else:
log.debug('Could not find any contacts to send to')
 
 
if __name__ == '__main__':
notifications = get_notifications()
send_notifications(notifications)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.