diff --git a/libs/api-action/line.js b/libs/api-action/line.js index 47718b5..26001f8 100644 --- a/libs/api-action/line.js +++ b/libs/api-action/line.js @@ -36,6 +36,14 @@ const pushMessage = async (target, message = '') => { await client(opts) } +const textObject = (txt = '') => { + if (typeof txt !== 'string' || txt.trim().length === 0) return null + return { + type: 'text', + text: txt + } +} + /** * send reply message * @param {string} replyToken line message reply token @@ -43,21 +51,35 @@ const pushMessage = async (target, message = '') => { */ const replyMessage = async (replyToken, message) => { let url = '/message/reply' - let opts = { - method: 'post', - url, - data: { - replyToken, - messages: [ - { - type: 'text', - text: message - } - ] + + let regex = /^\$(.+?)\$/ + let m = message.match(regex) + let obj = null + message = message.replace(/^\$.+?\$/, '') + if (m !== null && m.length > 1) { + switch (m[1]) { + case 'image': + break + case 'text': + obj = textObject(message) + break + default: + obj = textObject(message) } } - await client(opts) + if (obj !== null) { + let opts = { + method: 'post', + url, + data: { + replyToken, + messages: [obj] + } + } + + await client(opts) + } } module.exports = { diff --git a/libs/line-message/commands/actions/group.js b/libs/line-message/commands/actions/group.js index f59fe2e..f7c4806 100644 --- a/libs/line-message/commands/actions/group.js +++ b/libs/line-message/commands/actions/group.js @@ -1,5 +1,6 @@ const api = require('../../../api-action') const DB = require('../../../database') +const axios = require('axios') async function run (fn = null, txt, source) { if (!fn || typeof fn !== 'function') return null @@ -447,6 +448,29 @@ const addYoutube = async (txt = '', source = {}, db) => { } } +const image = async (txt, source, db) => { + if (typeof txt !== 'string' || txt.trim().length === 0) return null + let imgs = txt.split(';') + if (imgs.length !== 2) return null + + try { + await axios({ + url: imgs[0], + method: 'head' + }) + await axios({ + url: imgs[1], + method: 'head' + }) + } catch (err) { + console.log(err) + return null + } + return { + reply: `$image$${txt}` + } +} + const hello = async () => { return {reply: 'Hello World'} } @@ -458,5 +482,6 @@ module.exports = { addtwitch: run.bind(null, addTwitch), deltwitch: run.bind(null, delTwitch), addyoutube: run.bind(null, addYoutube), - hello: run.bind(null, hello) + hello: run.bind(null, hello), + image: run.bind(null, image) } diff --git a/libs/line-message/commands/actions/index.js b/libs/line-message/commands/actions/index.js index d3595c3..0e5bf9a 100644 --- a/libs/line-message/commands/actions/index.js +++ b/libs/line-message/commands/actions/index.js @@ -24,9 +24,10 @@ for (let i of actGrpFile) { } const runAct = async (cmd, txt = '', source = {}) => { - if (!cmd || typeof cmd !== 'string' || cmd.length === 0) return null - if (!(cmd in cmds)) return null - let result = await cmds[cmd](txt, source) + if (!cmd || (typeof cmd !== 'string' && !Array.isArray(cmd)) || cmd.length === 0) return null + let c = typeof cmd === 'string' ? cmd : cmd[0] + if (!(c in cmds)) return null + let result = await cmds[c](typeof cmd === 'string' ? txt : cmd[1] || '', source) return result.reply } diff --git a/libs/line-message/commands/index.js b/libs/line-message/commands/index.js index 217b2f6..0cbb219 100644 --- a/libs/line-message/commands/index.js +++ b/libs/line-message/commands/index.js @@ -15,6 +15,7 @@ const parseCMD = async (text = '', source = {}) => { let reply = null try { + // query normal command let result = await db.query({ text: `select "message", "group" from "public"."commands" where "cmd" = $1 and ("group" = '' or "group" = $2)`, values: [cmd, source.groupId] @@ -28,6 +29,8 @@ const parseCMD = async (text = '', source = {}) => { if (m !== null && m.length > 0) { for (let i = 0; i < m.length; i++) { let c = m[i].replace(/^{{/, '').replace(/}}$/, '') + let carr = c.split('=') + if (carr.length > 1) c = carr let res = await actions(c, arr.slice(1).join(' '), source) content = content.replace(m[i], res || '') } @@ -38,6 +41,52 @@ const parseCMD = async (text = '', source = {}) => { } } } + + if (reply === null) { + // query keyword commands + let keyCMD = await db.query({ + text: `select "message", "group", "cmd" from "public"."key_commands" where ("group" = '' or "group" = $1)`, + values: [source.groupId] + }) + if (keyCMD.rowCount > 0) { + let obj = keyCMD.rows.filter(t => t.group === '') + let obj2 = keyCMD.rows.filter(t => t.group === source.groupId) + obj = obj.map(t => { + for (let i of obj2) { + if (i.cmd === t.cmd && i.group !== '') return i + } + return t + }) + let regex = null + let txt = '' + for (let i of obj) { + txt += (txt.length > 0 ? '|' : '') + i.cmd + } + regex = new RegExp(`(${txt})`) + let m = arr.slice(1).join(' ').match(regex) + if (m !== null && m.length > 0) { + let key = obj.filter(t => t.cmd === m[0]) + if (key.length > 0) { + let content = key[0].message + let m = content.match(/{{(.+?)}}/g) + if (m !== null && m.length > 0) { + for (let i = 0; i < m.length; i++) { + let c = m[i].replace(/^{{/, '').replace(/}}$/, '') + let carr = c.split('=') + if (carr.length > 1) c = carr + let res = await actions(c, arr.slice(1).join(' '), source) + content = content.replace(m[i], res || '') + } + } + if (content.trim().length > 0) { + reply = { + reply: content + } + } + } + } + } + } } catch (err) { console.log(err) } diff --git a/schema/20180712-1.sql b/schema/20180712-1.sql new file mode 100644 index 0000000..8afa970 --- /dev/null +++ b/schema/20180712-1.sql @@ -0,0 +1,11 @@ +-- auto-generated definition +create table key_commands +( + key varchar(100) not null, + "group" varchar(150) default '' :: character varying not null, + message varchar(300) default '' :: character varying not null, + ctime timestamp with time zone default CURRENT_TIMESTAMP not null, + mtime timestamp with time zone default CURRENT_TIMESTAMP not null, + constraint key_commands_key_group_pk + primary key (key, "group") +); \ No newline at end of file