add save line message to log
This commit is contained in:
@@ -23,6 +23,10 @@ const textMessage = async (evt) => {
|
||||
text = text.trim()
|
||||
if (text.length === 0) return
|
||||
|
||||
saveToLog(source, text).then(() => {}).catch(err => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
let result = await commands(text, source)
|
||||
if (result === null) return
|
||||
if (typeof result === 'object' && 'reply' in result) {
|
||||
@@ -31,6 +35,41 @@ const textMessage = async (evt) => {
|
||||
}
|
||||
}
|
||||
|
||||
const saveToLog = async (source, text = '') => {
|
||||
if (!source || typeof source !== 'object') return null
|
||||
if (typeof text !== 'string' || text.length === 0) return null
|
||||
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) {
|
||||
data = api.line.getUserInfo(groupId, userId)
|
||||
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)`
|
||||
values = [groupId, userId, text]
|
||||
await db.query({text, values})
|
||||
} catch (err) {
|
||||
console.log('save log error :::: ', err)
|
||||
}
|
||||
|
||||
db.release()
|
||||
return null
|
||||
}
|
||||
|
||||
const leaveGroup = async (group = '') => {
|
||||
if (typeof group !== 'string' || group.trim().length === 0) return
|
||||
let db = await DB.connect()
|
||||
|
||||
Reference in New Issue
Block a user