There are plenty of dazzling features we implement in modern web apps. Usually it’s social media integration, sometimes crawlers gathering information from other sites. But there’s one feature that is often overlooked in small projects – even when it’s widely used in bigger ones. I’m talking about text message sending.
Part I – The Purpose
One more thing before we go on to the code. What may be the purpose to implement sending SMS messages in your app? Well, there are multiple choices. First of all – notifying users about different actions, like their package being sent, or order received. Many apps use SMS confirmation as an additional authentication or authorization measure – like banks, for example. When performing some operations you are asked to provide the code you received via SMS a few moments earlier. There was also a brave, but interesting idea of replacing passwords with one-off codes sent to users via SMS. It may have some drawbacks, but also many advantages – you don’t have to remember passwords for each website you log to. Your phone tends to be close at hand most of the time, right :) ?
Part II – The Providers
You wouldn’t go anywhere without providers. There are many platforms providing APIs for sending messages. A lot of them (like Twilio) provide worldwide service – and the prices aren’t even that high, starting at less than 0.01$ for US and Canada, around 0.03$ for Poland, going to 0.07$ for other European countries. Using Twilio is easy – they provide a gem for sending message through their API, and even dynamically generated samples of code for different programming languages (not only Ruby and Python) or other tools (cURL). Unfortunately I couldn’t test their message service in Poland. That’s why sometimes it’s better to search for a local provider. I stumbled uponSMSAPI.pl, which also happened to give you 50 free messages for test purposes. Well, that’s an excellent opportunity for us! It also seems to be a little cheaper than Twilio – at least when it comes to sending local messages.
Part III – The Code
First off we set up the app, blah blah blah… I assume that you know the thing already :). You’ll also need an account in the platform system. I decided to stick with SMSAPI. It seemed easier at the time, and I also found a gem configured for working with their service – yay! It’s called sms_sender – check it out on rubygems.org. We’ll be using this later.
So, I assume you have the account by now. Let’s get to code (at last!). You’ll need to include the sms_sender gem in your Gemfile. This line will do the trick:
Don’t forget to run
Done? OK, now let’s add your SMSAPI login credentials to local_env.yml.
SMSAPI_USERNAME: 'firstname.lastname@example.org' SMSAPI_PASSWORD: 'your_password'
Next, it’s time to add a few lines of config to your environments/development.rb and production.rb files.
# config for sms_sender # this way you can keep your account safe – just add the local_env.yml to .gitignore # (if you use git) config.sms_sender_login = ENV["SMSAPI_USERNAME"] config.sms_sender_password = ENV["SMSAPI_PASSWORD"] # sets sending eco SMS messages to true config.sms_sender_eco = 1
Eco messages are the cheapest ones. They are sent from random numbers and don’t let you use diacritical signs – but in most cases it wil be enough. However you can add a line
config.sms_sender_from = 'defined_name'
to send pro messages with custom „from” attribute. Note that you have to define it earlier in the SMSAPI panel, and you should comment out or remove the config.sms_sender_eco line.
Well, and that’s almost everything. The last thing to do is actually send the message! It’s just a piece of cake. One line. One method. Two parameters. Could it be easier?
And everything just works. Try it yourself!
Part IV – Teh Testz
One more thing you might want to work on – the tests. Let’s say it – it’s just useful. Mr. Zubala thought about it and provided us with a fake implementation sender for test suite.
assert_sms_sent 'to_number', 'message_body'
to check the if the message was sent correctly. Just remember, that this test will be passed only once per sent message. Any other calls of this method will fail.
On the opossite – you can check to make sure that no SMS was sent.
if you want to make sure that no texts were sent. Simple and easy.
And that’s all. Doesn’t sound that complex after all, right? So get going and incorporate some new features in your apps!
I’d also highly encourage you to try Twilio. Aside sending messages, it provides some other fun features (like making calls from your app). I’ll try to cover them sometime in the future :)