Я пытаюсь добавить элемент очереди в UIPath Orchestrator от выполнения Dialogflow и после получения ответа от Orchestrator, отображаемого в ответе DialogFlow Intent
Index.js
---------
'use strict';
var Orchestrator = require('uipath-orchestrator');
var QUEUE_NAME = 'DialogflowTasks';
var QUEUE_CHECK_INTERVAL = 10000; // 10 secs
var QUEUE_ITEM_OUTCOME = {
UNKNOWN: 'UNKNOWN',
TIMEOUT: 'TIMEOUT',
FAILED: 'TIMEOUT',
SUCCESS: 'SUCCESS'
};
const functions = require('firebase-functions');
const async = require('async');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const config = {
"hostname": "platform.uipath.com",
"tenancyName": "myTenancyName",
"usernameOrEmailAddress": "myUserName",
"password": "myPassword"
};
const orchestrator = new Orchestrator(config);
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
var dataFromUiPath = "Data not found!";
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function OrchestratorQueueItem(specificData) {
this.Name = QUEUE_NAME;
this.SpecificContent = specificData;
this.Reference = '';
}
function waitForQueueItemCompletion(id, elapsed, cb) {
var before = Date.now();
orchestrator.v2.odata.getItemProcessingHistory(
id,
{},
function (err, result) {
var after;
result = result.value[0];
if (err) {
cb(undefined, QUEUE_ITEM_OUTCOME.UNKNOWN, null);
return;
}
if (result.Status === 'Successful') {
cb(undefined, QUEUE_ITEM_OUTCOME.SUCCESS, result);
return;
}
if (result.Status === 'InProgress' || result.Status === 'New') {
after = Date.now();
elapsed += (after - before);
if (elapsed > 60000) {
cb(undefined, QUEUE_ITEM_OUTCOME.TIMEOUT, null);
return;
}
setTimeout(function () {
waitForQueueItemCompletion(id, elapsed, cb);
}, QUEUE_CHECK_INTERVAL);
return;
}
cb(undefined, QUEUE_ITEM_OUTCOME.FAILED, null);
}
);
}
function recoverPassword (search, cb) {
async.waterfall([
function (next) {
orchestrator.v2.odata.postAddQueueItem(
{itemData: new OrchestratorQueueItem({
type: 'recoverPasswordForLoginID',
search: search
})},
function (err, response) {
if (err) {
next(err);
return;
}
next(undefined, response.Id);
}
);
},
function (queueItemId, next) {
setTimeout(function () {
waitForQueueItemCompletion(queueItemId, 0, next);
}, QUEUE_CHECK_INTERVAL);
},
function (outcome, result, next) {
var outputData;
var message = "test";
if (outcome === QUEUE_ITEM_OUTCOME.SUCCESS) {
try {
outputData = JSON.parse(result.OutputData);
outputData = outputData.DynamicProperties.text;
} catch (e) {
console.log('getItemProcessingHistory: ' + e.message);
outputData = undefined;
}
if (outputData) {
//message = 'Password has been recovered successfully.:\n' + outputData;
message = outputData;
console.log(message);
} else {
message = 'Sorry, Password can not be recovered, Enter correct Login ID?';
}
next(undefined, message);
return;
}
console.log('Could not process the item: ' + outcome);
if (outcome === QUEUE_ITEM_OUTCOME.FAILED) {
next(undefined, 'Item failed to process');
} else {
next(undefined, 'Could not confirm the result...');
}
}
], cb);
}
function AccountNumberHandler(agent) {
var AccountNumber = " " ;
AccountNumber = agent.parameters.accountNumber;
if (AccountNumber.length > 0) {
return new Promise((resolve, reject) => {
recoverPassword(AccountNumber, function onComplete(error, data) {
if (!error) {
console.log("Response from UIPATH:" + data);
agent.add(data);
} else {
console.log("Error from recoverPassword:"+error);
}
});
});
}
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('GetAccountPassword - custom', AccountNumberHandler);
agent.handleRequest(intentMap);
});
Package.json
------------
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "8"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "^2.5.0",
"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2",
"dialogflow": "^0.6.0",
"dialogflow-fulfillment": "^0.6.1",
"uipath-orchestrator": "*",
"async": "*"
}
}
В намерении "GetAccountPassword - custom" я получаю номер учетной записиот пользователя, который затем отправляется в Orchestrator как queueItem.Затем Orchestrator возвращает пароль, который должен отображаться в ответе Intent.В функции AccountNumberHandler (agent) результат отображается в консоли, но не отображается в ответе Intent.См. Ниже код в функции AccountNumberHandler (агент):
console.log("Response from UIPATH:" + data);
agent.add(data);