-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsupport.js
228 lines (215 loc) · 6.53 KB
/
support.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
'use strict'
const fs = require('fs')
const path = require('path')
const assert = require('assert')
const reload = require('./reloader')
const ticket = reload(path.join(__dirname, 'ticket.js'))
function ProcessError(message) {
this.name = 'ProcessError';
this.message = message || 'Ticketing error';
this.stack = (new Error()).stack;
}
ProcessError.prototype = Object.create(Error.prototype);
ProcessError.prototype.constructor = ProcessError;
const actions = [
{
action: (_data, _message) => 'DEFAULT ACTION',
regexp: /^$/i,
reply: (message, _output) => `Yes ${message.userName}? Ask me for help if you need me.`,
},
{
action: (_data, _message) => help(),
regexp: /help/i,
reply: (message, output) => `\n${output}`,
},
{
action: (data, _message, id) => ticket.closeTicket(data.tickets, id),
regexp: /close #?([0-9]*)/i,
reply: (message, output) => `ticket #${output.id} is closed`,
},
{
action: (data, message, content) =>
ticket.openTicket(data.tickets, message, content.trim()),
regexp: /please (.*)/i,
reply: (message, output) => `will ${output.content} (ticket #${output.id})`,
},
{
action: (data, _message, id) => ticket.findTicket(data.tickets, id),
regexp: /show #?([0-9]*)/i,
reply: (message, output) => ticket.showTicket(output),
},
{
/* eslint-disable max-params */
action: (data, _m, id, comment) => ({
'comment': comment,
'ticket': ticket.findTicket(data.tickets, id),
}),
regexp: /ticket #?([0-9]*) (.*)$/i,
reply: (message, output) => ticket.addComment(
output.ticket, output.comment),
},
{
action: (data, message, id) => ticket.assign(
data.tickets, id, message.userName),
regexp: /take #?([0-9]*)/i,
reply: (message, output) => `ticket #${output.id} is assigned to ${output.assignee}`,
},
{
/* eslint-disable max-params */
action: (data, _message, id, assignee) =>
ticket.assign(data.tickets, id, assignee),
regexp: /assign #?([0-9]*) to (\w*)/i,
reply: (message, output) => `ticket #${output.id} is assigned to ${output.assignee}`,
},
{
action: (data, _message) => data.tickets,
regexp: /debug/i,
reply: (message, output) => JSON.stringify(output, null, 4),
},
{
action: (data, message, username) => ({
'tickets': ticket.filterTicketsAnd(
username ? ticket.userNameFilter(
data.tickets, username) : data.tickets
, {
'roomName': message.roomName,
'status': 'open'
}),
}),
regexp: /todo ?(\w*)/i,
reply: (message, output) => ticket.showTickets(output.tickets),
},
{
action: (data, message) => ({
'tickets': ticket.filterTicketsOr(
ticket.userNameFilter(data.tickets, message.userName)
, {
status: 'open'
})
}),
regexp: /mine/i,
reply: (message, output) => ticket.showTickets(output.tickets),
},
{
action: (data, message) => ({
'tickets': ticket.filterTicketsOr(data.tickets, {
'roomName': message.roomName,
}),
}),
regexp: /history/i,
reply: (message, output) => ticket.showTickets(output.tickets),
},
{
action: (data, _message, id) => ticket.forgetTicket(data.tickets, id),
regexp: /forget #?([0-9]*)/i,
reply: (message, output) => `deleted ticket #${output}`,
},
{
action: (_data, _message, _type) => true,
regexp: /meaning of life/i,
reply: (_message, _output) => '42',
},
{
action: (_data, _message, _type) => true,
regexp: /high five/i,
reply: (_message, _output) => 'ヘ( ^o^)ノ\(^_^ )',
},
{
action: (data, _message, type) => gimme(data.fs, type),
regexp: /gimme\s*a*n*\s*(\w*)/i,
reply: (message, output) => `${message.userName} here have a ${output}`,
},
{
action: (data, _message, type) => listType(data.fs, type),
regexp: /what\s*(\w*)s?/i,
reply: (message, output) => `\n${output.join('\n')}`,
},
{
action: (data, _message, prompt) => prompt,
regexp: /^(hello|hi|good [\s\w]*|happy [\s\w]*|yo|bonjour|bonsoir|nihao|hola)/i,
reply: (message, output) => `${output} ${message.userName}`,
},
{
action: (_data, _message, _type) => true,
regexp: /.*\?$/i,
reply: (message, _output) => `${message.userName} I don't understand the question`,
},
]
function help() {
return actions.map((action) => `${action.regexp.toString()}`).join('\n')
}
var sources = {}
function gimme(injectedFs, type) {
const typeList = listType(injectedFs, type)
const randomIndex = Math.floor(Math.random() * sources[type].length)
const result = typeList[randomIndex]
return result
}
function listType(injectedFs, type) {
const typePath = path.join('node_modules', 'candobot-data', 'data', path.basename(`${type}.txt`))
/* eslint-disable no-sync */
if(!injectedFs.existsSync(typePath)) {
throw new ProcessError(`I don't know how to give you ${type}!`)
}
if(!sources[type]) {
/* eslint-disable no-sync */
const content = injectedFs.readFileSync(typePath)
sources[type] = content
.toString()
.trim()
.split('\n')
}
return sources[type]
}
/**
* Callback for Array.find.
* @param action action object
* @this object with content and result
* @returns true if match regexp, false otherwise
*/
function searchRegexp(action) {
if(action && action.regexp) {
const exec = action.regexp.exec(this.content)
if(exec) {
this.result = exec.slice(1)
return true
}
}
return false
}
function process(data, message) {
assert(ticket.isValidTickets(data.tickets),
`Expecting valid tickets data key, got: ${JSON.stringify(data.tickets)}`)
const findParams = {
content: message.content,
result: null
}
const action = actions.find(searchRegexp, findParams)
var reply;
if(action) {
try {
var output = action.action(data, message, ...findParams.result)
reply = `${action.reply.bind(output)(message, output)}`
} catch(e) {
if(e instanceof ProcessError) {
return e.message
}
throw e
}
} else {
reply = `I don't understand: "${message.content}". Can you try again?`
}
return `#${message.prefix}: ${reply}`
}
function store(storePath, tickets) {
return ticket.store(fs, storePath, tickets)
}
function load(storePath) {
return ticket.load(fs, storePath)
}
module.exports = {
ProcessError,
load,
process,
store,
}