Chat API Ruby SDK for WhatsApp API

Download SDK

You can customize this SDK to your needs and make it more convenient by clicking here:

WhatsApp Ruby SDK Generator

The SDK allows you to receive and send messages through your WhatsApp account. Sign up now

The Ruby gem for the Chat API SDK

  • API version: 1.0.0
  • Package version: 1.0.0
  • Build package: org.openapitools.codegen.languages.RubyClientCodegen

Installation

Build a gem

To build the Ruby code into a gem:

gem build openapi_client.gemspec

Then either install the gem locally:

gem install ./openapi_client-1.0.0.gem

(for development, run gem install --dev ./openapi_client-1.0.0.gem to install the development dependencies)

or publish the gem to a gem hosting service, e.g. RubyGems.

Finally add this to the Gemfile:

gem 'openapi_client', '~> 1.0.0'

Install from Git

If the Ruby gem is hosted at a git repository: https:///YOURGITUSERNAME/YOURGITREPO, then add the following in the Gemfile:

gem 'openapi_client', :git => 'https:///YOUR_GIT_USERNAME/YOUR_GIT_REPO.git'

Include the Ruby code directly

Include the Ruby code directly using -I as follows:

ruby -Ilib script.rb

Getting Started

Please follow the installation procedure and then run the following code:

# Load the gem
require 'openapi_client'

# Setup authorization
OpenapiClient.configure do |config|
  # Configure API key authorization: instanceId
  config.api_key['instanceId'] = 'YOUR API KEY'
  # Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
  #config.api_key_prefix['instanceId'] = 'Bearer'

  # Configure API key authorization: token
  config.api_key['token'] = 'YOUR API KEY'
  # Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
  #config.api_key_prefix['token'] = 'Bearer'
end

api_instance = OpenapiClient::Class1InstanceApi.new

begin
  #Repeat the manual synchronization attempt with the device
  result = api_instance.call_retry
  p result
rescue OpenapiClient::ApiError => e
  puts "Exception when calling Class1InstanceApi->call_retry: #{e}"
end

Documentation for API Endpoints

All URIs are relative to https://api.chat-api.com

Documentation for Authorization

instanceId

  • Type: API key
  • API key parameter name: instanceId
  • Location: URL query string

token

  • Type: API key
  • API key parameter name: token
  • Location: URL query string

Preparatory work

At the very beginning, we need to connect whatsapp with our script, so as we write the code, we check its operation. To do this, go to your personal account and get a QR code there. Next, open WhatsApp on your smartphone, go to Settings -> WhatsApp Web -> Scan a QR code.

Now we need to indicate a WebHook URL so the server can run the scrip when new messages arrive. Indicate a direct link to your script. You can’t indicate server IP only, but you can indicate the port.


The first steps for beginners

This client library is a Ruby gem which can be compiled and used in your Ruby and Ruby on Rails project. This library requires a few gems from the RubyGems repository.

  1. Open the command line interface or the terminal and navigate to the folder containing the source code.
  2. Run gem build whatsapp_api.gemspec to build the gem.
  3. Once built, the gem can be installed on the current work environment using gem install whatsapp_api-1.0.0.gem

Building Gem

How to Use

The following section explains how to use the WhatsappAPI Ruby Gem in a new Rails project using RubyMine™. The basic workflow presented here is also applicable if you prefer using a different editor or IDE.

1. Starting a new project

Close any existing projects in RubyMine™ by selecting File -> Close Project. Next, click on Create New Project to create a new project from scratch.

Create a new project in RubyMine

Next, provide TestApp as the project name, choose Rails Application as the project type, and click OK.

Create a new Rails Application in RubyMine - step 1

In the next dialog make sure that correct Ruby SDK is being used (minimum 2.0.0) and click OK.

Create a new Rails Application in RubyMine - step 2

This will create a new Rails Application project with an existing set of files and folder.

2. Add reference of the gem

In order to use the WhatsappAPI gem in the new project we must add a gem reference. Locate the Gemfile in the Project Explorer window under the TestApp project node. The file contains references to all gems being used in the project. Here, add the reference to the library gem by adding the following line: gem 'whatsapp_api', '~> 1.0.0'

Add references of the Gemfile

3. Adding a new Rails Controller

Once the TestApp project is created, a folder named controllers will be visible in the Project Explorer under the following path: TestApp > app > controllers. Right click on this folder and select New -> Run Rails Generator....

Run Rails Generator on Controllers Folder

Selecting the said option will popup a small window where the generator names are displayed. Here, select the controller template.

Create a new Controller

Next, a popup window will ask you for a Controller name and included Actions. For controller name provide Hello and include an action named Index and click OK.

Add a new Controller

A new controller class anmed HelloController will be created in a file named hello_controller.rb containing a method named Index. In this method, add code for initialization and a sample for its usage.

Initialize the library

How to Test

You can test the generated SDK and the server with automatically generated test cases as follows:

  1. From terminal/cmd navigate to the root directory of the SDK.
  2. Invoke: bundle exec rake

Other WhatsApp API SDKs

You can configure the SDK or generate other SDKs in 30+ programming languages: Chat API SDK generator.