I got a little bored with what I had been posting (I suspect you did, too), so I’m doing something neat. I’m going to write a full (though limited) program for you. Here are the user stories (if you don’t know what user stories are, see here before continuing):
- As a web user I can enter a phone number and a SMS message into a queue so that it can be sent by a mobile handset.
- As a mobile handset user, I can push a button to dequeue a message and send it via SMS so that I might risk my device’s stability and my own financial solvency by opening up the internet to sending messages through my phone.
A Little More Definition
So, I’m really not going to let you send SMS messages through my phone, that would be really stupid for me to open that sort of thing up. I will, however, give you the code so that you can demo it to our friends.
Technologies Used
- Mojolicious – A very clever web framework in Perl
- MongoDB – This is MongoDB, and I’m using the Perl interface, which is interestingly enough also called MongoDB
- JSON – It’s JSON
- SL4A – Scripting Languages for Android, I will use this for the consumer of the piece I’m writing about today. You won’t see me use this until tomorrow.
Here’s the Code
So here are a few things you will probably notice right away:
- I’m using the
__DATA__
segment of the script to store all of my templates.Mojolicious::Lite
lets you do that, it’s pretty cool. - I’m using Mojolicious in CGI mode, mainly to keep things simple for the demo. Mojo also supports hypnotoad and morbo, as well as a bunch of other things which I have been assured the cool kids are totally using. I like hypnotoad and morbo (for development only) both quite a bit.
Let’s go into the code a bit
- The
'/'
route is the landing page, it just presents you with the form. - When you submit that form, you go to the ‘/sms’ route (via
POST
only). This then allows you to queue up your message in the Mongo collection. - Then there’s the
'/dequeue'
route which does afind_and_modify()
call against the collection. The reason I’m doing it this way is because this is an atomic action in MongoDB, so I don’t have to worry about anybody trying to modify or remove the record while I’m fetching and removing it. - Then I take the entry and print it out in JSON.
Tomorrow I will write an SL4A script which will consume the '/dequeue'
route to fetch a record for transmission.
The Conclusion
This one isn’t terribly fancy, but I thought it’d be nice to stick a bunch of stuff together for a quick demo, and it’s been a while since I played with SL4A. I hope you liked reading this as much as I enjoyed writing it.
Be on the lookout tomorrow for when I do the SL4A component.
One thought on “Gist of the Day: Using Mojo, Mongo, and SL4A to send SMS Messages (1 of 2)”