mtfosbot/libs/line-message/index.js

37 lines
1005 B
JavaScript
Raw Normal View History

2018-06-27 09:50:56 +00:00
const commands = require('./commands')
2018-07-13 15:04:54 +00:00
const DB = require('@libs/database')
2018-07-13 05:30:54 +00:00
const api = require('@libs/api-action')
2018-06-28 13:26:44 +00:00
/**
* parse text message object
* @param {object} evt line message event object
*/
const textMessage = async (evt) => {
2018-07-13 15:04:54 +00:00
let {replyToken, source, message, type} = evt
if (type === 'leave' && 'groupId' in source && 'type' in source && source.type === 'group') {
await leaveGroup(source.groupId)
return
}
2018-06-27 09:50:56 +00:00
if (!source || !('type' in source) || source.type !== 'group') return
if (!message || message.type !== 'text') return
let {text} = message
if (typeof text !== 'string') return
text = text.trim()
if (text.length === 0) return
2018-06-27 09:50:56 +00:00
let result = await commands(text, source)
if (result === null) return
if (typeof result === 'object' && 'reply' in result) {
2018-07-10 15:48:26 +00:00
console.log('reply message :::: ', result.reply)
2018-06-28 09:37:33 +00:00
await api.line.replyMessage(replyToken, result.reply)
2018-06-27 09:50:56 +00:00
}
}
2018-07-13 15:04:54 +00:00
const leaveGroup = async (group = '') => {
}
module.exports = {
2018-06-28 09:37:33 +00:00
textMessage
}