23
People seem to love exchanging msgs anonymously...

SMSRoulette Tutorial (Twilio, Python, Redis)

Embed Size (px)

DESCRIPTION

Learn how to create an SMSRoulette application using Twilio, Python, and Redis.Created by Vimal (https://github.com/jvimal) for the Teach Twilio Contest (http://www.twilio.com/contests/2011/04/teach-twilio.html).

Citation preview

Page 1: SMSRoulette Tutorial (Twilio, Python, Redis)

People seem to love exchangingmsgs anonymously...

Page 2: SMSRoulette Tutorial (Twilio, Python, Redis)

Hmmm, I want to learn by doing...

What can I do?

Page 3: SMSRoulette Tutorial (Twilio, Python, Redis)

Perfect!I’ll build an anon SMS chat app

using Twilio!called...

SMSRoulette

Page 4: SMSRoulette Tutorial (Twilio, Python, Redis)

Here’s the workflow illustrating the idea...

1. Users will SMS to our Twilio number to initiate random chat

2. SMSRoulette finds a waiting userand connects us with them

3. SMSRoulette relays messagesto provide anonymity!

SMSRoulette

SMSRoulette

Page 5: SMSRoulette Tutorial (Twilio, Python, Redis)

Block Diagram

Twilio Cloud Service

(1) User A sends "connect"SMS to a number

SMSRoulette Server

Active connectionslast_senttofrom

Waiting connectionsnumbernumber

Redis storageserver

(2) Callback from Twilioto handle commands (3) Waiting users are connected

to other waiting users randomly

(4) User B types message, whichis relayed by SMSRoulette

back to User A

Page 6: SMSRoulette Tutorial (Twilio, Python, Redis)

Commands we should supportconnect: registers phone number with SMSRoulettedisconnect: removes phone numberrefresh: reconnect to another random usercall: share phone number with other userstats: show server statistics

What we need to get startedTwilio: get free account at http://twilio.com

python: http://python.org flask: http://flask.pocoo.org/ (for easy webapp development)

redis: http://redis.io https://github.com/andymccurdy/redis-py (our persistent data store)

editor: vim/emacs/whatever you like

Page 7: SMSRoulette Tutorial (Twilio, Python, Redis)

Let’s get started!

First, we register (or buy one) our phone number with Twilio that users will call.

For now, we will use our account’s sandbox number.

With sandbox number, you cannot send msgs to any phone #.Register a few numbers as Caller IDs.

I used my mobile and Google Voice numbers.

Page 8: SMSRoulette Tutorial (Twilio, Python, Redis)

Beginning our Flask app

We write a simple Flask appto parse an incoming SMS andreturn a success message.

Save this file as app.py. We will modify this heavily later!

For more details on how Flask apps are built, check: http://flask.pocoo.org/.

Page 9: SMSRoulette Tutorial (Twilio, Python, Redis)

Y U NO USE TEXT SLIDES?

Page 10: SMSRoulette Tutorial (Twilio, Python, Redis)

Type it in :)

Problem?

Alright: https://gist.github.com/949919

Page 11: SMSRoulette Tutorial (Twilio, Python, Redis)

Let’s test it out!

Send an SMS to your sandbox number. Don’t forget to prefix SMS with your PIN!

Our app should print this log

Configure the SMS URL at https://www.twilio.com/user/account/to http://<your webserver’s IP>:5000

Set HTTP Method to GET.

Your mobile should get “Successfully parsed SMS” messageprefixed with “Sent from a Twilio Trial Account -”

Page 12: SMSRoulette Tutorial (Twilio, Python, Redis)

Something works!

Page 13: SMSRoulette Tutorial (Twilio, Python, Redis)

Managing users

Pretty straightforward

logic

For ease of reading, code in full:https://gist.github.com/949918

Now, we implement the supported

commands that we saw earlier.

We use redis as a persistent data store so that we don’t lose

state when the webserver needs to

be restarted.

These functions go into users.py.

Page 14: SMSRoulette Tutorial (Twilio, Python, Redis)

Fill in the logic for this yourself!

def linkup(): """Links up two random people from the waiting list.""" pass

def stats(number): """Implement your favourite stats here. :-)""" pass

Page 15: SMSRoulette Tutorial (Twilio, Python, Redis)

Done?

Page 16: SMSRoulette Tutorial (Twilio, Python, Redis)

Linking up users

https://gist.github.com/949923

Alright, here’s one way to do it:

Page 17: SMSRoulette Tutorial (Twilio, Python, Redis)

There are possible race conditions...

Good exercise to find and fix them

Page 18: SMSRoulette Tutorial (Twilio, Python, Redis)

Integrating with the Flask app

CONNECT_SYNONYMS = ['connect', 'conn', 'login', 'register']DISCONNECT_SYNONYMS = ['disconnect', 'dc', 'logout']REFRESH_SYNONYMS = ['refresh', 'reload']CALL_SYNONYMS = ['call', 'share']STATS_SYNONYMS = ['stats']

Being user friendly :)

https://gist.github.com/949930

Now we demux an incoming SMS to perform our commands

Page 19: SMSRoulette Tutorial (Twilio, Python, Redis)

send_sms(number, text)Implementing

And finally, a way to send SMS! Straight out of Twilio’s sample

code.

https://gist.github.com/949939

Page 20: SMSRoulette Tutorial (Twilio, Python, Redis)

Run it, it should work.

app.py: Contains the Flask webapp, handles the core functionalitysms.py: Wrapper for sending SMS through Twiliousers.py: The core API that’s called through SMS

Run it as follows:$ python app.py * Running on http://0.0.0.0:5000/

Page 21: SMSRoulette Tutorial (Twilio, Python, Redis)

Twilio has a log...

... of all SMSs sent... of failed requests

... you can watch people chat

Be responsible :)

It should help debugging... or

https://www.twilio.com/user/account/log/sms

https://www.twilio.com/user/account/debugger

Page 22: SMSRoulette Tutorial (Twilio, Python, Redis)

Hope you could chat with someone...

I couldn’t.

Code available here:https://github.com/jvimal/SMSRoulette

Page 23: SMSRoulette Tutorial (Twilio, Python, Redis)

My first Twilio contest submission

Thanks for reading. Hope you enjoyed it!