-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·102 lines (89 loc) · 3.25 KB
/
app.js
File metadata and controls
executable file
·102 lines (89 loc) · 3.25 KB
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
require('./connectorSetup.js')();
var UserWelcomedKey = 'UserWelcomed';
var showAvatar = 'showAvatar';
var AvatarReminder = 'AvatarReminder';
var AvatarApertura = 'AvatarApertura';
var AvatarPresentation = 'AvatarPresentation';
var JsonPath = require('jsonpath');
var faqs = require('./faqs.json');
var intents = JsonPath.query(faqs, '$.faqs.responses[?(@.intent != "")].intent');
var recognizer = new builder.LuisRecognizer('https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/bb31143e-7f94-4838-aeb3-02b399603fbf?subscription-key=eb17b4ee1a45442c909a3779fcfd00c1');
bot.recognizer(recognizer);
//TODOS - timer must be a user variable;
var timer;
//Bot listening for inbound backchannel events
bot.on("event", function (event) {
var msg = new builder.Message().address(event.address);
msg.textLocale("it-IT");
if (event.name === showAvatar) {
if (event.value === AvatarApertura) {
msg.text("Ciao, sono il nuovo Assistente Virtuale dell'Arma dei Carabinieri");
}
}
bot.send(msg);
});
bot.dialog('apertura', function (session) {
if (!session.privateConversationData[UserWelcomedKey]) {
session.privateConversationData[UserWelcomedKey] = true;
session.send('Ciao, come posso aiutarti?');
} else {
session.send('Ben tornato, come posso aiutarti adesso?');
}
timer = setTimeout(function(){
session.send('La chat è ancora attiva. Fammi una domanda.');
var reply = createEvent(showAvatar, AvatarReminder, session.message.address);
session.endDialog(reply);
},20000);
}).triggerAction({
matches: 'apertura'
});
bot.dialog('chiusura', function (session) {
clearTimeout(timer);
session.endConversation('Grazie per averci contattato.');
session.privateConversationData[UserWelcomedKey] = false;
}).triggerAction({
matches: 'chiusura'
});
bot.dialog('faqs', function (session, args, next) {
clearTimeout(timer);
session.endDialog(retrieveResponse(args.intent.intent));
//args.intent.score - it contains the score value
}).triggerAction({
matches: intents
});
bot.dialog('presentazione', function (session) {
clearTimeout(timer);
session.send('Mi presento');
session.send('Sono il nuovo Assistente Virtuale dell\'Arma dei Carabinieri.');
session.send('Sostituisco la collega');
session.send({
attachments: [
{
contentType: "image/jpeg",
contentUrl: "http://webchatcarabinieri.azurewebsites.net/media/virtual_assistant_carabinieri.jpg",
name: "virtual_assistant_carabinieri.jpg"
}
]
});
session.endDialog('Spero di essere alla sua altezza');
}).triggerAction({
matches: 'presentazione'
});
//Creates a backchannel event
const createEvent = (eventName, value, address) => {
var msg = new builder.Message().address(address);
msg.data.type = "event";
msg.data.name = eventName;
msg.data.value = value;
return msg;
}
function retrieveResponse(intent) {
var output = "";
var response = JsonPath.query(faqs, '$.faqs.responses[?(@.intent === "' + intent + '")].response');
if (response) {
output = response[0];
} else {
output = 'Spiegati meglio';
}
return output;
}