Playing with Twilio
2 min read
So the nice guys over at Twilio were kind enough to provide me with a US$30 credit to have a play around with their service. After talking with my fellow WDI student Rob the idea to create a geolocation SMS app was born.
Wanting to take advantage of the ruby gem provided by the Twilio team Sinatra seemed like the obvious choice to get something together in rapid time. The idea was simple, open a web page and use HTML5's handy geolocation object to add a pin to a google map and grab the lat/lng position. If the browser didn't support geolocation then the map wouldn't be displayed, but if it did then the user was presented with a form to enter a number to SMS.
Once the user had entered a number, the rest was easy really. The form post is captured by the Sinatra app and then the SMS is sent using the ruby gem. If the message is sent then I just added a URL parameter which is used to append a class to the body and change the background color to green. If it fails then the background goes red. I'm sure it's possible to capture the error and output that too, and perhaps I'll update that soon too.
However, those that are paying attention will have noticed one major flaw in this idea, each message that is sent is charged to my Twilio account! Now whilst the US$30 was a very generous gift, and each SMS only costs $0.04 at the time of writing, if I were to push this project live it would have pretty limited shelf life.
If you'd like to check out the code, improve it or just have a poke around, feel free to clone the repo and let me know what you think. You'll notice that I haven't included my real Twilio account info in there, just the testing credentials that Twilio provide. However if you swap them out in the
test_credentials.rb file -
@@account_sid = 'yourcodehere' @@auth_token = 'yourcodehere' @@from = 'yournumberhere'
You'll be able to use up your own credit too!