Skip to content

Extended TIE client that accepts additional custom request headers and times out after 20 seconds

License

Notifications You must be signed in to change notification settings

jolzee/leopard-tie-client

Repository files navigation

leopard-tie-client

Node NPM Travis David Gitmoji NPM

Extended TIE client that accepts additional custom request headers and times out after 20 seconds

Usage

import TIE from 'leopard-tie-client';

Installation

Install via yarn

yarn add leopard-tie-client

or npm

npm install leopard-tie-client

Examples

See the runkit example.

Builds

If you don't use a package manager, you can access leopard-tie-client via unpkg (CDN), download the source, or point your package manager to the url.

leopard-tie-client is compiled as a collection of CommonJS modules & ES2015 modules for bundlers that support the jsnext:main or module field in package.json (Rollup, Webpack 2)

The leopard-tie-client package includes precompiled production and development UMD builds in the dist/umd folder. They can be used directly without a bundler and are thus compatible with many popular JavaScript module loaders and environments. You can drop a UMD build as a <script> tag on your page. The UMD builds make leopard-tie-client available as a window.leopardTieClient global variable.

Usage

Example usage

const TIE = require('leopard-tie-client');

const teneoEngineUrl = 'https://some.teneo/engine-instance';
const logResponse = response => {
  console.log(response);
  return response;
};

TIE.sendInput(teneoEngineUrl, null, { text: 'My name is Peter' })
  .then(logResponse)
  .then(({ sessionId }) => TIE.sendInput(teneoEngineUrl, sessionId, { text: 'What is my name?' }))
  .then(logResponse)
  .then(({ sessionId }) => TIE.close(teneoEngineUrl, sessionId))
  .catch(err => console.error(err));

Note that when used as a Node.js module, you need to manually handle the session by passing the session ID to the API functions.

Example usage

const teneoEngineUrl = 'https://some.teneo/engine-instance';
const logResponse = response => {
  console.log(response);
  return response;
};

TIE.sendInput(teneoEngineUrl, null, { text: 'My name is Peter' })
  .then(logResponse)
  .then(({ sessionId }) => TIE.sendInput(teneoEngineUrl, null, { text: 'What is my name?' }))
  .then(logResponse)
  .then(({ sessionId }) => TIE.close(teneoEngineUrl))
  .catch(err => console.error(err));

Note that in the browser the session is maintained via cookies and the API cannot manually override the browser's handling of the session. That means that you never need (nor should) pass the session ID when using the API in the browser.

A note on CORS

TIE API Client needs to send a cookie to the Teneo Engine instance in order for the session to be maintained. This requires CORS communication to be setup between the place where the TIE API Client is and the Teneo Engine instance. The TIE API Client handles this by sending the value of document.location.origin to the Teneo Engine instance as a parameter.

By default the Teneo Engine includes CORS headers in the responses to browser requests coming from any domain. This means any site can interact with a Teneo Engine. If you want to restrict your engine to only include CORS headers for specific domains, you can add a file called tieapi_cors_origins.txt to your solution. You can upload this file in Teneo Studio in the Teneo Resource Manager where you should add it to the views folder. The file should contain the list of allowed origin domains (one domain per line, domains should include port numbers if applicable).

API Documentation

TIE.sendInput

Sends inputData to the url. Returns a Promise if no callback is provided.

Signature
TIE.sendInput((url: string), (sessionId: string), (inputData: object), [(callback: function)]);
Parameters

url: URL to a running Teneo Engine instance.

sessionId: Session id to be passed to the Teneo Engine instance. Pass null if unknown. Also not required if TIE client is used in the browser. The browser will automatically set and return session cookies.

inputData: An object taking the shape:

{
  text: "Some input text",
  channel: "webview",
  command: "login",
  someParam: "foo",
  anotherParam: "bar",
  headers: {
	  Authorization: "Basic blahblahblah==",
	  Date: "Tue, 15 Nov 1994 08:12:31 GMT",
	  otherHeaderName: "some header value"
  }
}

All properties except text and headers will be sent to the Teneo Engine instance as extra parameters.

The headers property if not required. Although if your TIE resides behind a reverse proxy and you need to pass request headers for say authorization then you have a means of setting any custom headers.

callback(error: Error, response: teneoEngineResponse) [Optional]: Callback for handling the response from the Teneo Engine instance.

TIE.close

Closes the running (or specified session). Returns a Promise if no callback is provided.

Signture
TIE.close((url: string), (sessionId: string), [(callback: function)]);
Parameters

url: URL to a running Teneo Engine instance.

sessionId: Session id to be passed to the Teneo Engine instance. Pass null if unknown.

callback(error: Error, response: TeneoEngineResponse) [Optional]: Callback for handling the response from the Teneo Engine instance.

TIE.init

Returns a version of the Teneo Interaction Engine API with the Teneo Engine url prefilled.

> const teneoApi = TIE.init('https://some.teneo/engine-instance');
> teneoApi.sendInput(null, { text: 'Sending some text to the prefilled url' })
    .then(response =>
      console.log(response);
      return teneoApi.close(response.sessionId);
    }).catch(error => console.error(error));
Signature
TIE.init((url: string));
Parameters

url: URL to a running Teneo Engine instance.

TeneoEngineResponse

Response from the Teneo Interaction Engine API.

Normal response:

{
  "status": 0,
  "input": {
    "text": "input text",
    "parameters": {}
  },
  "output": {
    "text": "output text",
    "emotion": "",
    "link": "",
    "parameters": {}
  },
  "sessionId": "current session id"
}

Error response:

{
  "status": -1,
  "input": {
    "text": "input text",
    "parameters": {}
  },
  "message": "ERROR MESSAGE"
}

Misc

This module was created using generator-jolzee-node-module.

This module provides a way of communicating with a Teneo Engine instance either on the server as a NodeJS module or in a browser loaded through a script node.

License

The code is available under the MIT license.

Contributing

We are open to contributions, see CONTRIBUTING.md for more info.

About

Extended TIE client that accepts additional custom request headers and times out after 20 seconds

Resources

License

Stars

Watchers

Forks

Packages

No packages published