bluechat
v0.1.4
Published
This is a generic chatbots backend, where you can enter your assistant's credentials and register an environment variable check, from a previously determined model, and make external requests. - This is an alpha version
Downloads
1
Readme
Bluechat
This is a generic chatbots backend, where you can enter your assistant's credentials and register an environment variable check, from a previously determined model, and make external requests.
- This is an alpha version
const bluechat = require('bluechat');
bluechat.initEngine();
bluechat.initServer();
Installing
Import the module
npm install bluechat
Prepare the environment
- In your home directory, create a file called .env
- env
Here you will enter your Watson Assistant credentials and the Port for the server to run, as shown below:
PORT= ASSISTANT_APIKEY= ASSISTANT_URL= ASSISTANT_ID=
- env
Routes
Session Id
Request
Here you will make an initial request, to obtain the 'session_id', which will be used to let the Assistant know which session the messages sent are from.
GET / <url>:<port>
example:
GET / localhost:3000
Response
The response will be like this:
{ "data": { "session_id": "<id>" } }
Message
Request
Here you will make a request passing in the parameters the 'session_id' obtained in the initial request, and in the body a json with key 'text' and the message entered by the user.
POST <url>:<port>/<session_id> Content-Type: application/json { "text": "<message>" }
example:
POST localhost:3000/3d86a86b-6b3a-483c-98c0-9098f21fde22 Content-Type: application/json { "text": "corona" }
Response
The response will be like this:
{ "data": { "response": { "messages": [ { "response_type": "text", "text": "<message>" } ], "total": 1 }, "context": "" } }
Methods
initEngine
- This is the initial method of the module, as the name says, starts the engine, when this method is executed, the connection with ibm-watson is made and the connection of the other internal methods for the module's operation is also made.
bluechat.initEngine();
initServer
- This method starts the server by listening to the port inserted in the .env file.
bluechat.initServer();
createContext
- In this method you will register a context variable and insert a callback function to be executed when returning the context variable registered in watson
- There is no limit on the context variables registered.
example:bluechat.createContext(contextVariable, function);
- You can assemble the message or pass the data on an object to use a generic view that uses the ibm response pattern.
- In this example we will use the second one.
bluechat.createContext('color', (contexts) => { let response = { type: 'option', title: 'Choose the color', options: [] }; for (let color of contexts.colors) { response.options.push({ label: color, value: { input: { text: color } } }); } return response; });
- List of object parameters to use the generic view
- option
"type": "option", "title": "<title>", "options": [options]
- pause
"type": "pause", "time": "<time>", "typing": true/false
- suggestion
"type": "suggestion", "title": "<title>", "suggestion": [suggestion]
- image
"type": "image", "source": "<source>"
- text
"type": "text", "text": "<text>"
- option
use
- This is a middleware method, it is called at the insertion position at the beginning/middle/end of the inserted context variables.
example:bluechat.use(function);
bluechat.createContext(contextVariable, function); bluechat.use((reject, contexts) => { if (contexts.userPrivileges) return reject(); else return true; }); bluechat.createContext(contextVariable, function);