Back

Step by Step Guide to Building a WhatsApp Business API Integration in Ruby

Aug 7, 2024 • 6 minute read

Introduction

Hey there, fellow Ruby enthusiast! Ready to supercharge your messaging game with WhatsApp Business API? You're in the right place. We'll be using the nifty whatsapp_sdk package to make this integration a breeze. Buckle up!

Prerequisites

Before we dive in, make sure you've got:

  • A Ruby environment that's up and running
  • A WhatsApp Business API account (if you don't have one, go grab it!)

Installation

Let's kick things off by adding the whatsapp_sdk gem to your project:

gem install whatsapp_sdk

Or, if you're using a Gemfile (and you should be!), toss this line in:

gem 'whatsapp_sdk'

Then run bundle install. Easy peasy!

Configuration

Time to get that WhatsApp client up and running:

require 'whatsapp_sdk' client = WhatsApp::Client.new( access_token: 'your_access_token', phone_number_id: 'your_phone_number_id' )

Replace those placeholder values with your actual credentials, and you're good to go!

Sending Messages

Text Messages

Sending a simple text message is a piece of cake:

client.messages.send_text( to: 'recipient_phone_number', body: 'Hello from Ruby!' )

Media Messages

Want to spice things up with some media? No problem:

client.messages.send_image( to: 'recipient_phone_number', image_url: 'https://example.com/image.jpg' )

Template Messages

For those pre-approved templates:

client.messages.send_template( to: 'recipient_phone_number', template_name: 'your_template_name', language: 'en_US', components: [ { type: 'body', parameters: [{ type: 'text', text: 'John' }] } ] )

Receiving Messages

Setting up Webhooks

To handle incoming messages, you'll need to set up a webhook. Here's a quick example using Sinatra:

require 'sinatra' post '/webhook' do # Process the incoming webhook payload payload = JSON.parse(request.body.read) # Handle the message end

Handling Incoming Messages

Once you've got your webhook set up, you can process incoming messages like this:

if payload['entry'][0]['changes'][0]['value']['messages'] message = payload['entry'][0]['changes'][0]['value']['messages'][0] # Do something with the message end

Advanced Features

Interactive Messages

Want to add some buttons or lists? Here's how:

client.messages.send_interactive( to: 'recipient_phone_number', type: 'button', body: { text: 'Choose an option' }, action: { buttons: [ { type: 'reply', reply: { id: 'button1', title: 'Option 1' } }, { type: 'reply', reply: { id: 'button2', title: 'Option 2' } } ] } )

Message Status Updates

Keep track of your messages:

client.messages.get_status(message_id: 'your_message_id')

Error Handling and Best Practices

Always wrap your API calls in a begin/rescue block:

begin # Your API call here rescue WhatsApp::Error => e puts "Oops! Something went wrong: #{e.message}" end

And don't forget about rate limits! Be a good API citizen and space out your requests.

Testing

Unit testing is your friend. Mock those API responses:

RSpec.describe WhatsApp::Client do let(:client) { WhatsApp::Client.new(access_token: 'fake_token', phone_number_id: 'fake_id') } it 'sends a text message' do allow(client.messages).to receive(:send_text).and_return(true) expect(client.messages.send_text(to: '1234567890', body: 'Test')).to be true end end

Deployment Considerations

When you're ready to go live:

  1. Use environment variables for sensitive info
  2. Implement proper error logging
  3. Consider using a queueing system for high-volume messaging

Conclusion

And there you have it! You're now armed with the knowledge to build a robust WhatsApp Business API integration in Ruby. Remember, the official WhatsApp Business API documentation is your best friend for staying up-to-date with the latest features and best practices.

Now go forth and build something awesome! Happy coding! 🚀