mtfosbot/libs/line-message/index.js

143 lines
4.1 KiB
JavaScript
Raw Permalink 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') {
2018-07-13 16:19:33 +00:00
try {
await leaveGroup(source.groupId)
} catch (err) {
console.log(err)
}
2018-07-13 15:04:54 +00:00
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-08-28 15:34:08 +00:00
saveToLog(source, text).then(() => {}).catch(err => {
console.log(err)
})
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-08-28 15:45:31 +00:00
const saveToLog = async (source, msg = '') => {
2018-08-28 15:34:08 +00:00
if (!source || typeof source !== 'object') return null
2018-08-28 15:45:31 +00:00
if (typeof msg !== 'string' || msg.length === 0) return null
2018-08-28 15:34:08 +00:00
let {groupId, userId} = source
if (typeof groupId !== 'string' || groupId.length === 0) return null
if (typeof userId !== 'string' || userId.length === 0) return null
// connect database
let db = await DB.connect()
try {
let text = `select * from "public"."line_user" where "id" = $1`
let values = [userId]
let user = await db.query({text, values})
let data = null
if (user.rowCount === 0) {
2018-08-28 15:42:13 +00:00
data = await api.line.getUserInfo(groupId, userId)
2018-08-28 15:39:21 +00:00
console.log('user data ::::: ', data)
2018-08-28 15:34:08 +00:00
if (data === null) {
db.release()
return null
}
let text = `insert into "public"."line_user" ("id", "name") values ($1, $2)`
let values = [userId, data.displayName]
await db.query({text, values})
}
text = `insert into "public"."line_message_log" ("group", "user", "message") values ($1, $2, $3)`
2018-08-28 15:45:31 +00:00
values = [groupId, userId, msg]
2018-08-28 15:34:08 +00:00
await db.query({text, values})
} catch (err) {
console.log('save log error :::: ', err)
}
db.release()
return null
}
2018-07-13 15:04:54 +00:00
const leaveGroup = async (group = '') => {
2018-07-13 16:19:33 +00:00
if (typeof group !== 'string' || group.trim().length === 0) return
let db = await DB.connect()
let gData = await db.query({
text: `select * from "public"."line_group" where "id" = $1`,
values: [group]
})
if (gData.rowCount === 0) {
db.release()
return
}
try {
await db.query({
text: `delete from "public"."commands" where "group" = $1`,
values: [group]
})
await db.query({
text: `delete from "public"."key_commands" where "group" = $1`,
values: [group]
})
await db.query({
text: `delete from "public"."line_fb_rt" where "line" = $1`,
values: [group]
})
await db.query({
text: `delete from "public"."line_twitch_rt" where "line" = $1`,
values: [group]
})
await db.query({
text: `delete from "public"."line_youtube_rt" where "line" = $1`,
values: [group]
})
await db.query({
text: `delete from "public"."twitch_channel" where "id" in (
select ch."id" from "public"."twitch_channel" ch
left join "line_twitch_rt" rt
on rt."twitch" = ch."id"
where rt."twitch" is null
)`
})
await db.query({
text: `delete from "public"."facebook_page" where "id" in (
select ch."id" from "public"."facebook_page" ch
left join "line_fb_rt" rt
on rt."twitch" = ch."id"
where rt."twitch" is null
)`
})
await db.query({
text: `delete from "public"."youtube_channel" where "id" in (
select ch."id" from "public"."youtube_channel" ch
left join "line_youtube_rt" rt
on rt."twitch" = ch."id"
where rt."twitch" is null
)`
})
await db.query({
text: `delete from "public"."line_group" where "id" = $1`,
values: [group]
})
} catch (err) {
console.log(err)
}
db.release()
2018-07-13 15:04:54 +00:00
}
module.exports = {
2018-06-28 09:37:33 +00:00
textMessage
}