Sending WhatsApp OTP and Notification with Ruby

A quick walk-through on how to send WhatsApp OTP and Notification with Sendchamp WhatsApp API using Ruby

Aliyu Abubakar

Sep 24, 2021

Learn how to quickly send WhatsApp OTP and Notification with Ruby using sendchamp API.

In this guide we will walk you through:

  1. Signing up free sendchamp account
  2. Activate a WhatsApp number
  3. Setting up Ruby Application
  4. Sending your WhatsApp message

Prerequisite

Before you can get started, you need the following already set up: You have Ruby installed. If you don't, read about how to install it on your system here.

  • 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.

image

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.

Activate a WhatsApp Number

Visit your sendchamp dashboard and navigate to WhatsApp channel.

image

Click on activate number and choose a subscription plan that is suitable for you. You won’t be asked to pay for it until your number is active.

When you select a plan, you’ll be redirected to a page that would ask you to fill in the necessary information. Basic information, WhatsApp account information and your business information.

image

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 an SMS message with Sendchamp using Ruby.

Setting up Your Ruby Application

First, let's create a new file in the directory and call it send_wa.rb. In this file, let's include uri and net/http using require.

Replace the string ACCESS-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/whatsapp/message/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/whatsapp/message/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["Content-Type"] = 'application/json'
request["Authorization"] = 'Bearer '
request.body = "{
\"custom_data\":{
\"Body\":{
\"1\":\"Damilola\",
\"2\":\"Olotu\",
\"3\":\"Lagos\"}},
\"sender\":\"2347067959173\",
\"recipient\":\"2349039099438\",
\"template_code\":\"TEMPLATE_CODE\",\
"type\":\"template\"}"

response = https.request(request)
puts response.read_body

But wait, what do these attributes mean?

  • The originator is the sender of the message; Your approved Whatsapp number on Sendchamp. You can use our phone number if you have not registered a number 2347067959173

  • The recipients are the phone numbers 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 type should be set to template.

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 SMS 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 send_wa.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.

Thanks for getting to the end of the tutorial. Be sure that if you have more questions, you can join our [Slack community] (https://bit.ly/community-tutorial-link) here and we will help resolve it.

© 2022 Sendchamp. All rights reserved.