-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·88 lines (76 loc) · 2.16 KB
/
index.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
#!/usr/bin/env node
const client = require('request-promise-native')
// const { scheduleJob } = require('node-schedule') // Maybe overkill? Just use real cron?
const { IncomingWebhook } = require('@slack/client')
const webhook = new IncomingWebhook(process.env.SLACK_WEBHOOK_URL)
const restId = process.env.RESTAURANT_ID
async function fetchMenu() {
const [todayMenu, nextMenu] = await client({
json: true,
uri: `https://timechef.elior.com/api/restaurant/${restId}/menus/2`,
})
// TODO: give nextMenu if called after a certain time
return todayMenu
}
let lastMenu = {}
async function getMenu(always = false) {
const today = new Date()
today.setUTCHours(0, 0, 0, 0)
const isCurrent = today <= lastMenu.date
// don't fetch again when we already have a current (or future) menu
if (isCurrent) return lastMenu
let menu = await fetchMenu()
let date, items
if (!menu) {
console.warn(`Menu is ${menu}! Using last menu instead`)
menu = lastMenu
[date, items] = [menu.date, menu.items]
} else {
date = new Date(menu.date)
items = menu.famillePlats[0].plats.map(_ => _.libelle)
}
// don't send an old menu, unless forced
if (!always && date < today) return
return { date, items }
}
function isValidMenu(menu) {
if (
menu === null
||
!Array.isArray(menu.items)
||
!(menu.date instanceof Date)
)
return false
return true
}
function formatMenu(menu) {
console.log(menu) //DEBUG
// TODO: make it pretty
const message = menu.items.join('\n')
const messageObj = {
text: message,
mrkdwn: true,
}
return messageObj
}
async function sendMessage(message) {
try {
const res = await webhook.send(message)
console.log('Message sent:', res)
} catch (err) {
console.error('Error:', err)
}
}
async function sendMenu(always = false) {
const menu = await getMenu(always)
// don't send an invalid menu
if (!isValidMenu(menu)) return
const message = formatMenu(menu)
await sendMessage(message)
lastMenu = menu
}
// Script to run every weekday at 11h11s11 that will send out the day's menu
// scheduleJob('11 11 11 * * MON-FRI', sendMenu)
// TODO: start some server
sendMenu() // DEBUG