Subscribe to notifications about demolitions scheduled nearby via SMS
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
db
sandboxes
.gitignore
README.md
contact.py
daily-alerts-twilio.sh
geocoder.py
main.py
message.py
requirements.txt
sample.env
send_alerts.py
test_contact.py

README.md

demo-alerts

Let's help people find houses nearby that are planned to be demolished and share health tips via SMS.

Try it by texting a street address in Detroit to 313-254-DEMO (aka 313-254-3366). A street address should look like "9385 E Vernor" or "2 Woodward", no need to include the city, state or zip code.

We locate the address you send us against our geocoder and then populate the text response by querying two datasets on our Open Data Portal:

  • Upcoming Demolitions: Properties with a contractor and scheduled knock-down date
  • Demolition Pipeline: Properties with a completed asbestos abatement survey and projected for demo within the year

We'll text you back with information about whether or not nearby houses are planned for demolition. "Nearby" is within 500ft, or about 1.5 blocks. You can opt-into notifications three days before houses nearby are scheduled to be knocked down.

If you call this number, we'll play back an instructional voice recording.

Install

Requires Python 3.6

git clone https://github.com/CityOfDetroit/demo-alerts.git
cd demo-alerts
pip install -r requirements.txt

We store our subscribers in a SQLite database. Create a subscribers table like so:

import sqlite3

conn = sqlite3.connect("db/sample.sqlite")
c = conn.cursor()

c.execute("CREATE TABLE subscribers (active integer, phone text, matched_address text, location text, subscribed_date text, last_alert_date text)")

conn.commit()
conn.close()

The SQLite Manager extension for Firefox is handy here.

Development

  1. Copy sample.env to .env and add your secrets. We have autoenv installed to auto-execute this file
  2. Run python main.py to start the server
  3. Download ngrok. In another terminal tab, change directories to your unzipped download and run ./ngrok http <your-port-number> to forward the server to <some-subdomain>.ngrok.io
  4. Login to Twilio. Go to Console > Phone Numbers.
  5. Set Messaging to "Configure with Webhooks, or TwiML Bins or Functions" and "A Message Comes In" to your ngrok webhook
  6. Set Voice & Fax to "Configure with Webhooks, or TwiML Bins or Functions" and "A Call Comes In" to a TwiML Bin (twilio.com/console/dev-tools/twiml-bins) that looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Play>https://some-file.mp3</Play>
</Response>
  1. Text or call your number to trigger the Express app!

Send the alerts

Once a day, we want to send our active subsribers a notice if there are demos scheduled nearby them in the next three days.

In development, you can just run: python send_alerts.py

For production, we shedule this to run as a cron job on a remote Linux server:

  1. Edit daily-alets-twilio.sh and replace /path/to/ with your full directories
  2. Edit send_alerts.py with the full path to your database, around line 16
  3. Add a new cron task: crontab -e
  4. Configure the job to run at 11am every day and log errors to a local file:
0 11 * * * /bin/bash /path/to/demo-alerts/daily-alerts-twilio.sh >> /path/to/cron-output.log 2>&1
  1. List all cron jobs to confirm it's scheduled: crontab -l