Receive SMS in Ruby Rails

The guide on this page provides you the opportunity to learn how you can use a Ruby program to get the received SMS messages from the SMS Gateway. This can be done by using HTTP requests that collect the received SMS messages. The guide contains a video tutorial and a step by step guide which helps you to learn the way you can receive SMS messages with Ruby.

What is the Ruby SMS API?

The Ruby Rails SMS API allows you to send SMS messages from your Ruby Rails program. The Ruby SMS API initiates HTTP requests and sends them to the SMS Gateway that delivers the message to the recipient.

Prerequisites

Receive SMS in Ruby Rails

  1. Open Notepad application
  2. Copy-Paste the example code from this page
  3. Save the file as ReceiveSMS.rb
  4. Open Ozeki SMS Gateway and choose HTTP Server connection
  5. Open the HTML form of the HTTP Server connection
  6. Send some test messages
  7. Open Command Prompt
  8. Type 'ruby ReceiveSMS.rb' in the Command Prompt and press Enter
  9. Ruby Rails receives sms messages

You can freely use the Ruby example SMS code in your projects and you also allowed to modify it as well. If you wish to just run this example code to see how it works, just check the step by step guide below or the video tutorial on this page to see that it takes to execute the Ruby SMS example code on your computer.

require 'net/http'
require 'rexml/document'
include REXML

def DisplayMessages(response)
	xmldoc = Document.new(response)

	xmldoc.elements.each('response/data/message') do |value|
		begin
			sender = value.elements['originator'].text
			text = value.elements['messagedata'].text
			DisplayMessage(sender,text)
		rescue
			puts("The inbox is empty.")
		end
	end
end

def DisplayMessage(sender, text)
	puts("#{sender}: #{text}")
end

username = "john"
password = "Xc3ffs"
httpUrl = "https://127.0.0.1:9508/"
folder = "inbox";
limit = "3";

sendString = "#{httpUrl}api?action=receivemessage&username="\
			 "#{username}&password="\
			 "#{password}&folder=#{folder}&limit="\
			 "#{limit}&afterdownload=delete"

puts("Sending http request #{sendString}")

OpenSSL::SSL.send(:remove_const, :VERIFY_PEER)
OpenSSL::SSL.const_set(:VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE)

url = URI(sendString)
response = Net::HTTP.get(url)
puts("Http response received:")
DisplayMessages(response)

Step 1 - Open Notepad

In this first step, you need to open a text editor application that can handle Ruby files. For that purpose, you can use the default Notepad application that can be found on all computers using Windows operating system. So, as Figure 1 shows that, just open it from your desktop.

open notepad
Figure 1 - Open notepad text editor

Step 2 - Copy the Ruby code

Next, you need to copy the Ruby example code from this page. To do that, just scroll up to the example code section of this page, and mark out the whole example Ruby code. Next, as you can see it in Figure 2, press Ctrl+C on your keyboard to copy the Ruby code to the clipboard.

copy ruby code
Figure 2 - Copy ruby source code from the website

Step 3 - Paste the code into the text file

After you copied the Ruby source code from this page, you need to place it into a text document. To do this, just go to the Notepad application that you opened before, and as you can see it in Figure 3, just press Ctrl+V on your keyboard to paste the whole Ruby source code into the text document.

paste code to notepad
Figure 3 - Paste source code to notepad

Step 4 - Save the text as a Ruby file

At this point, you have got a text file that contains the source code, but it is not in the right, Ruby file format yet. Notepad can save files in any format, so you can solve this problem quite easily. So, select the 'Save as' option in Notepad, and save the file with the '.rb' file extension. This procedure is demonstrated in Figure 4.

save ruby file
Figure 4 - Save ruby file as ReceiveSMS.rb

Step 5 - Send some test messages

Now, your example code is ready to run, but first, you need to have some messages in your inbox folder. For testing purposes, you can use the HTTP Server connection to send some test messages to yourself and test the PHP example code. So, open the SMS Gateway and select the HTTP Server connection. Here, open the HTML form of the connection, and like in Figure 5, send some messages to the 'Ozeki' recipient.

Simulate incoming sms
Figure 5 - Simulate some incoming SMS

Step 6 - Open Command Prompt

The main step of the guide is to execute the Ruby program. To do that, you need to open the Command Prompt on your computer. So, first navigate to the folder, where you saved the Ruby file using the File Explorer. Here, as you can see it in Figure 6, type 'cmd' in the File Explorer, and if you press Enter, the Command Prompt opens on your computer.

open cmd
Figure 6 - Open command prompt

Step 7 - Execute the Ruby program

In the Command Prompt, you just need to perform one line of command to execute the Ruby program. So, just type 'ruby *filename*.rb' into the Command Prompt and press Enter. By doing this, the Ruby example will be executed and you will be able to see the results in the console window (Figure 7). Here, you can see the HTTP request and the response to that request as well which contains all received messages.

run ruby code
Figure 7 - Run ruby code in command prompt