-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (54 loc) · 1.76 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
const express = require('express')
const app = express()
const PORT = process.env.PORT || 3000
const line = require('@line/bot-sdk')
const handleMessage = require('./src/handleMessage')
const handleTWFAMessage = require('./src/twfa')
require('dotenv').config()
const lineConfig = {
channelAccessToken: process.env.LINE_CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.LINE_CHANNEL_SECRET
}
const lineTWFAConfig = {
channelAccessToken: process.env.TWFA_LINE_CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.TWFA_LINE_CHANNEL_SECRET
}
const client = new line.Client(lineConfig)
const TWFAClient = new line.Client(lineTWFAConfig)
app.post('/callback', line.middleware(lineConfig), (req, res) => {
Promise.all(req.body.events.map(handleEvent))
.then(result => res.json(result))
.catch(err => console.log(err))
})
app.post('/twfa', line.middleware(lineTWFAConfig), (req, res) => {
Promise.all(req.body.events.map(twfaEvent))
.then(result => res.json(result))
.catch(err => console.log(err))
})
const twfaEvent = async event => {
if (event.type !== 'message') {
return Promise.resolve(null)
}
const returnMessage = await handleTWFAMessage(event)
if (returnMessage.type === 'return') {
return TWFAClient.replyMessage(
event.replyToken,
returnMessage.object
).catch(err => console.log(err))
} else return Promise.resolve(null)
}
const handleEvent = async event => {
if (event.type !== 'message') {
return Promise.resolve(null)
}
const returnMessage = await handleMessage(event)
return client
.replyMessage(event.replyToken, returnMessage)
.catch(err => console.log(JSON.stringify(err)))
}
app.get('/', async (req, res) => {
res.send('helloworld')
})
app.listen(PORT, () => {
console.log(`Port : ${PORT}`)
})