Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72d0e7f

Browse files
committed
add gimme compliment
1 parent e49c42f commit 72d0e7f

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ tickets.json
66
*.lock
77
**.swp
88
.v8flags.*
9+
*.txt
10+
yo.js

bot.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ const data = {}
99
Wechaty.instance() // Singleton
1010
.on('scan', (url, code) => console.log(`Scan QR Code to login: ${code}\n${url}`))
1111
.on('login', user => console.log(`User ${user} logined`))
12-
.on('message', message => { reload('./handler.js')(data, message) })
12+
.on('message', message => {
13+
message.ready().then(() => {
14+
reload('./handler.js')(data, message)
15+
})
16+
})
1317
.init()

support.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const fs = require('fs')
4+
const path = require('path')
45

56
const Sample = {
67
assignee: null,
@@ -105,6 +106,21 @@ function help() {
105106
return actions.map(action => `${action.regexp.toString()}`).join('\n')
106107
}
107108

109+
function getRandomInt(min, max) {
110+
return Math.floor(Math.random() * (max - min + 1) + min);
111+
}
112+
113+
var sources = {}
114+
115+
function gimme(type) {
116+
if(!sources[type]) {
117+
const content = fs.readFileSync(path.basename(`${type}.txt`))
118+
sources[type] = content.toString().split('\n')
119+
}
120+
const saying = sources[type][getRandomInt(0, sources[type].length)]
121+
return saying
122+
}
123+
108124
const actions = [
109125
{
110126
regexp: /^$/gi,
@@ -127,40 +143,45 @@ const actions = [
127143
reply: (message, output) => `will ${output.content} (ticket #${output.id})`,
128144
},
129145
{
130-
regexp: /show #([0-9]*)/gi,
146+
regexp: /show #([0-9]*)/gi,
131147
action: (tickets, message, id) => findTicket(tickets, id),
132148
reply: (message, output) => showTicket(output),
133149
},
134150
{
135-
regexp: /take #([0-9]*)/gi,
151+
regexp: /take #([0-9]*)/gi,
136152
action: (tickets, message, id) => assign(tickets, id, message.userName),
137153
reply: (message, output) => `ticket #${output.id} is assigned to ${output.assignee}`,
138154
},
139155
{
140-
regexp: /assign #([0-9]*) to (\w*)/gi,
156+
regexp: /assign #([0-9]*) to (\w*)/gi,
141157
action: (tickets, message, id, assignee) => assign(tickets, id, assignee),
142158
reply: (message, output) => `ticket #${output.id} is assigned to ${output.assignee}`,
143159
},
144160
{
145-
regexp: /debug/gi,
161+
regexp: /debug/gi,
146162
action: (tickets, message, id) => tickets,
147163
reply: stringifyThis,
148164
},
149165
{
150-
regexp: /todo/gi,
166+
regexp: /todo/gi,
151167
action: (tickets, message, id) => tickets,
152168
reply: (message, output) => showTickets(output, ['open']),
153169
},
154170
{
155-
regexp: /history/gi,
171+
regexp: /history/gi,
156172
action: (tickets, message, id) => tickets,
157173
reply: (message, output) => showTickets(output, ['open', 'closed']),
158174
},
159175
{
160-
regexp: /forget it/gi,
176+
regexp: /forget it/gi,
161177
action: (tickets, message, id) => forget(tickets),
162178
reply: (message, output) => `deleted ${output} tickets`,
163179
},
180+
{
181+
regexp: /gimme\s*a*n*\s*(\w*)/gi,
182+
action: (tickets, message, type) => gimme(type),
183+
reply: (message, output) => `${message.userName} ${output[0].toLowerCase()}${output.substr(1)}`,
184+
},
164185
]
165186

166187
function searchRegexp(action) {
@@ -186,7 +207,7 @@ function process(tickets, message) {
186207
reply = `${action.reply.bind(output)(message, output)}`
187208
// }
188209
} else {
189-
reply = `I don't understand: ${message.content}`
210+
reply = `I don't understand: ${message.content}, can you try again?`
190211
}
191212
return `#${message.prefix}: ${reply}`
192213
}

test/support_test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("support", function() {
4949

5050
it("invalid", function() {
5151
const reply = support.process(tickets, message('handsome'))
52-
assert.equal(reply, "#candra: I don't understand: handsome")
52+
assert.equal(reply, "#candra: I don't understand: handsome, can you try again?")
5353
})
5454

5555
it("help", function() {
@@ -61,4 +61,9 @@ describe("support", function() {
6161
const reply = support.process(tickets, message('forget it'))
6262
assert.equal(tickets.lastId, 0)
6363
})
64+
65+
it("gimme a compliment", function() {
66+
const reply = support.process(tickets, message('gimme a compliment'))
67+
assert.equal(reply.substr(-1, 1), '.')
68+
})
6469
})

0 commit comments

Comments
 (0)