How to make phone calls and send SMS with Ruby

Bandwidth has a full suite of messaging API solutions to power your business’ needs. Whether you want to send messages from toll-free numbers, local numbers, or short codes, Bandwidth’s APIs make it easy to enable in your software or platform, while Bandwidth’s relationships with the carriers give you reliable message delivery and insights.
Introduction
In this article, you will see a quick overview of what sending your first message and creating your first call looks like when using Bandwidth’s Voice and Messaging APIs along with Bandwidth’s Ruby SDK. First you will see some code examples and then at the end of this article is an explanation of the variables you would need to insert into the code to make the SDK work with your Bandwidth account.
Additional resources:
- Bandwidth’s SDKs & Developer Tools
- Bandwidth Ruby SDK GitHub
- Bandwidth Developing with Ruby Webinar Transcript
Packages and Client Initialization
First you need to install the gems needed for this example like this.
gem install bandwidth-sdk
gem install sinatra
You then need to initialize the configuration for the SDK client.
require 'bandwidth'
include Bandwidth
bandwidth_client = Bandwidth::Client.new(
messaging_basic_auth_user_name: '{username}',
messaging_basic_auth_password: '{password}',
voice_basic_auth_user_name: '{username}',
voice_basic_auth_password: '{password}'
)
$voice_client = bandwidth_client.voice_client.client
$messaging_client = bandwidth_client.messaging_client.client
How to send a text message with Ruby
Sending a SMS with Ruby from a Bandwidth number looks like this. This must be in the same file as your initiated client or the client must be imported here.
require 'bandwidth'
include Bandwidth::Messaging
messageBody = MessageRequest.new
messageBody.application_id = '{app_id}'
messageBody.to = ['{to}']
messageBody.from = '{from}'
messageBody.text = 'Hello, I am sending a message! How fun!'
$messaging_client.create_message('{account_id}', body: messageBody)
You will need to set up a server that can receive a callback/webhooks with a JSON body that will tell you if the message was successfully delivered or not. Some simple Ruby server code using sinatra that can process Bandwidth messaging callbacks could look like this.
require 'sinatra'
post '/messaging' do
data = JSON.parse(request.body.read)
if (data[0]['type'] == 'message-delivered')
//successful delivery action
halt 200
end
if (data[0]['type'] == 'message-failed')
failureReason = data["description"]
//failed delivery action
halt 200
end
halt 200
end
How to make a phone call with Ruby
Making a phone call with Ruby from a Bandwidth number looks like this. This must be in the same file as your initiated client or the client must be imported here.
require 'bandwidth'
include Bandwidth::Voice
voiceBody = ApiCreateCallRequest.new
voiceBody.from = '{from}'
voiceBody.to = '{to}'
voiceBody.answer_url = '{url}'
voiceBody.application_id = '{app_id}'
$voice_client.create_call('{account_id}', :body => voiceBody)
When the call is answered by whoever you sent the call to, Bandwidth will send a callback/webhook with a JSON body to the URL you specified when you created the call. You can send BXML verbs back to Bandwidth in response to the callback/webhook in order to create new actions on the call. Some simple server code that would make the call play some text-to-speech audio and then hang up could look like this:
require 'bandwidth'
require 'sinatra'
include Bandwidth::Voice
post '/voice' do
data = JSON.parse(request.body.read)
if (data['eventType'] == 'answer')
response = Response.new()
speak_sentence = SpeakSentence.new({
:sentence => 'I am saying something and now will hang up.',
:voice => 'julie'
})
response = Response.new()
hangup = Hangup.new()
response.push(speak_sentence)
response.push(hangup)
response.to_bxml()
end
halt 200
end
And that’s it! That’s how simple it is to create your first message and call with a Bandwidth phone number, Bandwidth’s API, and Bandwidth’s Ruby SDK.
Variable Reference
Here is an explanation of the variables used in the above code that will be unique to your account and use case.
Variable Name | Explanation |
username | your username for the Bandwidth Dashboard |
password | your password for the Bandwidth Dashboard |
app_id | the ID of your messaging or voice application; applications are set within the Bandwidth Dashboard and are used associate your Bandwidth phone numbers with URLs for callback/webhooks |
from_number | when creating outgoing messages or calls, the number you are sending to; this can be your cell phone number for testing or the number of one of your customers |
to_number | when creating outgoing messages or calls, this is a number you have been assigned by Bandwidth that lives on your Bandwidth Dashboard account |
account_id | your Bandwidth Dashboard account ID |
url | a URL you own that to which Bandwidth will send callbacks/webhooks related to call and messaging events; note that for messaging, this URL is not set at the time of message creation and is instead defined by the Bandwidth Dashboard application used to send the message |
Developing with Bandwidth – Ruby
Looking for additional information on developing with Ruby? The webinar below goes into greater detail on the topics presented in this post.