-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsendReminderMsg.js
More file actions
43 lines (40 loc) · 1.75 KB
/
sendReminderMsg.js
File metadata and controls
43 lines (40 loc) · 1.75 KB
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
if (process.env.NODE_ENV !== "production") require("dotenv").config();
const fetch = require("node-fetch");
const getConfigData = require("./util/getConfigData");
const { parseGroupmeError } = require("./util/parseGroupmeResponse");
// const setConfigTo = require("./util/setConfigTo");
async function sendMsgToGroup(pgClient) {
return new Promise((res, rej) => {
if (!process.env.BOT_ID) {
return rej("No bot ID in environment variables!"); // return rej(), don't throw new Error()
}
let message;
getConfigData(pgClient).then(json => {
if (!json.message || json.message.trim() === "") {
return rej("No message to send.");
}
message = json.message;
fetch("https://api.groupme.com/v3/bots/post", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
bot_id: process.env.BOT_ID,
text: message,
}),
})
.then(msg => msg.text())
.then(text => {
if (text !== "") {
return rej(parseGroupmeError(text));
}
console.log("Message sent.", process.uptime());
//setConfigTo(pgClient, { lastSent: Date.now() }).then(() => res(), () => rej("Failed to update config.")); // no longer recording lastSent (circular dependency: sendReminderMsg -> setConfigTo -> setCronAlarm)
}, error => {
rej("Failed to fetch.");
});
}, err => rej(err)); // propogate!
});
}
module.exports = sendMsgToGroup;