Ruby Whatsapp SDK
The SDK provides a set of operations and classes to use the Whatsapp API. Send stickers, messages, audio, videos, locations, react and reply to messages or just ask for the phone numbers through this library in a few steps!
Demo
Check the Ruby on Rails Whatsapp example repository that uses this library to send messages.
Installation
Add this line to your application's Gemfile:
gem 'whatsapp_sdk'
And then execute:
$ bundle
Or install it yourself as:
$ gem install whatsapp_sdk
Quick Start
There are three primary resources, Messages
, Media
and PhoneNumbers
. Messages
allows users to send any message (text, audio, location, video, image, etc.). Media
allows users to manage media, and Phone Numbers
enable clients to query the associated phone numbers.
To use Messages
, Media
or PhoneNumbers
, you need to initialize the Client
that contains auth information. There are two ways to do it.
Option 1) Use an initializer
# config/initializers/whatsapp_sdk.rb
WhatsappSdk.configure do |config|
config.access_token = ACCESS_TOKEN
config.api_version = API_VERSION
config.logger = Logger.new(STDOUT) # optional, Faraday logger to attach
config. = { bodies: true } # optional, they are all valid logger_options for Faraday
end
Notes:
- Optionally, you can specify the desired API version to use (defaults to the latest version if omitted). Available API version can be found here.
- You can attach a logger. More Details on Faraday Logger Options are here.
Option 2) Create a Client
instance :
# without
client = WhatsappSdk::Api::Client.new("<ACCESS TOKEN>") # replace this with a valid access token
# OR optionally use a logger, api_version and
logger = Logger.new(STDOUT)
= { bodies: true }
client = WhatsappSdk::Api::Client.new("<ACCESS TOKEN>", "<API VERSION>", logger, )
Set up a Meta app
1) Create a Meta Business app

2) Add Whatsapp to your Application

3) Add a phone number to your account

Try sending a message to your phone in the UI.
4) Copy the ACCESS_TOKEN, the SENDER_ID, the BUSINESS_ID and the RECIPIENT_NUMBER

5) Use the GEM to interact with Whatsapp
Example: 1. Install the gem by running `gem install whatsapp_sdk` in the gem. 2. Open the irb terminal by running `irb` 3. `require "whatsapp_sdk"` 4. Set up the `ACCESS_TOKEN`, the `SENDER_ID`, the `BUSINESS_ID` and the `RECIPIENT_NUMBER` in variables. ```ruby ACCESS_TOKEN = "EAAZAvvr0DZBs0BABRLF8zohP5Epc6pyNu" BUSINESS_ID = 1213141516171819 SENDER_ID = 1234567891011 RECIPIENT_NUMBER = 12398765432 ``` 5. Configure the Client by running ```ruby WhatsappSdk.configure do |config| config.access_token = ACCESS_TOKEN end ``` 6. Try the Phone Numbers API or Messages API Phone Numbers API ```ruby registered_number = client.phone_numbers.registered_number(SENDER_ID) ``` Messages API ```ruby message_sent = client.messages.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "Hey there! it's Whatsapp Ruby SDK") ``` Check the [example.rb file](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/blob/main/example.rb) for more examples.Sneak Peek
client = WhatsappSdk::Api::Client.new("<ACCESS TOKEN>") # replace this with a valid access token
client.phone_numbers.register_number(SENDER_ID, 123456) # 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: "test/fixtures/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: "test/fixtures/assets/sticker.webp", type: "image/webp")
client.messages.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: sticker.id)
APIs
Templates
Business Profile API
Phone numbers API
Media API
Messages API
Component's example
```ruby currency = WhatsappSdk::Resource::Currency.new(code: "USD", amount: 1000, fallback_value: "1000") date_time = WhatsappSdk::Resource::DateTime.new(fallback_value: "2020-01-01T00:00:00Z") media_component = WhatsappSdk::Resource::MediaComponent.new(type: "image", link: "http(s)://URL") location = WhatsappSdk::Resource::Location.new( latitude: 25.779510, longitude: -80.338631, name: "miami store", address: "820 nw 87th ave, miami, fl" ) parameter_image = WhatsappSdk::Resource::ParameterObject.new(type: "image", image: media_component) parameter_text = WhatsappSdk::Resource::ParameterObject.new(type: "text", text: "TEXT_STRING") parameter_currency = WhatsappSdk::Resource::ParameterObject.new(type: "currency", currency: currency) parameter_date_time = WhatsappSdk::Resource::ParameterObject.new(type: "date_time", date_time: date_time) parameter_location = WhatsappSdk::Resource::ParameterObject.new(type: "location", location: location) header_component = WhatsappSdk::Resource::Component.new(type: "header", parameters: [parameter_image]) body_component = WhatsappSdk::Resource::Component.new( type: "body", parameters: [parameter_text, parameter_currency, parameter_date_time] ) button_component1 = WhatsappSdk::Resource::Component.new( type: "button", index: 0, sub_type: "quick_reply", parameters: [ WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "PAYLOAD") ] ) button_component2 = WhatsappSdk::Resource::Component.new( type: "button", index: 1, sub_type: "quick_reply", parameters: [ WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "PAYLOAD") ] ) location_component = WhatsappSdk::Resource::Component.new(type: "header", parameters: [parameter_location]) client.messages.send_template( sender_id: 12_345, recipient_number: 12345678, name: "hello_world", language: "en_US", components: [...] ) ```Alternatively, you could pass a plain json like this:
client.messages.send_template(sender_id: 12_345, recipient_number: 12345678, name: "hello_world", language: "en_US", components_json: [{...}])
Send interactive messages Visit the Official API Documentation
List Message's example
```ruby interactive_header = WhatsappSdk::Resource::InteractiveHeader.new(type: "text", text: "I am the header!") interactive_body = WhatsappSdk::Resource::InteractiveBody.new(text: "I am the body!") interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(text: "I am the footer!") interactive_action = WhatsappSdk::Resource::InteractiveAction.new(type: "list_message") interactive_action.button = "I am the button CTA" interactive_section_1 = WhatsappSdk::Resource::InteractiveActionSection.new(title: "I am the section 1") interactive_section_1_row_1 = WhatsappSdk::Resource::InteractiveActionSectionRow.new( title: "I am the row 1 title", id: "section_1_row_1", description: "I am the optional section 1 row 1 description" ) interactive_section_1.add_row(interactive_section_1_row_1) interactive_action.add_section(interactive_section_1) interactive_list_messages = WhatsappSdk::Resource::Interactive.new( type: "list", header: interactive_header, body: interactive_body, footer: interactive_footer, action: interactive_action ) client.messages.send_interactive_list_messages( sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, interactive: interactive_list_messages ) ```Alternatively, you could pass a plain json like this:
client.messages.send_interactive_list_messages(
sender_id: 12_345, recipient_number: 1234567890
interactive_json: {...}
)
Reply Button's example
```ruby interactive_header = WhatsappSdk::Resource::InteractiveHeader.new(type: "text", text: "I am the header!") interactive_body = WhatsappSdk::Resource::InteractiveBody.new(text: "I am the body!") interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(text: "I am the footer!") interactive_action = WhatsappSdk::Resource::InteractiveAction.new(type: "reply_button") interactive_reply_button_1 = WhatsappSdk::Resource::InteractiveActionReplyButton.new( title: "I am the reply button 1", id: "button_1" ) interactive_action.add_reply_button(interactive_reply_button_1) interactive_reply_button_2 = WhatsappSdk::Resource::InteractiveActionReplyButton.new( title: "I am the reply button 2", id: "button_2" ) interactive_action.add_reply_button(interactive_reply_button_2) interactive_reply_buttons = WhatsappSdk::Resource::Interactive.new( type: "reply_button", header: interactive_header, body: interactive_body, footer: interactive_footer, action: interactive_action ) client.messages.send_interactive_reply_buttons( sender_id: 12_345, recipient_number: 1234567890, interactive: interactive_reply_buttons ) ```Alternative, you could pass a plain json like this:
client.messages.send_interactive_reply_buttons(
sender_id: 12_345, recipient_number: 1234567890
interactive_json: {...}
)
Errors
If the API returns an error then an exception WhatsappSdk::Api::Responses::HttpResponseError
is raised. The object contains information returned by the Cloud API. For more information about the potential error check the official documentation.
Examples
Visit the example file with examples to call the API in a single file.
Whatsapp Cloud API
- See the official documentation for the Whatsapp Cloud API.
- For pricing, refer to the official documentation. As of today, Whatsapp offers have 1000 conversations free per month.
Troubleshooting
If you try to send a text message directly without a message template created and approved in your Meta control panel, you can't start a chat with other people. But if you receive a message before, it's possible to send a message.
If the API response is still success
, but the message is not delivered:
- ensure the device you're sending the message to is using a supported Whatsapp version. Check documentation. Try also replying a message to the number you are registered on your Whatsapp.
- Ensure your Meta App uses an API version greater than or equal to
v.14
. - Ensure that the Panel in the Facebook dashboard doesn't display any errors.
- Ensure your Meta App uses an API version greater than or equal to
Note: Sometimes the messages are delayed; see Meta documentation.
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests.
Run ' bundle exec rake install' to install this gem onto your local machine. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Run all the tests
- Unit tests: Run
rake test
- Sorbet Typecheck: run
srb tc
- Linters:
bundle exec rubocop
To update the Cloud API version update the version in lib/whatsapp_sdk/api/api_configuration.rb
. Check the Cloud API changelog for API udpates.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
If you want a feature to be implemented in the gem, please, open an issue and we will take a look as soon as we can.
Do you want to contribute and are unsure where to start? Ping me on Twitter, and I will help you!
Check Contributing file.
License
The gem is available as open source under the terms of the MIT License.