Sending Voice Message with Ruby using Sendchamp Voice API
In this tutorial, you’ll learn how to send voice messages with Ruby using sendchamp Voice API.
Aliyu Abubakar
Sep 3, 2021
In this Sendchamp Developer Tutorial you’ll learn how to send voice message using the Senchamp Voice API with Ruby.
In this guide we will walk you through:
- Signing up free Sendchamp account
- Setting up Ruby Application
- Sending your first message
Prerequisite
Before you can get started, you need the following already set up:
- Ruby and a familiarity with how to create a new app.
- A sendchamp account
Signing up free sendchamp account
If you Already have an account? skip to the next section You can sign up for a free sendchamp account here.
When you sign up, you'll have to verify your email address. This allows sendchamp to verify your identity.
After you've verified your email address, you'll be asked a question in order to get you started in a way that's relevant to you.
After you finish setting up your account, you'll have access to your dashboard where you can access your API Key and perform dashboard communication functions.
Make sure you set up your development environment before continuing; you can read on how to set up your Ruby Environment.
You can now make your API request and send voice message with Sendchamp using Ruby.
Setting up Your Ruby Application
First, let's create a new file in the directory and call it voice_message.rb. In this file, let's include uri and net/http using require.
Replace the string PUBLIC-KEY in the following Ruby code and hardcode your API key.
Note: For production applications, we recommend storing your key in a configuration file or environment variable instead and passing this variable with the key to the require function.
Load and initialize uri and net/http
require "uri"
require "net/http"
url = URI("https://sandbox-api.sendchamp.com/api/v1/voice/send")
The above base URL is for testing and messages are previewed using simulator. To be able to send live messages, you'll have to switch to live on your dashboard and replace your public key.
Live Base URL
url = URI("https://api.sendchamp.com/api/v1/voice/send")
Now, to send a message, we add our API Access key and pass a few required arguments:
request = Net::HTTP::Post.new(url)
request["Accept"] = "application/json"
request["Authorization"] = "Bearer ACCESS_KEY"
request["Content-Type"] = "application/json"
request.body = "{\n \"customer_mobile_number\": [\"2348055372961\"],\n
\"message\": \"Hello from Olumide Latest\",\n
\"repeat\": 2,\n
\"type\": \"outgoing\"\n}"
response = https.request(request)
puts response.read_body
But wait, what do these attributes mean?
The customer mobile number is the phone number that will receive the message. All numbers should be in the international format with country code. You must specify this attribute as an array even if you have just a single recipient. You can send a message to up to 50 numbers at a time.
The message is the content of the voice message.
The type is the method of the message and should be set to either outgoing or incoming
Make sure to replace the values in the sample code with adequate data for testing. There are additional optional attributes as well; you can find them in the Voice API documentation.
If this call fails, the sendchamp client throws an error. We can rescue this to provide more error information to a user if need be:
response = https.request(request)
puts response.read_body
Enough said, let's try running it from the command line
ruby voice_message.rb
If everything works fine, you should see the API response as output from the script. If you used a live API key and added funds to your wallet, the message will be delivered to the recipient.
If you need help and more guidance, feel free to drop a message on our Developer Community