-
Notifications
You must be signed in to change notification settings - Fork 2
/
send_lucky_number.js
executable file
·65 lines (59 loc) · 1.67 KB
/
send_lucky_number.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
#!/usr/bin/node
const fetch = require("node-fetch");
const Messer = require("messer/src/messer");
const config = require("/root/.messer/tmp/config.json");
const url = "https://get-lucky.netlify.com/.netlify/functions/get";
const messer = new Messer();
messer.messen
.login()
.then(() => fetch(url))
.then(r => r.json())
.then(apiResponse => {
let lucky = apiResponse.data;
console.log(`Lucky string: ${lucky}`);
lucky = parseInt(lucky);
if (lucky === NaN) process.exit(1);
console.log(`Lucky number: ${lucky}`);
luckyOne = config[lucky];
if (luckyOne === undefined) return lucky;
console.log(`Lucky person: ${luckyOne}`);
return luckyOne;
})
.then(luckyOne => {
console.log("Sending message...");
if (Number.isInteger(luckyOne)) {
let message = {
body: `Dzisiaj szczęśliwy numerek to ${luckyOne}`
};
messer.messen.api.sendMessage(message, config.group);
return console.log("Message sent");
}
messer.messen.api.getUserID(luckyOne, (err, data) => {
let message = {
body: `Dzisiaj szczęśliwy numerek ma @${luckyOne}`
};
if (err) {
console.log(`Could not get id of ${luckyOne}`);
messer.messen.api.sendMessage(message, config.group);
} else {
const id = data[0].userID;
messer.messen.api.sendMessage(
{
...message,
mentions: [
{
tag: `@${luckyOne}`,
id: id
}
]
},
config.group
);
}
return console.log("Message sent");
});
})
.catch(error => {
console.log(error);
process.exit(1);
});