-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.js
60 lines (54 loc) · 1.6 KB
/
message.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
const exec = require('child_process').exec;
const WebSocket = require('ws');
const fetch = require('node-fetch');
const wsUri = 'ws://localhost:8280/';
const websocket = new WebSocket(wsUri);
websocket.onopen = () => console.log('OPEN');
websocket.onclose = () => console.log('CLOSED');
websocket.onerror = (e) => console.log('ERROR', e);
websocket.onmessage = (m) => onMessage(m);
const config = process.argv.slice(2);
console.log('Bot is set for user:', config[0]);
/**
*
* @returns {Promise<string>}
*/
async function getJoke() {
try {
const resp = await fetch('https://geek-jokes.sameerkumar.website/api');
return await resp.json();
} catch (e) {
console.log(e);
}
}
/**
*
* @param sender String
* @param message String
* @return void
*/
function sendMessage(sender, message) {
message = message.replace(/'/g, '').replace(/Chuck Norris/g, sender.replace(/@.*$/, '').toUpperCase());
const command = `curl -X POST -d '{"global_id":"` + sender + `","data":{"type":"text", "message":` + message + `}}' localhost:8180/message/send/v1`;
exec(command, function (error) {
if (error !== null) {
console.log('exec error: ' + error);
}
});
}
/**
*
* @param m
* @returns {Promise<void>}
*/
async function onMessage(m) {
const data = JSON.parse(m.data);
if (data.type === 'private_message') {
const sender = data.payload.sender.glob_id.replace('master$', '');
const authorIsMe = config[0] === sender;
if (!authorIsMe) {
const joke = await getJoke();
sendMessage(sender, joke);
}
}
}