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!
Before we dive in, make sure you've got:
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!
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 a simple text message is a piece of cake:
client.messages.send_text( to: 'recipient_phone_number', body: 'Hello from Ruby!' )
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' )
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' }] } ] )
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
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
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' } } ] } )
Keep track of your messages:
client.messages.get_status(message_id: 'your_message_id')
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.
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
When you're ready to go live:
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! 🚀