mtfosbot/route/line/index.js

31 lines
689 B
JavaScript
Raw Permalink Normal View History

2018-06-25 10:07:10 +00:00
const Router = require('koa-router')
const r = new Router()
// const koaBody = require('koa-body')
const {
verifyLine,
getRaw
2018-07-13 05:30:54 +00:00
} = require('@libs/middleware')
const {
textMessage
2018-07-13 05:30:54 +00:00
} = require('@libs/line-message')
2018-06-25 10:07:10 +00:00
r.post('/', getRaw, verifyLine, async (c, n) => {
console.log(JSON.stringify(c.request.body, null, 2))
if (!('events' in c.request.body)) return c.throw(400, 'data struct error')
let evt = c.request.body.events || []
evt.forEach(t => {
let type = t.type
switch (type) {
case 'message':
textMessage(t).catch(err => {
console.log(err)
})
break
}
})
2018-06-25 10:07:10 +00:00
c.body = 'success'
c.status = 200
})
module.exports = r