-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
166 lines (131 loc) · 4.17 KB
/
bot.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
const { RTMClient, WebClient } = require('@slack/client');
const token = "xoxb-376760662918-375782643699-ZJ1waVnJf6nFkHjpNUQJZE1O";
const rtm = new RTMClient(token);
const web = new WebClient(token);
const botname = 'dory';
let botId = null;
let channelId = null;
const startCommand = "pr ";
var commands = [
"list",
"add",
"review"
];
var personality = [
"Bonjour, je m\’appelle Dory. J\’ai une maladie qui s\’appelle la trouble de mémoire immédiate.",
"Je les ai perdu… J’ai perdu tout le monde… Mais je ne me rappelle plus qui…",
"Et en fait je crois que j’ai jamais mangé de poisson de ma vie"
];
var store = [];
/*
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (data) => {
console.log(`Authenticated to ${data.team.name}`);
});*/
rtm.on('message', function handleRtmMessage(message) {
//console.log(message);
// console.log("botId : " + botId);
channelId = message.channel;
// exit if not a text message
if (message.type != 'message' || !message.text) {
console.log("exit if not a text message");
return;
}
// exit if not a message for the bot
let botMention = '<@'+botId+'>';
if (botId == null || message.text.indexOf(botMention) !== 0) {
console.log("exit if not a message for the bot");
return;
}
// exit if bot sent this message
const username = getUsernameFromId(message.user);
if (message.username == botname || username == botname) {
console.log("exit if bot sent this message");
return;
}
//console.log("message from : " + username);
let messageContent = message.text.replace(botMention, '').trim();
// exit if not a valid command prefix
if (messageContent.indexOf(startCommand) !== 0) {
onCommandNotFound(message);
return;
}
// exit if not a valid command
let command = messageContent.replace(startCommand, "").trim();
let arg = "";
if (command.indexOf(" ") !== -1) {
let splited = command.split(" ");
command = splited[0];
arg = splited[1];
}
if (commands.indexOf(command) == -1) {
onCommandNotFound(message);
return;
}
console.log("command received : " + command);
onCommand(command, arg, username);
});
function onCommandNotFound() {
let random = getRandomArbitrary(0, 3);
console.log("random", random);
let reply = personality[random];
postMessage(reply);
}
function onCommand(c, arg, username) {
if (c == "list") {
if (!store.length) {
console.log("store is empty");
postMessage('0 pull request en attente de review');
} else {
var result = store.join("\n");
postMessage(store.length + ' pull request en attente de review :\n' + result);
}
} else if (c == "add") {
if (!arg) {
console.log("missing argument", arg);
return;
}
if (arg.indexOf("http://") === 0 || arg.indexOf("https://") === 0) {
console.log("invalid argument", arg);
return;
}
console.log("new pr in store : " + arg);
store.push(arg);
postMessage('votre pr est ajoutée.\n' + store.length + ' pull request en attente de review.');
} else if (c == "review") {
var pr = store.pop();
postMessage(username + ' commence la review de ' + pr + '\n' + 'il reste maintenant ' + store.length + ' pull request en attente de review.');
}
}
var users = [];
function updateUsers(data) {
// console.log("updateUsers", data);
users = data.members;
}
function getUsernameFromId(id) {
const user = users.find(user => user.id === id);
return user ? user.name : 'unknown member';
}
function updateBotId() {
let bot = users.find(user => user.name === botname);
botId = bot.id;
}
// fetch users in channel
web.users.list().then((data, err) => {
// console.log('web users list', data, err);
updateUsers(data);
updateBotId();
});
async function getUserInfo(user) {
var res = await web.users.info({user: user}).then((data) => data.user.name);
console.log("getUserInfo", res);
return res;
}
function postMessage(text) {
web.chat.postMessage({token:token, channel: channelId, text: text, as_user: false, username: botname}).then((res) => console.log(res));
}
// On renvoie un nombre aléatoire entre une valeur min (incluse)
// et une valeur max (exclue)
function getRandomArbitrary(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
rtm.start();