-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
122 lines (98 loc) · 3.17 KB
/
app.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const config = require('./config')
const router = require('koa-router')()
const koaBody = require('koa-body')
const thenRequest = require('then-request')
const Slack = require('slack-node')
const Koa = require('koa')
// app
const app = module.exports = new Koa()
// routes
router.get('/', infoResponse)
router.post(config.URL_LISTENING_SEND, processSending)
app.use(koaBody())
app.use(router.routes())
// responsible methods
async function infoResponse(ctx) {
ctx.body = config.HOME_MESSAGE
}
async function processSending(ctx) {
const body = ctx.request.body
const restaurants = body.restaurants
const slackToken = body.slack_token
// bot customization
const slackChannel = body.slack_channel || '#mittag_essen' // haha ;)
const slackUsername = body.slack_username || 'Eating great again?' // seriously.
const slackEmoji = body.slack_emoji || ':donald:' // i mean. nobody else.
slack = new Slack()
slack.setWebhook(`https://hooks.slack.com/services/${slackToken}`)
for (let i = 0; i < restaurants.length; i++) {
const restaurant = restaurants[i]
const zomatoId = restaurant.zomato_id
const slackIcon = restaurant.slack_icon
const restaurantData = await getRestaurantData(zomatoId)
const name = restaurantData.name
const menus = restaurantData.menus
// the message to be sent
let slackMessage = ''
if (menus.length == 0) {
slackMessage = `\n ${slackIcon} *${name}* _Sorry, no daily menu today._\n${restaurantData.url}\n`
} else {
slackMessage = `\n ${slackIcon} *${name}*\n\n`
for (let j = 0; j < menus.length; j++) {
const dish = menus[j]
let priceString = '\n'
if (dish.dish.price) {
priceString = ` : ${dish.dish.price}\n`
}
slackMessage = `${slackMessage}${dish.dish.name}${priceString}`
}
}
console.info(slackChannel)
console.info(slackUsername)
console.info(slackEmoji)
slack.webhook({
channel: slackChannel,
username: slackUsername,
icon_emoji: slackEmoji,
text: slackMessage,
}, function(err, response) {
console.log(`#slack: ${slackMessage}`)
})
}
ctx.body = {
result: "ok"
}
}
async function getRestaurantData(zomatoId) {
const restaurantResponse = await thenRequest('GET',
`https://developers.zomato.com/api/v2.1/restaurant?res_id=${zomatoId}`,
{
headers: {
user_key: config.ZOMATO_TOKEN,
Accept: 'application/json'
}
}
)
const restaurant = JSON.parse(restaurantResponse.getBody('UTF-8'))
const dailyMenuResponse = await thenRequest('GET',
`https://developers.zomato.com/api/v2.1/dailymenu?res_id=${zomatoId}`,
{
headers: {
user_key: config.ZOMATO_TOKEN,
Accept: 'application/json'
}
}
)
const dailyMenu = JSON.parse(dailyMenuResponse.getBody('UTF-8'))
let menus = [] // empty menus in case there is nothing provided for today
if (dailyMenu.daily_menus[0]) {
menus = dailyMenu.daily_menus[0].daily_menu.dishes
}
return {
name: restaurant.name,
url: restaurant.url,
menus
}
}
app.listen(config.HTTP_PORT)
console.info(`Listening on port ${config.HTTP_PORT}. Call me 📞!`)