Contact Us

Sending a message with Ruby in under 2 minutes

Prerequisites

Step 1: Set up a project

Rather than using rails for scaffolding a project we are going to write a standard ruby script and use bundler to include the library file. Make a new folder, and within that folder make a file for the sender and open it:

mkdir senderruby
cd senderruby
touch sender.rb
open sender.rb

Step 2: Install the SDK

While the prompt is inside the senderruby folder, we are going to install two packages. First the messages SDK:

gem install messagemedia_messages_sdk

Then the pp package to print results:

gem install pp

Step 3: Add the code

Inside sender.rb add the following in the code:

require 'message_media_messages'
require 'pp'

include MessageMediaMessages

auth_user_name = '[YOUR_API_KEY]'
auth_password = '[YOU_API_SECRET]'
use_hmac = false

client = MessageMediaMessages::MessageMediaMessagesClient.new(
    auth_user_name: auth_user_name,
    auth_password: auth_password,
    use_hmac_authentication: use_hmac
)

messages_controller = client.messages
body = SendMessagesRequest.new
body.messages = []

body.messages[0] = Message.new
body.messages[0].content = 'My first message'
body.messages[0].destination_number = '+614<phone_number>'
body.messages[0].delivery_report = true
body.messages[0].format = FormatEnum::SMS

begin
    result = messages_controller.send_messages(body)
    pp result
rescue SendMessages400ResponseException => ex
    puts "Caught SendMessages400ResponseException: #{ex.message}"
rescue APIException => ex
    puts "Caught APIException: #{ex.message}"
end

Step 4: Send

Run the command:

ruby sender.rb

 

You’ve now sent a simple message with Ruby. Check out the API documentation and the Github repository to see what else you can do.