Receiving & Sending Texts Using Tropo & the Python Framework Django
September 22nd, 2011 by kbondDjango is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. This blog will discuss how to implement Django with Python to receive and reply to a text message – note that installation instructions will be Mac oriented, if you use Windows you can find some alternate install instructions here.
Requirements:
1)Tunnlr - used to simulate a web server, so you can test from a desktop
-signup to Tunnlr here
-detailed instructions on setup can be found here
2)Python Library
-Download in the terminal:
$git clone https://github.com/tropo/tropo-webapi-python.git
-Or download directly from github.com here
3)Django
-Download Django-1.3.1.tar.gz
Next, in the terminal, execute the following
$cd Django-1.3.1
$sudo python setup.py install
Follow the above instructions to install/setup Tunnlr and Django; once they’re ready, go into whatever directory you want the Django/Tropo app to reside in (Sites/Django_Apps, for example. Once you’re in your chosen directory, go ahead and download the Python library mentioned above. With everything installed and downloaded, enter this in the terminal:
$django-admin.py startproject mysite
This will create a directory that looks like this:
mysite/
__init__.py
manage.py
settings.py
urls.py
The file “urls.py” is the only one that needs to be edited in this directory. Open this file and insert this:
from django.conf.urls.defaults import *
from hello.views import hello
urlpatterns = patterns('',
('^hello/$', hello)
)
This sets the URL that calls the app to something like this http://server_example.com/hello/, and sets it to specifically call the method “hello” in the actual Tropo app.
On to the meat – while in the mysite directory, enter the following in the terminal:
$python manage.py startapp hello
This will open another directory with four more files as shown below:
hello/
__init__.py
models.py
tests.py
views.py
The Tropo application will be stored in the “views.py” file, shown below, which receives a text and replies back to the same number:
# Create your views here.
# sends information back to Tropo
from django.http import HttpResponse
# This will allow you to call Tropo functions
from tropo import Tropo
# This will bypass authorization
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
# hello is the method name called by urls.py
# Also, request is where all of the text's information lies
def hello(request):
t = Tropo()
# This is the initial text that you want to extract
msg = request.POST['msg']
# This sends a text reply back
json = t.say("you just said: " + msg)
# This renders the reply into JSON so Tropo can read it
json = t.RenderJson(json)
# This sends the JSON to Tropo
return HttpResponse(json)
Now that this is complete, go to Tropo and create a new WebAPI app. Define the URL that powers this app as your Tunnlr address (which can be set up and located using the Tunnlr instructions mentioned earlier), followed by the app name. In our case, it would be :
http://web1.tunnlr.com:XXXXX/hello/
Hit Create, then once the screen refreshes with your application, add a U.S. number (needed to send an SMS) and hit Update.
You now want to launch Tunnlr under port 8000, in order for the app to be accessible – instructions on the manual launch of Tunnlr can be found on the same instructions page mentioned earlier. Django comes with its own webserver, which is mostly used for development, and we’re going to use it to access the app. Navigate to the directory mysite, which we were in previously, and enter:
$python manage.py runserver
With the Django webserver running and Tunnlr open and ready, you can now send a text to the phone number you added to your Tropo app earlier – shortly after sending, you should receive a text message back saying “You just said: …..”
That pretty much sums everything up – it’s a basic example without a lot of real world functionality, but we received enough questions on how to implement Django with Tropo that we felt a quick example would be beneficial. You can view both of the created directories on github here.
Note that all of the Tunnlr bits are unnecessary if you have an actual webserver – you would just be running Django there, instead, and using the URL to your server and the “hello” resource.
Related posts:
- Hello, Python World (Django Edition)
- HOWTO: Working with the Python 3 branch of the Tropo Python Web API library
- Updated Version of the Python Module for the Tropo WebAPI
- Add SMS and IM to any existing Tropo application in Ruby, python, PHP, JavaScript or Groovy… today!
- Python Tropo WebAPI Library Now Available in PyPI for Easy Installation
