generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
slackbot.js
99 lines (78 loc) · 3.27 KB
/
slackbot.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
module.exports = (fetch) => {
console.log('Hello from SlackBot');
const token = process.env.SLACK_TOKEN;
const headerToSend = process.env.REACT_APP_CUSTOM_REQUEST_HEADER;
let EVENTS;
const fetchEvents = async () => {
try {
// const res = await fetch("https://vrms.io/api/events");
const res = await fetch("http://localhost:4000/api/events", {
headers: {
"x-customrequired-header": headerToSend
}
});
resJson = await res.json();
const today = new Date();
const todaysEvents = resJson.filter(event => {
const eventDate = new Date(event.date);
console.log('Event date: ', eventDate);
const year = eventDate.getFullYear();
const month = eventDate.getMonth();
const date = eventDate.getDate();
const yearToday = today.getFullYear();
const monthToday = today.getMonth();
const dateToday = today.getDate();
console.log('Event: ', year, month, date);
console.log('Today: ', yearToday, monthToday, dateToday);
return (year === yearToday && month === monthToday && date === dateToday);
})
EVENTS = todaysEvents.filter(event => {
const now = new Date();
// 10 Minutes til event startTime
// If event.date -
return (event.date )
});
console.log(EVENTS);
} catch(error) {
console.log(error);
};
const BASE_URL = 'https://slack.com/api/chat.postMessage'
const channel = 'C013H2HN0VC';
if (EVENTS && EVENTS.length > 0) {
EVENTS.forEach(async event => {
const team = event.project.name;
const linkNew = 'http://localhost:3000/checkIn/newUser?eventId=' + event._id;
const linkReturning = 'http://localhost:3000/checkIn/returningUser?eventId=' + event._id;
const messageToSend = `&text=Hey ${team} team! Here are the links to check-in for your meeting tonight: \n\nNew User: ${linkNew} \nReturning User: ${linkReturning} \n\nHave fun tonight!`
const urlToSend = `${BASE_URL}?token=${token}&channel=${channel}${messageToSend}&unfurl_media=false&username=VRMS Bot`;
if (team.length > 0) {
console.log('Send slack message');
// await sendSlackMessage(urlToSend);
} else {
console.log("Didn't do anything with " + (event && event));
}
})
}
};
async function sendSlackMessage(url) {
console.log('Sending...');
await fetch(url, {
method: 'post'
})
.then(res => {
const response = res.json();
return response;
})
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
});
console.log('Done sending');
}
setTimeout(async () => {
// await fetchEvents();
}, 5000);
// return job;
}