add save line message to log

This commit is contained in:
Jay 2018-08-28 23:34:08 +08:00
parent ac446758b6
commit ad45231d8d
4 changed files with 93 additions and 2 deletions

View File

@ -16,7 +16,8 @@
{"file": "20180811-1.sql", "version": 14},
{"file": "20180815-1.sql", "version": 15},
{"file": "20180816-1.sql", "version": 16},
{"file": "20180826-1.sql", "version": 17}
{"file": "20180826-1.sql", "version": 17},
{"file": "20180828-1.sql", "version": 18}
],
"test": []
}

View File

@ -95,7 +95,37 @@ const replyMessage = async (replyToken, message) => {
}
}
/**
* query user info
* @param {string} group group id
* @param {string} id user id
*/
const getUserInfo = async (group = '', id = '') => {
if (typeof group !== 'string' || group.trim().length === 0) return null
if (typeof id !== 'string' || id.trim().length === 0) return null
let url = `/group/${group.trim()}/member/${id.trim()}`
try {
let result = await client({
url,
method: 'get'
})
if ('data' in result && typeof result.data === 'object') {
let {displayName, userId} = result.data
if (typeof displayName !== 'string' || displayName.length === 0) return null
if (typeof userId !== 'string' || userId.length === 0) return null
return {
displayName,
userId
}
}
} catch (err) {
console.log('get user info error :::: ', err)
}
return null
}
module.exports = {
pushMessage,
replyMessage
replyMessage,
getUserInfo
}

View File

@ -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()

21
schema/20180828-1.sql Normal file
View File

@ -0,0 +1,21 @@
CREATE TABLE public.line_user
(
id varchar(256) PRIMARY KEY NOT NULL,
name varchar(512) NOT NULL,
ctime timestamp with time zone DEFAULT current_timestamp NOT NULL,
mtime timestamp with time zone DEFAULT current_timestamp NOT NULL
);
CREATE TABLE public.line_message_log
(
"group" varchar(256) NOT NULL,
"user" varchar(256) NOT NULL,
message varchar(2048) DEFAULT '' NOT NULL,
ctime timestamp with time zone DEFAULT current_timestamp NOT NULL,
mtime timestamp with time zone DEFAULT current_timestamp NOT NULL,
CONSTRAINT line_message_log_line_group_id_fk FOREIGN KEY ("group") REFERENCES public.line_group (id) ON DELETE CASCADE,
CONSTRAINT line_message_log_line_user_id_fk FOREIGN KEY ("user") REFERENCES public.line_user (id) ON DELETE CASCADE
);
ALTER TABLE public.line_message_log ADD id uuid DEFAULT public.uuid_generate_v4() NOT NULL;
ALTER TABLE public.line_message_log ADD CONSTRAINT line_message_log_pk PRIMARY KEY (id);