@artisol/teneo-interaction-engine-sdk
v0.2.1
Published
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.
Downloads
4
Readme
Teneo Interaction Engine Javascript API
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.
Usage
Node.js
Example usage
const TIE = require('@artisol/teneo-interaction-engine-sdk');
const teneoEngineUrl = 'https://some.teneo/engine-instance';
const logResponse = (response) => {
console.log(response.output);
return response;
}
TIE.sendInput(teneoEngineUrl, null, { text: 'Hello there' })
.then(logResponse)
.then(({ sessionId }) => TIE.sendInput(teneoEngineUrl, sessionId, { text: 'How are you doing?' }))
.then(logResponse)
.then(({ sessionId }) => TIE.close(teneoEngineUrl, sessionId));
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.
Browser
When using the API in the browser, you need to first have a bundled version of the API that you can load on the page. You can generate a bundled version of the API by executing the npm run build
script (see section on local development). The bundled file will be placed in the dist/ directory.
Example usage
const teneoEngineUrl = 'https://some.teneo/engine-instance';
const logResponse = (response) => {
console.log(response.output);
return response;
}
TIE.sendInput(teneoEngineUrl, null, { text: 'Hello there' })
.then(logResponse)
.then(() => TIE.sendInput(teneoEngineUrl, null, { text: 'How are you doing?' }))
.then(logResponse)
.then(() => TIE.close(teneoEngineUrl));
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.
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.
inputData: An object taking the shape:
{
text: "Some input text",
someParam: "foo",
anotherParam: "bar"
}
All properties except text will be sent to the Teneo Engine instance as extra parameters.
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('http://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);
});
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"
}
Setting up the project for local development
- Ensure you have supported versions of Node and NPM installed (see the
engines
property in package.json) - Run
npm install
- Run
npm test