forked from Giveth/giveth-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.js
246 lines (228 loc) · 6.37 KB
/
chatbot.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
const fs = require('fs')
const markdown = require('markdown').markdown
let privateRooms = {}
const {
positiveResponses,
negativeResponses,
messages,
questions,
} = require('./constants')
fs.readFile('./privateRooms.json', 'utf8', function(err, data) {
if (!err) {
privateRooms = JSON.parse(data)
}
})
exports.handleNewMember = function(event, room, toStartOfTimeline, client) {
if (
event.event.membership === 'join' &&
(!event.event.unsigned.prev_content ||
event.event.unsigned.prev_content.membership === 'invite')
) {
const user = event.getSender()
const room = event.getRoomId()
let roomMessages = messages[room]
if (roomMessages && checkUser(user)) {
handleWelcome(
room,
user,
client,
roomMessages.externalMsg,
roomMessages.internalMsg
)
}
}
}
exports.handleResponse = function(event, room, toStartOfTimeline, client) {
if (event.getType() === 'm.room.message' && toStartOfTimeline === false) {
let msg = event.getContent().body
const user = event.getSender()
if (checkUser(user)) {
if (
privateRooms[user] &&
privateRooms[user].welcoming &&
user != client.credentials.userId &&
room.roomId == privateRooms[user].room
) {
let greetingQuestions =
messages[privateRooms[user].welcoming.room].internalMsg
let curQuestion = privateRooms[user].welcoming.curQuestion
let positive = false
let negative = false
positiveResponses.some(response => {
if (msg.includes(response.toLowerCase())) {
positive = true
return true
}
return false
})
negativeResponses.some(response => {
if (msg.includes(response.toLowerCase())) {
negative = true
return true
}
return false
})
if (positive) {
sendInternalMessage(
greetingQuestions[curQuestion].positive,
user,
client
)
} else if (negative) {
sendInternalMessage(
greetingQuestions[curQuestion].negative,
user,
client
)
}
if (positive || negative) {
if (greetingQuestions.length > curQuestion + 1) {
sendNextQuestion(
curQuestion,
greetingQuestions,
user,
client,
privateRooms[user].welcoming.room
)
} else {
privateRooms[user].welcoming = undefined
}
} else {
sendInternalMessage(
"I didn't recognize that response :(",
user,
client
)
}
} else if (
(!privateRooms[user] || !privateRooms[user].welcoming) &&
user != client.credentials.userId
) {
if (privateRooms[user] && privateRooms[user].room == room.roomId) {
for (let key in questions) {
if (
questions.hasOwnProperty(key) &&
checkForRoomQuestions(msg, key, room.roomId, user, client)
) {
break
}
}
} else {
checkForRoomQuestions(msg, room.roomId, room.roomId, user, client)
}
}
} else if (
event.getType() === 'm.room.member' &&
event.event.membership === 'leave'
) {
let privateRoom = privateRooms[event.getSender()]
if (privateRoom && privateRoom.room == event.event.room_id) {
privateRoom.room = undefined
privateRoom.welcoming = undefined
savePrivateRooms()
}
}
}
}
function checkUser(user) {
// Ignore Slack bridge users
return !user.startsWith('@slack_giveth_')
}
function checkForRoomQuestions(
msg,
roomForQuestions,
roomToSendIn,
user,
client
) {
let questionsForRoom = questions[roomForQuestions]
if (questionsForRoom) {
questionsForRoom.forEach(question => {
let shouldAnswerQuestion = false
if (typeof question.trigger === 'string') {
shouldAnswerQuestion = msg
.toLowerCase()
.includes(question.trigger.toLowerCase())
} else {
question.trigger.some(trigger => {
if (msg.toLowerCase().includes(trigger.toLowerCase())) {
shouldAnswerQuestion = true
return true
}
return false
})
}
if (shouldAnswerQuestion) {
sendMessage(question.answer, user, client, roomToSendIn)
return true
}
})
}
return false
}
function handleWelcome(room, user, client, externalMsg, internalMsg) {
if (typeof externalMsg === 'string') {
sendMessage(externalMsg, user, client, room)
}
if (typeof internalMsg === 'string') {
sendInternalMessage(internalMsg, user, client)
} else if (typeof internalMsg === 'object') {
if (
!privateRooms[user] ||
(privateRooms[user] && !privateRooms[user].welcoming)
) {
sendNextQuestion(-1, internalMsg, user, client, room)
}
}
}
function sendNextQuestion(curQuestion, questions, user, client, room) {
curQuestion++
if (privateRooms[user]) {
privateRooms[user].welcoming = { room: room, curQuestion: curQuestion }
}
let question = questions[curQuestion]
sendInternalMessage(question.msg, user, client, () => {
if (!question.positive) {
sendNextQuestion(curQuestion, questions, user, client, room)
}
})
}
function sendInternalMessage(msg, user, client, callback) {
if (privateRooms[user] && privateRooms[user].room) {
sendMessage(msg, user, client, privateRooms[user].room)
if (callback) {
callback()
}
} else {
client
.createRoom({
preset: 'trusted_private_chat',
invite: [user],
is_direct: true,
})
.then(res => {
privateRooms[user] = { room: res.room_id }
savePrivateRooms()
sendMessage(msg, user, client, privateRooms[user].room)
if (callback) {
callback()
}
})
}
}
function sendMessage(msg, user, client, room) {
if (msg.length > 0) {
msg = msg.replace(/^ +| +$/gm, '')
let html = markdown.toHTML(msg)
msg = msg.replace('%USER%', user)
html = html.replace('%USER%', user)
client.sendHtmlMessage(room, msg, html)
}
}
function savePrivateRooms() {
fs.writeFile(
'./privateRooms.json',
JSON.stringify(privateRooms, null, 2),
'utf-8'
)
}