How to send SMS from Ruby
Let's see how you can use Ruby to send SMS messages with HTTP requests. The example code demonstrates that you only need a couple of lines of code to be able to set up the details of the message and send an HTTP request. Then, the SMS Gateway receives the request and delivers the SMS to the recipient. Lastly, the SMS Gateway sends the response back to the Ruby program which prints that response to the console.
What is the Ruby SMS API?
The Ruby SMS API (Ruby sms) allows you to send SMS messages from your Ruby program. The Ruby SMS API initiates HTTP requests and sends them to the SMS Gateway that delivers the message to the recipient.
Prerequisites
Send SMS from Ruby
- Open Notepad
- Create a new file
- Copy-Paste the example code below
- Save the file as HttpApiTester.rb
- Open Command Prompt
- Navigate to the folder where you saved the Ruby file
- Type 'ruby HttpApiTester.rb' in the Command Prompt
- Press Enter to execute the Ruby program
- Ruby Rails send sms text messages
Ruby SMS source code example
This simple example source code below, written in Ruby programming language is free to use, you can simply implement it into your project, or you can modify the source code to use it for other projects or applications. To run the example code and see how to send HTTP requests from Ruby, you just need to create a new Ruby file and place this example code into that file. To execute the Ruby program, just run it from the Command Prompt. These operations stated and demonstrated in the steps and the video sections.
require 'uri' require 'net/http' username = "john" password = "Xc3ffs" messagetype = "SMS:TEXT" httpUrl = "https://127.0.0.1:9508/" recipient = URI.encode_www_form_component("+36201324567") messagedata = URI.encode_www_form_component("TestMessage") sendString = "#{httpUrl}api?action=sendmessage&username=#{username}&password="\ "#{password}&recipient=#{recipient}&messagetype="\ "#{messagetype}&messagedata=#{messagedata}" 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:") puts(response)
Step 1 - Open a text editor
The first step is to execute the Ruby SMS example code above is to create a Ruby file which contains the source code. To perform this operation, you just need a text editor. You can download a custom text editor to do this, but the default Notepad application installed on Windows can do the job as well. So, as you can see it in Figure 1, just click on the icon of Notepad to open it.
Step 2 - Copy the Ruby source code
The next step of the guide is to get the Ruby source code. This can be easily done, since the code can be found on this page, so you just need to copy that. So, as Figure 2 shows that, just go to the example code section, mark out the whole source code and press Ctrl+C on your keyboard to copy the Ruby code to the clipboard.
Step 3 - Paste the Ruby code into your text editor
After you copied the Ruby code from this page, you need to paste it into your empty text file. To do that, you just need to navigate to the text file and just press Ctrl+V on your keyboard. By performing this action, the Ruby code will be pasted into your text file as Figure 3 shows that.
Step 4 - Save the Ruby file
After you placed the source code into the text file, the next step that you need to do, is to save the file using the file extension of Ruby which is '.rb'. So, just select the 'Save as' option in Notepad, name the file and add the '.rb' file extension as you can see it in Figure 4, and lastly, click on Save.
Step 5 - Open Command Prompt
The next step is to run the created Ruby file. This task can be easily done by using the Command Prompt. You can open this byx opening the File Explorer, navigating to the folder, where you saved the Ruby file, and typing 'cmd' in the address bar of the File Explorer like in Figure 5. If you press Enter at this point, the Command Prompt is going to show up and will be ready to use.
Step 6 - Run the Ruby file
In the Command Prompt, you only need to execute one simple command to be able to run the Ruby SMS example. The command needs to be 'ruby *file name*.rb'. After you typed that command, just press Enter, and the command executes the Ruby program. The example program prints the HTTP request and the response message from the SMS Gateway to the Command Prompt. (Figure 6)
Step 7 - Check the send result in the Ozeki log
In Ozeki 10 SMS Gateway, you can check easily what messages sent by your application, since the HTTP API service logs every event that occurred during the time it is enabled. So, if you open the SMS Gateway, and select details of the HTTP API service, you will be able to see the events. As Figure 7 shows that, the service logged an event, when the Ruby example program sent the HTTP request to the service.
The process of the message sending can be also viewed back by the events. For that, open the HTTP API User connection, that you had to configure before. Figure 8 demonstrates that how the connection handles the HTTP request and send the message to the recipient that you specified in your Ruby SMS example program.
Summary
This article explained to you how you can send SMS messages from a project written in Ruby. Sending messages from any Ruby object means that you can offer SMS services to your customers in any Ruby solution. This will make your solution versatile and widely used. The solution is based on the Ozeki SMS Gateway. This code we provided will send HTTP requests from Ruby to the Ozeki SMS Gateway and the Ozeki SMS Gateway will do as requested.
To know more about the system, feel free to check out the Ozeki website where you can find more information about any setup you can build using the Ozeki SMS Gateway. Start with the article about receiving SMS messages. Or check out some other programming languages that the Ozeki SMS Gateway can work with. For example, PHP.
The next thing to do is to download the Ozeki SMS Gateway and start working now!
More information
- Send SMS from a Browser using a HTTP request
- How to send SMS from java
- How to send SMS from php
- How to send SMS from perl
- How to send SMS from Python
- Ruby send SMS with the HTTP rest API (code sample)