Chatbase Node.JS Client
This is not an official Google Product
Use
Install Via NPM
npm install --save @google/chatbase
Require the client in the target application:
var chatbase = require('@google/chatbase');
Set a valid api key, user id, agent-type, and platform on the imported module to automatically create new messages with these fields pre-populated:
var chatbase = require('@google/chatbase')
.setApiKey(process.env.MY_CHATBASE_KEY)
.setPlatform('PLATFORM-X')
.setAsTypeUser();
var msg = chatbase.newMessage();
assert(msg.api_key === process.env.MY_CHATBASE_KEY);
Or one can set these on each individual message. Note: api key and user id must be provided as arguments to newMessage if one would like to override the factory when it has been previously set.
var chatbase = require('@google/chatbase')
.setAsTypeAgent()
.setPlatform('PLATFORM-Y');
var msg = chatbase.newMessage('my-api-key', 'my-user-id')
.setAsTypeUser()
.setPlatform('PLATFORM-Y');
assert(msg.platform === 'PLATFORM-Y');
All fields, with the exception of user id and api key can be set on a message instance. User id and api key must be given as arguments when the message is instantiated.
var chatbase = require('@google/chatbase');
var msg = chatbase.newMessage('my-api-key', 'my-user-id')
.setAsTypeUser()
.setAsTypeAgent()
.setTimestamp(Date.now().toString())
.setPlatform('PLATFORM-Z')
.setMessage('MY MESSAGE')
.setIntent('book-flight')
.setAsHandled()
.setAsNotHandled()
.setVersion('1.0')
.setUserId('user-1234')
.setAsFeedback()
.setAsNotFeedback()
.setCustomSessionId('123')
.setMessageId('123');
.setClientTimeout(5000)
Once a message is populated, one can send it to the service and listen on its progress using promises. Note that timestamp is not explicitly set here (although it can be) since it is automatically set on the message to the time of instantiation. Note also that the client type does not need to be explictly set either unless an agent client type is required since the message will automatically default to the user type.
var chatbase = require('@google/chatbase');
chatbase.newMessage('my-api-key')
.setPlatform('INTERWEBZ')
.setMessage('CAN I HAZ?')
.setVersion('1.0')
.setUserId('unique-user-0')
.send()
.then(msg => console.log(msg.getCreateResponse()))
.catch(err => console.error(err));
Given that a newly created message can be updated this can be achieved via the message interface as well.
var chatbase = require('@google/chatbase');
chatbase.newMessage('my-api-key', 'my-user-id')
.setPlatform('INTERWEBZ')
.setMessage('DO NOT WORK')
.setVersion('1.0');
.setUserId('unique-user-0')
.send()
.then(msg => {
msg.setIntent('an-intent')
.setAsFeedback()
.setAsNotHandled()
.update()
.then(msg => console.log(msg.getUpdateResponse()))
.catch(err => console.error(err));
})
.catch(err => console.error(err));
Groups of messages can also be queued and sent together:
var chatbase = require('@google/chatbase');
const set = chatbase.newMessageSet()
.setApiKey('abc')
.setPlatform('chat-space');
set.newMessage()
.setMessage('test_1')
.setIntent('book-flight')
.setUserId('unique-user-0')
.setClientTimeout(8000)
const msg = set.newMessage()
.setMessage('test_2')
set.sendMessageSet()
.then(set => {
console.log(set.getCreateResponse());
})
.catch(error => {
console.error(error);
})