Integrating WhatsApp with Ruby: A Simple Guide

Ignacio Chiazzo
3 min readOct 16, 2024

--

In today’s world, communication with customers via messaging platforms like WhatsApp is critical for businesses. Fortunately, integrating WhatsApp into your Ruby application is straightforward with the Ruby Whatsapp SDK which uses the Cloud API. You can enable your Ruby app to send and receive messages seamlessly using WhatsApp in just a few steps.

What is WhatsApp Cloud API?

The WhatsApp Cloud API, powered by Meta, allows businesses to integrate WhatsApp into their applications without worrying about managing local servers. It’s a cloud-hosted service, making it simpler and faster for developers to get started.

Capabilities of WhatsApp Cloud API

The Cloud API provides several key features that make it a robust tool for any developer:

  • Messaging: Send and receive text messages in real-time.
  • Media Sharing: Easily send images, videos, audio, and documents.
  • Interactive Messages: Use buttons and other interactive elements to improve user engagement.
  • Business Profiles, phone numbers, and templates: Manage Business profiles, phone numbers, and Templates.

Now, let’s see how to integrate this with a Ruby app using the ruby_whatsapp_sdk.

Step-by-Step Integration

Step 1: Install the Ruby WhatsApp SDK

To get started, add the gem whatsapp_sdk to your Gemfile (gem whatsapp_sdk) and then execute bundle install .

Step 2: Configure the SDK

You must set up your API credentials to connect your Ruby app to WhatsApp’s Cloud API. First, create an account on Meta’s developer platform and obtain the necessary token (see instructions).

Now, configure the SDK with your API token:

client = WhatsappSdk::Api::Client.new("<YOUR ACCESS TOKEN>") 

Step 3: Send Your First Message

With the SDK configured, you can send a message by calling the appropriate method. For instance, to send a simple text message, use the following code:

client.messages.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,message: "Hey there! it's Whatsapp Ruby SDK")

Just like that, your message is sent via WhatsApp!

Step 4: Handle Incoming Messages

To handle incoming messages, you’ll need to set up a webhook to receive updates from WhatsApp. You can use any Ruby web framework like Sinatra or Rails to create a route that listens for incoming POST requests.

Once your webhook is configured, WhatsApp will send incoming messages to your app, which you can process and respond to. Here is an example

Step 5: Use other APIs

The client object gives you access to the different APIs via methods like client.messages , client.templates or client.phone_numbers etc. Here are some examples:

pin = "123456"
client.phone_numbers.register_number(SENDER_ID, pin) # register the phone number to uplaod media and send message from.

# send a text and a location
client.messages.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,message: "Hey there! it's Whatsapp Ruby SDK")
client.messages.send_location(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, longitude: -75.6898604, latitude: 45.4192206, name: "Ignacio", address: "My house")

# upload a photo and send it
image = client.media.upload(sender_id: SENDER_ID, file_path: "tmp/assets/whatsapp.png", type: "image/png")image = client.media.get(media_id: uploaded_media.id)
client.messages.send_image(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: image.id)

# upload a sticker and send it
sticker = client.media.upload(sender_id: SENDER_ID, file_path: "tmp/assets/sticker.webp", type: "image/webp")
client.messages.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: sticker.id

Conclusion

Integrating WhatsApp into your Ruby application is straightforward, especially with the power of the WhatsApp Cloud API and the whatsapp_sdk gem. With a few lines of code, you can start engaging with users on one of the world’s most popular messaging platforms. Whether you need basic messaging or advanced features like media sharing and interactive buttons, the Cloud API has you covered.

This brief guide demonstrates the simplicity of working with the WhatsApp Cloud API using Ruby. If you have feedback on the gem, open an issue in the Github repository.

--

--