add key word command not fin
This commit is contained in:
parent
ceb522b21f
commit
4c56ff4300
@ -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,22 +51,36 @@ const pushMessage = async (target, message = '') => {
|
||||
*/
|
||||
const replyMessage = async (replyToken, message) => {
|
||||
let url = '/message/reply'
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
if (obj !== null) {
|
||||
let opts = {
|
||||
method: 'post',
|
||||
url,
|
||||
data: {
|
||||
replyToken,
|
||||
messages: [
|
||||
{
|
||||
type: 'text',
|
||||
text: message
|
||||
}
|
||||
]
|
||||
messages: [obj]
|
||||
}
|
||||
}
|
||||
|
||||
await client(opts)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
pushMessage,
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
11
schema/20180712-1.sql
Normal file
11
schema/20180712-1.sql
Normal file
@ -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")
|
||||
);
|
Loading…
Reference in New Issue
Block a user