Skip to content

Instantly share code, notes, and snippets.

@justinvoss
Created June 28, 2014 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinvoss/491692e3066d57875232 to your computer and use it in GitHub Desktop.
Save justinvoss/491692e3066d57875232 to your computer and use it in GitHub Desktop.
An example of how to use Twilio to set up a bare-bones voicemail line for show listeners to call in to.
#!/usr/bin/env python
import os
from flask import Flask
from flask import request
import twilio.twiml
import smtplib
from email.mime.text import MIMEText
EMAIL_SERVER = os.environ['EMAIL_SERVER']
EMAIL_USERNAME = os.environ['EMAIL_USERNAME']
EMAIL_PASSWORD = os.environ['EMAIL_PASSWORD']
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
resp = twilio.twiml.Response()
resp.say('Thank you for calling the podcast voicemail line. Please leave your message after the tone.')
resp.record(maxLength='120', action='/record')
return str(resp)
@app.route('/record', methods=['GET', 'POST'])
def handle_recording():
recording_url = request.values.get('RecordingUrl', None)
resp = twilio.twiml.Response()
if recording_url is not None:
resp.say('Thank you for your message.')
caller_number = request.values.get('From', '(unknown)')
from_address = 'Voicemail <nobody@example.com>'
to_address = 'you@example.com'
email_subject = 'New voicemail from {0}'.format(caller_number)
email_message = 'A new voicemail has been received: {0}'.format(recording_url)
try:
s = smtplib.SMTP(EMAIL_SERVER, 587)
s.login(EMAIL_USERNAME, EMAIL_PASSWORD)
message = MIMEText(email_message)
message['Subject'] = email_subject
message['From'] = from_address
message['To'] = to_address
s.sendmail(from_address, [to_address,], message.as_string())
except Exception, e:
# even if we can't send the email, not to worry, since Twilio will still save the MP3 on our behalf.
print e
finally:
s.quit()
else:
resp.say('There was a problem with your voicemail.')
resp.say('Goodbye.')
return str(resp)
if __name__ == '__main__':
app.run(debug=True, port=4000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment