constructor() { this.express = express(); epimetheus.instrument(this.express) this.assistant = new watson.AssistantV2({ version: '2018-11-08', iam_apikey: process.env.APIKEY, url: 'https://gateway-lon.watsonplatform.net/assistant/api' }); console.log(this.assistant); this.middleware(); this.routes(); }
// New Watson Assistant service Session router.get('/new-session', function(req, res) { assistant.createSession({ assistant_id: process.env.ASSISTANT_ID, }, function(err, response) { var sessionData; if (err) { console.error(err); } else{ console.log(JSON.stringify(response, null, 2)); sessionData = res.json(response); } return sessionData; }); });
/** * Query for 5 documents using the Watson Discovery Service * @param {string} topic - The topic to query for * @returns {promise} */ function query(topic) { var promise = new Promise(function(resolve, reject) { discovery.query({ environment_id : config.DISCOVERY.env_id, collection_id : 'news-en', query : topic, count : NUM_DOCS_TO_QUERY }, function(error, data) { if (error) { reject(error); } else { resolve(data); } }); }); return promise; }
// NOTE, due to the use of async/await, this file is not intended to be imported by the front-end // TODO investigate escaping queries before sending to the discovery service // This wrapper sets the discover service up and connects to the proper discovery environment and collection before making a query. // // All the params are passed straight through. function makeQuery(params) { const discovery = new DiscoveryV1({ version: '2017-08-01' }) // Using a Promise to allow the use of async/await in other code, this allows calls without callback hell which is important in // finding the correct collection, it requires both the environment and a discovery instance. return new Promise(async (resolve, reject) => { // Watson News now uses pre-defined env and coll id values params.environment_id = 'system' params.collection_id = 'news' // The SDK isn't setup for promises, wrapping in one so that we may use async/await with other calls to these functions discovery.query(params, (error, data) => { if (error) { reject(error) } else { resolve(data) } }) }) }
assistant.message(payload, function(err, data) { var returnObject = null; if (err) {
assistant.message(payload, function (err, response) { if (err) { context = response.context; if(!response.output.text[0].includes("action")){ "message":response.output.text[0] else if(JSON.parse(response.output.text[0]).action == 'forward') outputd = JSON.parse(response.output.text[0]); else if(JSON.parse(response.output.text[0]).action == 'process') if(JSON.parse(response.output.text[0]).data.function == 'Prepaid') outputd = JSON.parse(response.output.text[0]); else if(JSON.parse(response.output.text[0]).data.function == 'Postpaid') outputd = JSON.parse(response.output.text[0]); else if(JSON.parse(response.output.text[0]).data.function == 'Broadband') outputd = JSON.parse(response.output.text[0]); else if(JSON.parse(response.output.text[0]).data.function == 'SimCard')