DEV Community

Cover image for Send SMS with Ruby Mechanize gem
Merdan Durdyyev
Merdan Durdyyev

Posted on

Send SMS with Ruby Mechanize gem

Welcome

Good day, dear friends, coders, and enthusiasts. Here's another post about how to do something else with precious Ruby. Today, we are going to write Ruby code to send SMS via a web app, provided by a Mobile service Carrier. No matter, be it US carriers like AT&T, Verizon, Sprint, or carriers in Russia like MTC, Megafon, and Beeline, we can use their web app (if they provide one) to automate sending SMS with Ruby.


Mechanize gem

Let's first make it clear, what a gem is.
Gem :

  • is a software package that contains packed Ruby application or library.
  • is used to extend or modify functionality in Ruby applications

To sum it up, we create Ruby gems, to be able to distribute them and make someone else's life easier. Other Rubyists can use them in their own code to import ready-to-use code, enhance productivity, and make development much faster.

Mechanize gem - is a ruby library that makes automated web interaction easy. Mechanize can follow links and submit forms. Form fields can be populated and submitted.

You can find more about the mechanize gem here:


Sending Test SMS

Here is the piece of code, where you can follow and get a clear explanation of how to fill in the form fields, submit it, and send the required SMS.
The following input fields and service URL are shown just as an example and maybe named differently in actual services.

  • 'https://customer.verizon.com/' customer account URL
  • 'login' input field
  • 'password' input field
  • 'Sms.DestinationAddress' input field
  • 'Sms.Text' textarea field
require 'mechanize'

mech = Mechanize.new

# Ignore SSL certificate
mech.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE 

# The login page
login_page = mech.get('https://customer.verizon.com/')  
# The login form
login_form = login_page.forms[0]       

# Filling in 'login' and 'password' input fields, and submitting.
login_form['login'] = '888-555-4444'
login_form['password'] = 'ABCDEFG'
login_form.submit

# The SMS form page
sms_page = mech.get('https://customer.verizon.com/SMS/Send')    
# The SMS form, the first and only form
sms_form = sms_page.forms[0]                                

# Entering sms number and sms message to send
# 'Sms.DestinationAddress' and 'Sms.Text' are input and textbox names
sms_form['Sms.DestinationAddress'] = '555-888-2222'
sms_form['Sms.Text'] = 'This is test message'
sms_form.submit    
Enter fullscreen mode Exit fullscreen mode

Automate SMS Sending

What we need to do now is just add the list of recipients (their numbers and corresponding messages). I have added just two of them. But you can add lots of more.
What I have done here is:

  • I added list of recipients;
  • I gave the list to an 'each' block.

You can make it even better by gathering all that code under a function that gets respective arguments and call that function for each of the recipients.

require 'mechanize'

sms_list = [
    { number: '555-666-8888', message: 'Get this SMS...'},
    # And lots of lots SMS numbers and messages...
    { number: '555-777-0022', message: 'Last one...'}
]
mech = Mechanize.new

# Ignore SSL certificate
mech.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE 

# The login page
login_page = mech.get('https://customer.verizon.com/')  
# The login form
login_form = login_page.forms[0]       

# Filling in 'login' and 'password' input fields, and submitting.
login_form['login'] = '888-555-4444'
login_form['password'] = 'ABCDEFG'
login_form.submit

# The SMS form page
sms_page = mech.get('https://customer.verizon.com/SMS/Send')    
# The SMS form, the first and only form
sms_form = sms_page.forms[0]                                

# Send SMS to whole 'sms_list'
sms_list.each do |contact|
    # Entering sms number and sms message to send
    sms_form['Sms.DestinationAddress'] = contact[:number]
    sms_form['Sms.Text'] = contact[:message]
    # Submitting form, sending sms
    sms_form.submit                                             
end

Enter fullscreen mode Exit fullscreen mode

Sum It Up

We have looked at just a small piece of what 'mechanize' gem can actually do. It has lots of more possibilities.
You can write a console application that surfs the given URLs and performs given operations without your attendance. So, the rest is up to you to discover. Go for it!

Top comments (0)