-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
61 lines (46 loc) · 1.5 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
const fs = require('fs')
const paths = fs.readdirSync(__dirname + '/src/websites')
const replay = require('./src/replay')
const slack = require('./src/slackBlock')
const createSection = website => {
const content = website.createMessageBlock() || []
const emoji = website.emoji || ':food:'
content.unshift(slack.section(`*${emoji} ${website.title}*`))
content.push(slack.context(website.context))
content.push(slack.divider())
return content
}
const blank = ({title}) => ([
slack.section(`Brak menu dla ${title} :disappointed:`),
slack.divider()
])
exports.main = async (params) => {
const sendMessage = replay(params.response_url)
const websitesPromises = paths
.map(path => require('./src/websites/' + path))
.filter(website => website.title.toLowerCase().match(params.text.toLowerCase()))
.map(website => {
console.log('SCRAPE ' + website.title)
return website
.scrape()
.then(() => {
console.log('CREATE_MESSAGE ' + website.title)
return website.text ? createSection(website) : blank(website)
})
})
sendMessage([slack.section(`@${params.user_name} co chcesz zjeść?`)])
await Promise
.all(websitesPromises)
.then(messages => [].concat(...messages).filter(Boolean))
.then(message => JSON.stringify(message))
.then(blocks => {
if (!params.response_url) {
console.log(blocks)
}
return sendMessage(blocks)
})
return {
statusCode: 200,
body: 'Dzięki filip!'
}
}