-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
107 lines (90 loc) · 3.43 KB
/
Copy pathbot.js
File metadata and controls
107 lines (90 loc) · 3.43 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
103
104
105
106
107
const builder = require('botbuilder');
const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
const logMessage = require('./middleware/logMessages');
//import of messages.JSON
const botMsg = require('./messages/de_DE');
//import dialogs
const qnaDialog = require('./qnadialog');
const acceptDsgvoDialog = require('./acceptDsgvo');
// Make a new bot and set the QNA dialog as the default dialog. That way, the
// bot will enter QNA mode whenever there is no other active dialog on the
// stack.
const bot = new builder.UniversalBot(connector, [
function (session) {
session.beginDialog('acceptDsgvo');
},
function (session) {
session.beginDialog('qnadialogStart');
},
function (session) {
session.beginDialog('qna');
},
] );
//acceptDSGVO dialog triggered after conversationUpdate start message
bot.dialog('acceptDsgvo', acceptDsgvoDialog);
//show qnadialogStart dialog message
bot.dialog('qnadialogStart', function (session) {
builder.Prompts.text(session, botMsg.StartQnA);
});
bot.dialog('qna', qnaDialog);
bot.on('conversationUpdate', (message) => {
// Only send message if the bot is added to the conversation. As the bot
// does not enter group conversations, there is no difference if the user
// is greeted by the bot or the bot says hello to everyone in the channel.
const memberAdded = message.membersAdded && message.membersAdded.length > 0;
const botIsAddedMember = message.address && message.address.bot.id === message.membersAdded[0].id;
// Start the greeting
if (memberAdded && botIsAddedMember) {
message.membersAdded.forEach(function (identity){
if(identity.id === message.address.bot.id){
const reply = new builder.Message()
.address(message.address)
.text(botMsg.WelcomeMsg);
bot.send(reply);
} else{
const address = Object.create(message.address);
address.user = identity;
const reply = new builder.Message()
.address(message.address)
.text(botMsg.WelcomeMsg + identity.name);
bot.send(reply);
}
});
}
});
// Middleware for logging
bot.use({
/* send messages from user to bot*/
botbuilder: (session, next) => {
if (!session || !session.message || !session.message.text) {
return next();
}
logMessage.receive(session.message);
if (session.privateConversationData.userAcceptedDSGVO) {
logMessage.persist(session.message.address.conversation.id);
} else {
logMessage.deleteConversation(session.message.address.conversation.id)
}
next();
},
/* send messages from bot to user*/
send: function (message, next) {
if (message.type !== 'message') {
return next();
}
bot.loadSession(message.address, async function (error, session) {
const messageCopy = {...message, address: message.address || session.message.address};
logMessage.send(messageCopy);
if (session.privateConversationData.userAcceptedDSGVO) {
await logMessage.persist(messageCopy.address.conversation.id);
next();
} else {
next();
}
});
},
});
module.exports = bot;