Chat API Node.js SDK for messengers API

Download SDK

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

messengers Node.js SDK Generator

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

Download SDK and unpack the archive. Connect SDK to your project.

Installation

For Node.js

npm

To publish the library as a npm, please follow the procedure in "Publishing npm packages".

Then install it via:

npm install chat_api_sdk --save

Finally, you need to build the module:

npm run build
Local development

To use the library locally without publishing to a remote npm registry, first install the dependencies by changing into the directory containing package.json (and this README). Let's call this JAVASCRIPT_CLIENT_DIR. Then run:

npm install

Next, link it globally in npm with the following, also from JAVASCRIPT_CLIENT_DIR:

npm link

To use the link you just defined in your project, switch to the directory you want to use your chatapisdk from, and run:

npm link /path/to/<JAVASCRIPT_CLIENT_DIR>

Finally, you need to build the module:

npm run build

git

If the library is hosted at a git repository, e.g.https://github.com/YOURUSERNAME/chatapi_sdk then install it via:

    npm install YOUR_USERNAME/chat_api_sdk --save

For browser

The library also works in the browser environment via npm and browserify. After following the above steps with Node.js and installing browserify with npm install -g browserify, perform the following (assuming main.js is your entry file):

browserify main.js > bundle.js

Then include bundle.js in the HTML pages.

Webpack Configuration

Using Webpack you may encounter the following error: "Module not found: Error: Cannot resolve module", most certainly you should disable AMD loader. Add/merge the following section to your webpack config:

module: {
  rules: [
    {
      parser: {
        amd: false
      }
    }
  ]
}

Getting Started

Please follow the installation instruction and execute the following JS code:

var ChatApiSdk = require('chat_api_sdk');

var defaultClient = ChatApiSdk.ApiClient.instance;
// Configure API key authorization: instanceId
var instanceId = defaultClient.authentications['instanceId'];
instanceId.apiKey = "YOUR API KEY"
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//instanceId.apiKeyPrefix['instanceId'] = "Token"
// Configure API key authorization: token
var token = defaultClient.authentications['token'];
token.apiKey = "YOUR API KEY"
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//token.apiKeyPrefix['token'] = "Token"

var api = new ChatApiSdk.Class1InstanceApi()
var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully. Returned data: ' + data);
  }
};
api.expiry(callback);

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 messengers 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 messengers on your smartphone, go to Settings -> messengers 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.


How to Build

The generated SDK relies on Node Package Manager (NPM) being available to resolve dependencies. If you don't already have NPM installed, please go ahead and follow instructions to install NPM from here. The SDK also requires Node to be installed. If Node isn't already installed, please install it from here

NPM is installed by default when Node is installed

To check if node and npm have been successfully installed, write the following commands in command prompt:

  • node --version
  • npm -version

Version Check

Now use npm to resolve all dependencies by running the following command in the root directory (of the SDK folder):

npm install

Resolve Dependencies

Resolve Dependencies

This will install all dependencies in the node_modules folder.

Once dependencies are resolved, you will need to move the folder messengersAPILib in to your node_modules folder.

How to Use

The following section explains how to use the library in a new project.

1. Open Project Folder

Open an IDE/Text Editor for JavaScript like Sublime Text. The basic workflow presented here is also applicable if you prefer using a different editor or IDE.

Click on File and select Open Folder.

Open Folder

Select the folder of your SDK and click on Select Folder to open it up in Sublime Text. The folder will become visible in the bar on the left.

Open Project

2. Creating a Test File

Now right click on the folder name and select the New File option to create a new test file. Save it as index.js Now import the generated NodeJS library using the following lines of code:

var lib = require('lib');

Save changes.

Create new file

Save new file

3. Running The Test File

To run the index.js file, open up the command prompt and navigate to the Path where the SDK folder resides. Type the following command to run the file:

node index.js

Run test

How to Test

These tests use Mocha framework for testing, coupled with Chai for assertions. These dependencies need to be installed for tests to run. Tests can be run in a number of ways:

Method 1 (Run all tests)

  1. Navigate to the root directory of the SDK folder from command prompt.
  2. Type mocha --recursive to run all the tests.

Method 2 (Run all tests)

  1. Navigate to the ../test/Controllers/ directory from command prompt.
  2. Type mocha * to run all the tests.

Method 3 (Run specific controller's tests)

  1. Navigate to the ../test/Controllers/ directory from command prompt.
  2. Type mocha messengers API documentationController to run all the tests in that controller file.

To increase mocha's default timeout, you can change the TEST_TIMEOUT parameter's value in TestBootstrap.js.

Run Tests

Other messengers API SDKs

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