Skip to content

@glenbot /due_dates.py
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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) - 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.