From ad45231d8d59063eef320f692c9ccb32200b56a0 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 28 Aug 2018 23:34:08 +0800 Subject: [PATCH] add save line message to log --- bin/dbVersion.json | 3 ++- libs/api-action/line.js | 32 ++++++++++++++++++++++++++++++- libs/line-message/index.js | 39 ++++++++++++++++++++++++++++++++++++++ schema/20180828-1.sql | 21 ++++++++++++++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 schema/20180828-1.sql diff --git a/bin/dbVersion.json b/bin/dbVersion.json index c580866..f483ccf 100644 --- a/bin/dbVersion.json +++ b/bin/dbVersion.json @@ -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": [] } \ No newline at end of file diff --git a/libs/api-action/line.js b/libs/api-action/line.js index 16f4883..4f36a71 100644 --- a/libs/api-action/line.js +++ b/libs/api-action/line.js @@ -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 } diff --git a/libs/line-message/index.js b/libs/line-message/index.js index 22254b9..4757c81 100644 --- a/libs/line-message/index.js +++ b/libs/line-message/index.js @@ -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() diff --git a/schema/20180828-1.sql b/schema/20180828-1.sql new file mode 100644 index 0000000..d26f0cf --- /dev/null +++ b/schema/20180828-1.sql @@ -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); \ No newline at end of file