-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (127 loc) · 4.42 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const debug = require('debug')('hookhub:hooks:google-forms-slack')
debug('Loading vhs-hookhub-google-forms-slack')
debug(__dirname)
const express = require('express')
const router = express.Router()
const config = require('./config.json')
const smb = require('slack-message-builder')
const { createHmac } = require('node:crypto')
const isValid = (val) => val != null && typeof val === 'string' && val !== ''
// Perform sanity check
router.use((req, res, next) => {
const formId = req.header('X-Hookhub-Google-Form-Id')
const formHash = req.header('X-Hookhub-Google-Form-Hash')
const formTS = req.header('X-Hookhub-Google-Form-TS')
if (
!isValid(formId) ||
!isValid(formTS) ||
Number.isNaN(encodeURI(formTS)) ||
!isValid(formHash) ||
!isValid(req.rawBody)
) {
res.status(412).send({
result: 'ERROR',
message: 'Missing or invalid request arguments'
})
} else {
res.locals.formId = encodeURI(formId)
res.locals.formHash = encodeURI(formHash)
res.locals.formTS = Number(encodeURI(formTS))
next()
}
})
router.use((req, res, next) => {
if (config.forms[res.locals.formId] == null) {
res.status(401).send({
result: 'ERROR',
message: 'Invalid form'
})
} else {
res.locals.formConfig = { id: res.locals.formId, ...config.forms[res.locals.formId] }
next()
}
})
router.use((req, res, next) => {
const verifyKey = `${res.locals.formId}.${res.locals.formTS}.${res.locals.formConfig.secret}`
const verifyHash = createHmac('sha256', verifyKey).update(req.rawBody).digest('hex')
if (verifyHash !== res.locals.formHash) {
res.status(403).send({
result: 'ERROR',
message: 'Invalid hash'
})
} else if (res.locals.formTS < Date.now() - 1000) {
res.status(400).send({
result: 'ERROR',
message: 'Invalid ts'
})
} else {
next()
}
})
/* Default handler. */
router.use('/', async (req, res, next) => {
debug('Handling default request')
let post_body = generateMessage(res.locals.formConfig, req.body)
debug('post_body:', post_body)
const post_options = {
method: 'POST',
body: JSON.stringify(post_body)
}
try {
await fetch(config.slack.url, post_options)
res.send({
result: 'OK',
message: 'Message posted!'
})
} catch (err) {
res.status(500).send({
result: 'ERROR',
message: err
})
}
})
module.exports = router
const generateMessage = function (formConfig, payload) {
const slackOptions = { ...config.slack.options, ...formConfig.slack.options }
const filters = formConfig.slack.filter ?? []
const withAnswers = formConfig.slack.answers ?? true
const withFilters = filters.length
let slack_message = smb()
.username(slackOptions.username)
.iconEmoji(slackOptions.icon_emoji)
.channel(slackOptions.channel)
slack_message = slack_message.text(
`There is a new entry for the ${formConfig.slack.title} form!${(withAnswers || withFilters) && '\r\r'}`
)
if (withAnswers || withFilters) {
slack_message = slack_message
.attachment()
.fallback(
Object.entries(getFilteredAnswers(payload, filters))
.map((e) => `- ${e.join(': ')}`)
.join('\n')
)
.color('#0000cc')
.authorName(formConfig.slack.title)
.authorLink(`https://docs.google.com/forms/d/${formConfig.id}/viewform`)
.title(formConfig.slack.title)
.titleLink(`https://docs.google.com/forms/d/${formConfig.id}/viewform`)
.text('Form Results:')
Object.entries(getFilteredAnswers(payload, filters)).forEach(([k, v]) => {
slack_message = slack_message.field().title(k).value(v).short(false).end()
})
slack_message = slack_message
.footer('Via: vhs/hookhub-hook-google-forms-slack')
.ts(Date.now() / 1000)
.end()
}
return slack_message.json()
}
const getFilteredAnswers = (answers, filter) => {
if (filter.length === 0) return answers
return Object.entries(answers)
.filter(([k, v]) => filter.includes(k))
.reduce((c, [k, v]) => {
return { ...c, [k]: v }
}, {})
}