-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (57 loc) · 1.99 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
var memory = { users: {}, coldown: {}, richmenus: {} }
const line = require('@line/bot-sdk')
const express = require('express')
require('dotenv').config()
const admin = require('firebase-admin')
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FIREBASE_PROJECT_ID,
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n')
}),
databaseURL: process.env.FIREBASE_DATABASE_URL
})
const config = {
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.CHANNEL_SECRET,
}
async function main() {
const messages = {}
for (const iterator of require('./data/message/index.json')) {
messages[iterator] = require(`./data/message/${iterator}.json`)
}
const db = admin.database()
const ref = db.ref('/')
const client = new line.Client(config)
exports.client = client
exports.ref = ref
exports.memory = memory
const data = { messages: messages, altText: require('./data/message/alttext.json'), richmenus: (await ref.child('richmenus').get()).val(), tags: (await ref.child('tags').get()).val() }
exports.data = data
const { handleEvent } = require("./handleEvent.js")
setInterval(() => {
ref.child('users').update(memory.users)
}, 30000)
const app = express()
app.post('/linewebhook', line.middleware(config), (req, res) => {
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result))
.catch((err) => {
console.error(err)
res.status(500).end()
})
})
app.get(/^\/file\/(.*?)(?:\/)?$/, (req, res) => {
try {
res.sendFile(__dirname + '/data/file/' + req.params[0])
} catch (err) {
console.error(err)
res.status(500).end()
}
})
app.listen(process.env.PORT || 3000, () => {
console.log('on')
})
}
main().then()