-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot-api.js
76 lines (64 loc) · 2.14 KB
/
bot-api.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
const createClient = require('./chat-client');
const md5 = require('md5');
const {v4: createId} = require('uuid');
const getAvatar = email => `http://www.gravatar.com/avatar/${md5(email)}?s=48&d=identicon&r=PG`;
const createBotUser = name => {
const id = createId();
const email = `bot_${id}@chatbot.com`;
const avatar = getAvatar(email);
return {
name,
id,
email,
avatar,
fullName: name,
familyName: name,
};
};
const createBot = name => {
const botUser = createBotUser(name);
const client = createClient('BOT/' + encodeURIComponent(JSON.stringify(botUser)));
const sendMessage = (...args) => setTimeout(() => client.sendMessage(...args), 200);
const respondWith = getResponse => (action, ...rest) => {
const text = typeof getResponse === 'function' ? getResponse(action, ...rest) : getResponse;
const {receiver, sender} = action;
sendMessage(text, receiver === botUser.id ? sender : receiver);
};
const whenOneToOne = fn => (action, ...rest) => {
if (action.receiver === botUser.id) {
fn(action, ...rest);
}
};
return {
data: botUser,
init: client.init,
respondWith,
whenOneToOne,
matches,
sendMessage,
on: client.on,
};
};
const escapeRegExp = string => string.replace(/([.*+?^${}()|\[\]\/\\])/g, '\\$1');
const textMatchesSelector = (text, selector) => {
const matches = text.startsWith(selector);
const parsedText = matches ? text.replace(new RegExp(escapeRegExp(selector) + '\s*'), '') : null;
return [matches, parsedText];
};
const matches = selectors => action => {
const handled = Object.keys(selectors).filter(selector => selector !== 'default').some(selector => {
const {text} = action.payload;
const [matches, parsedText] = textMatchesSelector(text, selector);
if (matches) {
selectors[selector](action, parsedText);
}
return matches;
});
if (!handled && 'default' in selectors) {
selectors['default'](action);
}
};
module.exports = {
createBot,
matches,
};