-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
348 lines (296 loc) · 12.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* eslint-disable func-names */
/* eslint quote-props: ["error", "consistent"]*/
/**
* TIBCO BPM Demo Skill
**/
'use strict';
var AWS = require('aws-sdk');
var request = require('sync-request');
const Alexa = require('alexa-sdk');
const APP_ID = undefined; // TODO replace with your app ID (OPTIONAL).
var loginDetails;
//enable Demo Mode without TIBCO ActiveMatrix BPM Server
const demoMode = true;
const languageStrings = {
'en': {
translation: {
SKILL_NAME: 'TIBCO Case Management',
WELCOME: 'Welcome to your Case Management',
YOU_HAVE: 'You have currently ',
WORKITEMS: ' active Workitems in your Inbox',
CASES: ' active Cases',
HELP_MESSAGE: 'You can say ask me: „my Workitems“ or „my Cases“... What can I help you with?',
HELP_REPROMPT: 'What can I help you with?',
MORE_QUESTION: 'Do you have more Questions for me?',
NO_SERVER: 'could not reach the currently configured TIBCO BPM Demo Server.',
NO_LOGIN: 'Combination of Username and Password wrong defined.',
DEMO_USER: 'Demo User',
STOP_MESSAGE: 'Goodbye!',
},
},
'en-US': {
translation: {
SKILL_NAME: 'TIBCO Case Management',
},
},
'en-GB': {
translation: {
SKILL_NAME: 'TIBCO Case Management',
},
},
'de': {
translation: {
SKILL_NAME: 'TIBCO Vorgangssteuerung',
WELCOME: 'Willkommen zur Vorgangssteuerung',
YOU_HAVE: 'Du hast zur Zeit ',
WORKITEMS: ' offene Aufgaben in deinem Postfach',
CASES: ' offene Vorgänge',
HELP_MESSAGE: 'Du kannst sagen, „meine Aufgaben“, oder du kannst „meine Vorgänge“ sagen ... Wie kann ich dir helfen?',
HELP_REPROMPT: 'Wie kann ich dir helfen?',
MORE_QUESTION: 'Hast du weitere Fragen an mich?',
NO_SERVER: 'Der aktuell konfigurierte TIBCO BPM Demo Server konnte nicht erreicht werden.',
NO_LOGIN: 'Kombination Benutzername und Password falsch angegeben.',
DEMO_USER: 'Demo Anwender',
STOP_MESSAGE: 'Bis zum nächten mal!',
},
},
};
const handlers = {
'LaunchRequest': function () {
this.emit('Welcome');
},
'Welcome': function () {
var speechOutput;
speechOutput = this.t('WELCOME');
const imageObj = {
//only https URLs allowed here
smallImageUrl: 'https://www.tibco.com/blog/wp-content/uploads/2015/08/tibco-logo.jpg',
largeImageUrl: 'https://www.tibco.com/blog/wp-content/uploads/2015/08/tibco-logo.jpg'
};
if (demoMode) {
const reprompt = this.t('HELP_MESSAGE');
speechOutput = this.t('WELCOME') + ", " + this.t('DEMO_USER');
this.emit(':askWithCard', speechOutput, reprompt, this.t('SKILL_NAME'), speechOutput, imageObj);
} else {
try {
const reprompt = this.t('HELP_MESSAGE');
loginDetails = performLogin();
if (loginDetails.errorCode != 200) {
speechOutput = this.t('NO_LOGIN');
} else {
speechOutput = this.t('WELCOME') + " " + loginDetails.displayName;
}
this.emit(':askWithCard', speechOutput, reprompt, this.t('SKILL_NAME'), speechOutput, imageObj);
} catch(e) {
speechOutput = this.t('NO_SERVER');
this.emit(':tellWithCard', speechOutput, this.t('SKILL_NAME'), speechOutput, imageObj);
}
}
},
'myCases': function () {
var speechOutput;
if (demoMode) {
const count = Math.floor(Math.random() * 10);
speechOutput = this.t('YOU_HAVE') + count + this.t('CASES');
} else {
var contrats = listContrat(1, loginDetails.auth);
speechOutput = this.t('YOU_HAVE') + contrats.length + this.t('CASES');
}
const reprompt = this.t('MORE_QUESTION');
this.emit(':askWithCard', speechOutput, reprompt, this.t('SKILL_NAME'), speechOutput);
},
'myWorkitems': function () {
var speechOutput;
if (demoMode) {
const count = Math.floor(Math.random() * 10);
speechOutput = this.t('YOU_HAVE') + count + this.t('WORKITEMS');
} else {
var wicount = getWorkitems(loginDetails.auth);
speechOutput = this.t('YOU_HAVE') + wicount + this.t('WORKITEMS');
}
const reprompt = this.t('MORE_QUESTION');
this.emit(':askWithCard', speechOutput, reprompt, this.t('SKILL_NAME'), speechOutput);
},
'AMAZON.HelpIntent': function () {
const speechOutput = this.t('HELP_MESSAGE');
const reprompt = this.t('HELP_MESSAGE');
this.emit(':ask', speechOutput, reprompt);
},
'AMAZON.CancelIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'AMAZON.StopIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
};
exports.handler = function (event, context) {
const alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
// To enable string internationalization (i18n) features, set a resources object.
alexa.resources = languageStrings;
alexa.registerHandlers(handlers);
alexa.execute();
};
// ----------------------------------------------------------
// Activematrix BPM API Calls
// ----------------------------------------------------------
function getOrgResources(auth) {
// Call the AMX:BPM Service Process PF to request the status. We will get back his existing contracts
var apiPath = '/orgresource/get/-1';
// Construct the body of the request
//var payload = {};
//payload['@payloadMode'] = 'JSON';
//payload['serializedPayload'] = "";
//var reqBody = { payload: payload };
var data = runAPIcall(auth, 'GET', apiPath);
console.log('Returned body: ' + data);
var resourceObj = data.resource;
if (resourceObj instanceof Array) {
resourceObj = data.resource[0];
}
return resourceObj['@guid'];
}
function getWorkitems(auth) {
// Call the AMX:BPM Service Process PF to request the status. We will get back his existing contracts
var apiPath = '/worklist/items/RESOURCE/'+loginDetails.guid+'/0/1';
var data = runAPIcall(auth, 'GET', apiPath);
console.log('Returned body: ' + data);
return data.totalItems;
}
function listContrat(id, auth) {
// Call the AMX:BPM Service Process PF to request the status. We will get back his existing contracts
console.log('checking ClientID: ' + id);
var pfPath = '%2FCustomerportal-chatbot%2FProcess%20Packages%2FCustomerportalChatbot.xpdl/myContrats/1.0.0';
// Construct the body of the request
var payload = {};
payload['@payloadMode'] = 'JSON';
payload['serializedPayload'] = "{\"items\":[{\"$param\":\"Request\", \"mode\":\"IN\", \"$value\":{\"$type\":\"com.example.customer.portal.chatbot.myContratsRequest\", \"clientID\": \""
+ id + "\"}}]}";
var reqBody = { payload: payload };
var data = runPFServiceProcess(auth, pfPath, reqBody);
console.log('Contrat[0]: ' + data[0].numeroCompte);
return data;
}
// ----------------------------------------------------------
// These are generic AMX:BPM API Calls used by the chatbot
// ----------------------------------------------------------
function performLogin() {
// ActiveMatrix BPM Config params
var bpmUser = process.env.BPMUSER || 'tibco-admin';
var bpmPw = process.env.BPMPW || 'secret';
var bpmRestUrl = process.env.BPMRESTURL || 'http://localhost:8080/bpm/rest';
var loginDetails = {
login: bpmUser,
password: bpmPw,
cookie: null,
claims: null,
firstName: '',
lastName: '',
errorCode: 200,
errorMsg: ''
};
loginDetails.auth = 'Basic ' + new Buffer(bpmUser + ':' + bpmPw).toString('base64');
// Get the details of this user
var requestUrl = bpmRestUrl + '/orgresource/get/-1';
var orgResource = request('GET', requestUrl, {
'headers': {
'Authorization': loginDetails.auth,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
console.log('Login response = ' + orgResource.statusCode);
if (orgResource.statusCode != 200) {
loginDetails.errorCode = orgResource.statusCode;
loginDetails.errorMsg = orgResource.body.toString('utf-8');
loginDetails.auth = null;
console.log('Failed to login: ' + loginDetails.errorMsg);
return loginDetails;
}
var orgResourceObject = JSON.parse(orgResource.body.toString('utf-8'));
console.log('Returned body: ' + orgResource.body.toString('utf-8'));
var resourceObj = orgResourceObject.resource;
if (resourceObj instanceof Array) {
resourceObj = orgResourceObject.resource[0];
}
loginDetails.displayName = resourceObj['@name'];
loginDetails.guid = resourceObj['@guid'];
console.log('Logged in as: ' + loginDetails.displayName);
return loginDetails;
}
function runPFServiceProcess(auth, pfPath, body) {
// This call assumes your service process only returns one simple string response
var options = {
'headers': {
'Authorization': auth,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
if (body) {
options['body'] = JSON.stringify(body);
}
// ActiveMatrix BPM Config params
var bpmRestUrl = process.env.BPMRESTURL || 'http://localhost:8080/bpm/rest';
var requestUrl = bpmRestUrl + '/pageflow/start/' + pfPath;
console.log('Request URL = ' + requestUrl);
var response = request('POST', requestUrl, options);
console.log('Request Status Code = ' + response.statusCode);
if (response.statusCode != 200) {
console.log('Rest call failed: ' + response.body.toString('utf-8'));
return -1;
}
var returnedData = null;
var responseObject = JSON.parse(response.body.toString('utf-8'));
console.log('Returned body: ' + response.body.toString('utf-8'));
var frags = responseObject['xml-fragment'];
if (frags && frags.pageData.payload.serializedPayload) {
var payload = JSON.parse(frags.pageData.payload.serializedPayload);
console.log('Payload: %s', JSON.stringify(payload));
if (payload.items) {
for (var i = 0; i < payload.items.length; i++) {
if (payload.items[i].mode == 'OUT') {
returnedData = payload.items[i]['$value'];
if (returnedData instanceof Array) {
returnedData = payload.items[i]['$value']; //JGR: orginal with [0] at the end to return always no arrys
}
console.log('Returning data: %s', JSON.stringify(returnedData));
break;
}
}
} else {
console.log('No items in payload');
}
} else {
console.log('Rest response does not have payload');
}
return returnedData;
}
function runAPIcall(auth, methode, apiPath, body) {
// This call assumes your service process only returns one simple string response
var options = {
'headers': {
'Authorization': auth,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
if (body) {
options['body'] = JSON.stringify(body);
}
// ActiveMatrix BPM Config params
var bpmRestUrl = process.env.BPMRESTURL || 'http://localhost:8080/bpm/rest';
var requestUrl = bpmRestUrl + apiPath;
console.log('Request URL = ' + requestUrl);
var response = request(methode, requestUrl, options);
console.log('Request Status Code = ' + response.statusCode);
if (response.statusCode != 200) {
console.log('Rest call failed: ' + response.body.toString('utf-8'));
return -1;
}
var returnedData = null;
var responseObject = JSON.parse(response.body.toString('utf-8'));
console.log('Returned body: ' + response.body.toString('utf-8'));
returnedData = responseObject['xml-fragment'];
return returnedData;
}