SMSRoulette Tutorial (Twilio, Python, Redis)
-
Upload
rahim-sonawalla -
Category
Documents
-
view
1.813 -
download
0
description
Transcript of SMSRoulette Tutorial (Twilio, Python, Redis)
![Page 1: SMSRoulette Tutorial (Twilio, Python, Redis)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/1.jpg)
People seem to love exchangingmsgs anonymously...
![Page 2: SMSRoulette Tutorial (Twilio, Python, Redis)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/2.jpg)
Hmmm, I want to learn by doing...
What can I do?
![Page 3: SMSRoulette Tutorial (Twilio, Python, Redis)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/3.jpg)
Perfect!I’ll build an anon SMS chat app
using Twilio!called...
SMSRoulette
![Page 4: SMSRoulette Tutorial (Twilio, Python, Redis)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/4.jpg)
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)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/5.jpg)
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)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/6.jpg)
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)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/7.jpg)
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)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/8.jpg)
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)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/9.jpg)
Y U NO USE TEXT SLIDES?
![Page 10: SMSRoulette Tutorial (Twilio, Python, Redis)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/10.jpg)
Type it in :)
Problem?
Alright: https://gist.github.com/949919
![Page 11: SMSRoulette Tutorial (Twilio, Python, Redis)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/11.jpg)
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)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/12.jpg)
Something works!
![Page 13: SMSRoulette Tutorial (Twilio, Python, Redis)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/13.jpg)
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)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/14.jpg)
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)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/15.jpg)
Done?
![Page 16: SMSRoulette Tutorial (Twilio, Python, Redis)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/16.jpg)
Linking up users
https://gist.github.com/949923
Alright, here’s one way to do it:
![Page 17: SMSRoulette Tutorial (Twilio, Python, Redis)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/17.jpg)
There are possible race conditions...
Good exercise to find and fix them
![Page 18: SMSRoulette Tutorial (Twilio, Python, Redis)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/18.jpg)
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)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/19.jpg)
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)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/20.jpg)
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)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/21.jpg)
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)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/22.jpg)
Hope you could chat with someone...
I couldn’t.
Code available here:https://github.com/jvimal/SMSRoulette
![Page 23: SMSRoulette Tutorial (Twilio, Python, Redis)](https://reader033.fdocuments.in/reader033/viewer/2022061200/54782343b4af9f52108b4c1b/html5/thumbnails/23.jpg)
My first Twilio contest submission
Thanks for reading. Hope you enjoyed it!