How to Build an SMS Service

Using Twitter to Build a SMS Service > Example Service: Echo

11.4. Example Service: Echo

As an example, we'll create a service in Python that will echo back at you whatever you send to it.

For example, if you send the following message to Twitter's 40404 short code:

d echo "What's up?"

it will respond with:

ECHO: What's up? (reply? send: d echo hi)

The text in parentheses is added automatically by Twitter you can't get rid of it.

11.4.1. How To: Creating a Simple Twitter Service

Start by creating a new Twitter user for your service. We chose echo for this example, but you will have to choose your own. For the purpose of simplicity, we will set its password to 'echo' as well.

Users can add the service as their friend and then send it direct messages, to which the service will respond.

The Echo service will use a Twitter interface class that we'll be creating. We'll go over that class first and then over the details of the Echo service itself.

To create your Twitter service, you'll need:

A code listing will be provided after we go over the basic functionality provided by the Twitter class.

11.4.2. Functionality

Our Twitter interface provides most of the functions that you'll want to use to implement a Twitter service. Some of them are documented in the public API, but some of them are not. Here are the methods we'll be using:

Twitter requires that most commands be authenticated using Basic Authentication. The constructor builds an object that will attach the appropriate authorization header every time we make a request. The code inside the constructor for Twitter base64 encodes the text "username:password" and then puts it in an "Authorization" header prefixed with the word "Basic." You can read more about Basic Authentication at http://www.ietf.org/rfc/rfc2617.txt.

Almost every method simply uses the opener we created in the constructor to access one URL. The URL returns a JSON dictionary that we convert into a python dictionary.

The .json appended to the URL tells Twitter to return the results as JSON dictionaries. XML and RSS are supported for many methods as well. JSON was chosen here because it is lightweight and translates easily into Python dictionaries. All we have to do is call the read method of the JSON module on the results, and we get a Python dictionary back with all the results

A JSON dictionary for a get_friends call looks something like this (this user has one friend named testinos:

[{"name":"testinos","description":null,"location":null, "profile_image_url":"http:\/\/assets0.twitter.com\/images\/ default_image.gif?1181015407","url":null,"id":5719462, "screen_name":"testinos","protected":false}]

After calling json.read() on the text of the JSON, we get a Python dictionary that looks like this:

[{'name': 'testinos', 'url': None, 'profile_image_url': 'http://assets0.twitter.com/images/default_image.gif?1181015407', 'screen_name': 'testinos', 'location': None, 'protected': False, 'id': 5719462, 'description': None}]

There are a few methods that warrant some additional explanation:

11.4.3. The Echo application

We'll use the commands described above to build our service. The Twitter API only supports polling right now, so we'll be doing everything in a loop.

echo.py:

#!/usr/bin/python import twitter import time twit = twitter.Twitter('servicename', 'servicepass') while True: twit.befriend_all() print "checking..." dms = twit.get_direct_messages() for dm in dms: twit.delete_direct_message(dm['id']) print "Echoing! %s" % dm['text'] twit.send_direct_message(dm['sender_screen_name'], "ECHO: " + dm['text']) time.sleep(120) twit.delete_one_page_of_sent_messages()

Once you have the Twitter interface written, the service itself is trivial. Every two minutes we befriend anyone who has added us as their friend, and check for new messages. If we have a new message, we get the contents of it, delete it, and then send it back to the originator prefixed with ECHO:.

To try this out for yourself:

Have fun tweating!

Категории