-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionsbkp20180402.js
91 lines (80 loc) · 2.6 KB
/
actionsbkp20180402.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
/**
* Print out Verse API data for all the action samples
*/
window.addEventListener("message", function(event) {
// Add check for the event origin here
console.log(event);
document.getElementById("status").innerHTML = "";
var jsonNode = document.getElementById("json");
var a = JSON.stringify(event.data.verseApiData);
processEmail(a, function (mailContent) {
if(mailContent) {
console.log('processing');
var data = {"textToAnalyze": mailContent};
$.ajax({ url: "https://openwhisk.ng.bluemix.net/api/v1/web/ecodadmi%40us.ibm.com_cheoksv-dev/default/cheok-nlu.json", data, method: 'POST' })
.done(function(result) {
// jsonNode.innerText = mailContent;
jsonNode.innerText = JSON.stringify(result, null, 2);
jsonNode.innerText += "\n\nOriginal email content: \n"+mailContent;
})
}
else {
console.log('still no content yet..')
}
/**
* Message from Verse to check whether your web application is ready.
*/
if (event.data.verseApiType === "com.ibm.verse.ping.application.loaded") {
var loaded_message = {
verseApiType: 'com.ibm.verse.application.loaded'
};
/**
* Your application must send a message back to Verse
* to identify that it's ready to receive data from Verse.
*/
event.source.postMessage(loaded_message, event.origin);
}
})
function processEmail (a, cb) {
return new Promise(function (resolve, reject) {
//remove all the html tags
var p = '';
var opentag = false;
for(var i in a) {
if(a[i] == '>') {
opentag = false;
}
else if(opentag || a[i] == '<') {
opentag = true;
}
else {
p += a[i];
}
}
//remove all the whitespace, , and whitespaces (consecutive 2 or more)
p = p.replace(/(?:\r\n|\r|\n)/g, '').replace(/( )/g, '').replace(/\s\s+/g, '');
var q = '';
var skip = false;
for(var i in p) {
if(skip) {
skip = false;
continue;
}
else if(p[i] == '\\')
skip = true;
else
q += p[i];
}
// //manually parse the mail content, as JSON.parse(q) is not woring for some reason..
var closeInd = 0;
for(var i = (q.indexOf('"body":"')+8); i <= q.length; i++) {
if(q[i] == '"') {
closeInd = i;
break;
}
}
var mailContent = q.substring((q.indexOf('"body":"')+8), closeInd);
return resolve (cb(mailContent));
})
}
}, false);