Permalink
Browse files

SMSr app working to send SMS messages via Twilio and store phone numb…

…er/message in the DB.
  • Loading branch information...
akavlie committed Oct 2, 2010
0 parents commit 4fce90c3aa0e052499e617017f09cfceda91e698
@@ -0,0 +1,3 @@
+from sms import app
+app.run(debug=True)
+
@@ -0,0 +1,6 @@
+from flask import Flask
+
+app = Flask(__name__)
+app.config.from_object('sms.config')
+
+import sms.views
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,25 @@
+from sms import app
+from flaskext.sqlalchemy import SQLAlchemy
+
+db = SQLAlchemy(app)
+
+class Sent(db.Model):
+ """ Record of a sent message """
+ __tablename__ = 'sent_messages'
+ id = db.Column(db.Integer, primary_key=True)
+ phone = db.Column(db.Integer)
+ date = db.Column(db.Date)
+ message = db.Column(db.String)
+ status = db.Column(db.String)
+
+ def __init__(self, phone, message):
+ self.phone = phone
+ self.message = message
+
+class Message(db.Model):
+ """ Frequently used SMS messages """
+ __tablename__ = 'frequent_smses'
+ id = db.Column(db.Integer, primary_key=True)
+ description = db.Column(db.String)
+ message = db.Column(db.String)
+
Binary file not shown.
@@ -0,0 +1,18 @@
+body {
+ font-family: sans-serif;
+}
+
+#page {
+ margin: 50px 200px;
+}
+
+h1 {
+ font-size: 30pt;
+}
+
+.flash {
+ background: #CEE5F5;
+ padding: 0.5em;
+ border: 1px solid #AACBE2;
+}
+
@@ -0,0 +1,36 @@
+{% extends 'layout.html' %}
+
+{% block body %}
+
+<form action="/sms" method="post">
+ <p><label for="phone">Phone Number: </label>
+ <input type="text" name="phone_number" /></p>
+ <p><label for="phone_number">Message: </label>
+ <input type="text" name="message" />
+ <input type="submit" value="Send" /></p>
+</form>
+
+<div id="sent_messages">
+ {% if sent_messages %}
+ <table>
+ <tr>
+ <th>Time</th>
+ <th>Phone Number</th>
+ <th>Message</th>
+ <th>Status</th>
+ </tr>
+ {% for message in sent_messages %}
+ <tr>
+ <td>{{ message.date }}</td>
+ <td>{{ message.phone }}</td>
+ <td>{{ message.message }}</td>
+ <td>{{ message.status }}</td>
+ </tr>
+ {% endfor %}
+ </table>
+ {% else %}
+ No messages yet!
+ {% endif %}
+</div>
+
+{% endblock %}
@@ -0,0 +1,20 @@
+<!doctype HTML>
+<head>
+ <title>SMSr</title>
+ <link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
+</head>
+
+<body>
+<div id=page>
+ <h1>SMSr</h1>
+
+ {% for msg in get_flashed_messages() %}
+ <div class=flash>
+ {{ msg }}
+ </div>
+ {% endfor %}
+
+ {% block body %}{% endblock %}
+</div>
+</body>
+
@@ -0,0 +1,15 @@
+from sms import app
+import twilio
+
+def twilio_send(phone_number, message):
+ """Sends an SMS via the Twilio service. """
+
+ data = {'From': app.config['CALLER_ID'],
+ 'To': phone_number,
+ 'Body': message}
+
+ account = twilio.Account(app.config['ACCOUNT_SID'],
+ app.config['ACCOUNT_TOKEN'])
+ account.request('/%s/Accounts/%s/SMS/Messages' % (app.config['API_VERSION'],
+ app.config['ACCOUNT_SID']),
+ 'POST', data)
Binary file not shown.
@@ -0,0 +1,22 @@
+Copyright (c) 2009 Twilio, Inc.
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
+README.markdown
+setup.py
+twilio.py
@@ -0,0 +1,31 @@
+## Python Twilio Helper Library
+
+### Description
+The Twilio REST SDK simplifies the process of makes calls to the Twilio REST.
+The Twilio REST API lets to you initiate outgoing calls, list previous call,
+and much more. See http://www.twilio.com/docs for more information.
+
+### Installation
+
+ $ sudo pip install twilio
+
+### Manual Installation
+Download the source and run
+
+ $ python setup.py install
+
+### Usage
+To use the Twilio library, just 'import twilio' in the your current python
+file. As shown in example-rest.py, you will need to specify the ACCOUNT_ID and
+ACCOUNT_TOKEN given to you by Twilio before you can make REST requests. In
+addition, you will need to choose a 'To' and 'From' before making
+outgoing calls. See http://www.twilio.com/docs for more information.
+
+### Files
+ * **twilio.py**: include this library in your code
+ * **examples/example-rest.py**: example usage of REST
+ * **examples/example-twiml.py**: example usage of the TwiML generator
+ * **examples/example-utils.py**: example usage of utilities
+
+### License
+The Twilio Python Helper Library is distributed under the MIT License
@@ -0,0 +1,74 @@
+#!/usr/bin/env python
+
+import twilio
+
+# Twilio REST API version
+API_VERSION = '2010-04-01'
+
+# Twilio AccountSid and AuthToken
+ACCOUNT_SID = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
+ACCOUNT_TOKEN = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
+
+# Outgoing Caller ID previously validated with Twilio
+CALLER_ID = 'NNNNNNNNNN';
+
+# Create a Twilio REST account object using your Twilio account ID and token
+account = twilio.Account(ACCOUNT_SID, ACCOUNT_TOKEN)
+
+# ===========================================================================
+# 1. Initiate a new outbound call to 415-555-1212
+# uses a HTTP POST
+d = {
+ 'From' : CALLER_ID,
+ 'To' : '415-555-1212',
+ 'Url' : 'http://demo.twilio.com/welcome',
+}
+try:
+ print account.request('/%s/Accounts/%s/Calls' % \
+ (API_VERSION, ACCOUNT_SID), 'POST', d)
+except Exception, e:
+ print e
+ print e.read()
+
+# ===========================================================================
+# 2. Get a list of recent completed calls (i.e. Status = 2)
+# uses a HTTP GET
+d = { 'Status':2, }
+try:
+ print account.request('/%s/Accounts/%s/Calls' % \
+ (API_VERSION, ACCOUNT_SID), 'GET', d)
+except Exception, e:
+ print e
+ print e.read()
+
+# ===========================================================================
+# 3. Get a list of recent notification log entries
+# uses a HTTP GET
+try:
+ print account.request('/%s/Accounts/%s/Notifications' % \
+ (API_VERSION, ACCOUNT_SID), 'GET')
+except Exception, e:
+ print e
+ print e.read()
+
+# ===========================================================================
+# 4. Get a list of audio recordings for a certain call
+# uses a HTTP GET
+d = { 'CallSid':'CA0c7001f3f3f5063b7f7d96def0f1ed00', }
+try:
+ print account.request('/%s/Accounts/%s/Recordings' % \
+ (API_VERSION, ACCOUNT_SID), 'GET', d)
+except Exception, e:
+ print e
+ print e.read()
+
+# ===========================================================================
+# 5. Delete a specific recording
+# uses a HTTP DELETE, no response is returned when using DELETE
+try:
+ account.request( \
+ '/%s/Accounts/%s/Recordings/RE4e75a0b62a5c52e5cb96dc25fb4101d9' % \
+ (API_VERSION, ACCOUNT_SID), 'DELETE')
+except Exception, e:
+ print e
+ print e.read()
@@ -0,0 +1,97 @@
+#!/usr/bin/env python
+"""
+The TwiML Python Response Library makes it easy to write TwiML without having
+to touch XML. Error checking is built in to help preventing invalid markup.
+
+USAGE:
+To create TwiML, you will make new TwiML verbs and nest them inside another
+TwiML verb. Convenience methods are provided to simplify TwiML creation.
+
+SUPPORTED VERBS:
+ Response
+ Say
+ Play
+ Dial
+ Gather
+ Hangup
+ Redirect
+ Record
+ Pause
+ Number
+ Conference
+ Sms
+"""
+
+import twilio
+
+# ===========================================================================
+# Using Say, Dial, and Play
+r = twilio.Response()
+r.append(twilio.Say("Hello World", voice=twilio.Say.MAN,
+ language=twilio.Say.FRENCH, loop=10))
+r.append(twilio.Dial("4155551212", timeLimit=45))
+r.append(twilio.Play("http://www.mp3.com"))
+print r
+
+""" outputs:
+<Response>
+ <Say voice="man" language="fr" loop="10">Hello World</Say>
+ <Dial timeLimit="45">4155551212</Dial>
+ <Play>http://www.mp3.com</Play>
+</Response>
+"""
+
+# The same XML can be created above using the convenience methods
+r = twilio.Response()
+r.addSay("Hello World", voice=twilio.Say.MAN, language=twilio.Say.FRENCH,
+ loop=10)
+r.addDial("4155551212", timeLimit=45)
+r.addPlay("http://www.mp3.com")
+print r
+
+
+# ===========================================================================
+# Using Gather, Redirect
+r = twilio.Response()
+g = r.append(twilio.Gather(numDigits=1))
+g.append(twilio.Say("Press 1"))
+r.append(twilio.Redirect())
+print r
+
+""" outputs:
+<Response>
+ <Gather numDigits="1">
+ <Say>Press 1</Say>
+ </Gather>
+ <Redirect/>
+</Response>
+"""
+
+# ===========================================================================
+# Adding a Say verb multiple times
+r = twilio.Response()
+s = twilio.Say("Press 1")
+r.append(s)
+r.append(s)
+print r
+
+""" outputs:
+<Response>
+ <Say>Press 1</Say>
+ <Say>Press 1</Say>
+</Response>
+"""
+
+# ===========================================================================
+# You may want to add an attribute to a verb that the library doesn't support.
+# To set arbitrary attribute / value pairs, just include the new attribute
+# as a named parameter
+r = twilio.Response()
+r.append(twilio.Redirect(crazy="delicious"))
+print r
+
+""" outputs:
+<Response>
+ <Redirect crazy="delicious"/>
+</Response>
+"""
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+
+import twilio
+
+# Twilio AccountSid and AuthToken
+ACCOUNT_SID = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
+ACCOUNT_TOKEN = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
+
+# Create a Twilio utility object using your Twilio account ID and token
+utils = twilio.Utils(ACCOUNT_SID, ACCOUNT_TOKEN)
+
+# ===========================================================================
+# 1. Validate a Twilio request
+
+# the URL and POST parameters would typically be provided by the web
+# framework that is recieving the request from Twilio (e.g. Django)
+url = "http://UUUUUUUUUUUUUUUUUU"
+postvars = {}
+
+# the request from Twilio also includes the HTTP header: X-Twilio-Signature
+# containing the expected signature
+signature = "SSSSSSSSSSSSSSSSSSSSSSSSSSSS"
+
+print "The request from Twilio to %s with the POST parameters %s " % \
+ (url, postvars)
+if utils.validateRequest(url, postVar, signature):
+ print "was confirmed to have come from Twilio."
+else:
+ print "was NOT VALID. It might have been spoofed!"
Oops, something went wrong.

0 comments on commit 4fce90c

Please sign in to comment.