From 1fdd26e27c0479c330a42f6532762ca7e30dbd1f Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 28 Jun 2018 21:26:44 +0800 Subject: [PATCH] update --- background.js | 2 -- libs/api-action/line.js | 10 +++++++++ libs/line-message/commands/index.js | 32 ++++++++++++++++++++++++----- libs/line-message/index.js | 4 ++++ 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/background.js b/background.js index 8306d22..a569d20 100644 --- a/background.js +++ b/background.js @@ -103,7 +103,6 @@ new cron.CronJob({ //eslint-disable-line let twch = await db.query({ text }) - console.log('check twitch channel number :::: ', twch.rowCount) let ids = twch.rows.map(t => t.id) let streams = await api.twitch.getUserStream(ids) if (streams !== null && Array.isArray(streams)) { @@ -124,7 +123,6 @@ new cron.CronJob({ //eslint-disable-line }) const sendStreamNotify = async (streamer = null) => { - console.log(streamer) if (streamer === null || typeof streamer !== 'object' || !('user_id' in streamer) || !('id' in streamer)) return null let db = await DB.connect() diff --git a/libs/api-action/line.js b/libs/api-action/line.js index 3670e7d..47718b5 100644 --- a/libs/api-action/line.js +++ b/libs/api-action/line.js @@ -8,6 +8,11 @@ const client = axios.create({ } }) +/** + * push message to group or room or user + * @param {string} target target id (groupid, userid, roomid) + * @param {string} message push message + */ const pushMessage = async (target, message = '') => { if (typeof target !== 'string' || target.trim().length === 0) return if (typeof message !== 'string' || message.trim().length === 0) return @@ -31,6 +36,11 @@ const pushMessage = async (target, message = '') => { await client(opts) } +/** + * send reply message + * @param {string} replyToken line message reply token + * @param {string} message reply message + */ const replyMessage = async (replyToken, message) => { let url = '/message/reply' let opts = { diff --git a/libs/line-message/commands/index.js b/libs/line-message/commands/index.js index 8be22c0..0837fc4 100644 --- a/libs/line-message/commands/index.js +++ b/libs/line-message/commands/index.js @@ -15,17 +15,39 @@ const parseCMD = async (text = '', source = {}) => { if (arr.length === 0) return null if (arr[0][0] !== '!') return null let cmd = arr[0].replace(/^!/, '') - if (!(cmd in cmds)) return null + + // find default command + if (cmd in cmds) { + let db = await DB.connect() + let result = null + try { + result = await cmds[cmd](arr.slice(1).join(' '), source, db) + } catch (err) { + console.log(err) + } + // if (result === null) return null + db.release() + return result + } + let db = await DB.connect() - let result = null try { - result = await cmds[cmd](arr.slice(1).join(' '), source, db) + let text = `select "mrssage" from "public"."commands" where cmd = $1` + let values = [cmd] + let result = await db.query({ + text, + values + }) + if (result.rowCount === 0) { + db.release() + return null + } } catch (err) { console.log(err) + db.release() + return null } - // if (result === null) return null db.release() - return result } module.exports = parseCMD diff --git a/libs/line-message/index.js b/libs/line-message/index.js index 0ce06fd..1fdff7f 100644 --- a/libs/line-message/index.js +++ b/libs/line-message/index.js @@ -1,6 +1,10 @@ const commands = require('./commands') const api = require('../api-action') +/** + * parse text message object + * @param {object} evt line message event object + */ const textMessage = async (evt) => { let {replyToken, source, message} = evt if (!source || !('type' in source) || source.type !== 'group') return