-
Notifications
You must be signed in to change notification settings - Fork 0
/
pub.js
91 lines (77 loc) · 2.68 KB
/
pub.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
const dotenv = require('dotenv');
const fetch = require('node-fetch');
const addPubOrder = require('./datastore/firebase/rest').addPubOrder;
const deletePubOrder = require('./datastore/firebase/rest').deletePubOrder;
const getUserPubOrders = require('./datastore/firebase/rest').getUserPubOrders;
dotenv.config();
const checkOrders = async () => {
if (!isPubOpen()) {
console.log(`Pub is not open now. Not checking orders.`);
return;
}
console.log(`Checking users' pub orders...`);
const preparedOrders = new Set(await getPreparedOrders());
const userPubOrders = await getUserPubOrders();
console.log('preparedOrders: ', preparedOrders);
console.log('userPubOrders: ', JSON.stringify(userPubOrders));
for (let userId in userPubOrders) {
let userOrder = parseInt(userPubOrders[userId], 10);
if (!preparedOrders.has(userOrder)) {
continue;
}
console.log(`Found order #${userOrder} for ${userId}`);
try {
sendOrderReadyMessage(userId, userOrder);
deletePubOrder(userId, userOrder);
} catch (err) {
console.error(err);
}
}
};
function getPreparedOrders() {
const baseUrl = process.env.prod === 'true' ? `https://vde-bot.herokuapp.com/orders`
: `https://vde-bot.herokuapp.com/fakeorders`;
const options = {
headers: { 'Content-Type': 'application/json' },
method: 'GET'
};
return fetch(baseUrl, options)
.then(res => res.text())
.then(JSON.parse)
.catch(console.error);
}
/**
* Pub is open is 11:00am CT at the earliest, 9:00pm CT at the latest
* Will ping from 11:00am - 10:00pm CT (in case orders unfilled at 9pm)
* = 11:00 - 22:00 CT
* = 17:00 - 04:00 UTC (UTC is 6 hours ahead of CT)
*/
function isPubOpen() {
let date = new Date();
let utcHour = date.getUTCHours();
return utcHour >= 17 || utcHour <= 4;
}
function sendOrderReadyMessage(userId, userOrder) {
const url = `https://graph.facebook.com/v2.6/me/messages?access_token=${process.env.fb_access_token}`;
const body = {
'message': {
'text': `Pub order #${userOrder} is ready!`
},
'messaging_type': 'MESSAGE_TAG',
'recipient': {
'id': userId
},
'tag': 'PAIRING_UPDATE'
};
const options = {
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
method: 'POST'
};
console.log(options);
fetch(url, options)
.then(res => res.text())
.then(console.log)
.catch(console.error);
}
module.exports = checkOrders;