modify database schema
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* add group to database
|
||||
* @param {string} txt command body format => groupName notifyEnable(0,1)
|
||||
* @param {object} source
|
||||
* @param {object} db
|
||||
*/
|
||||
const addGroup = async (txt, source = {}, db) => {
|
||||
if (!db) return null
|
||||
if (!('type' in source) || !('groupId' in source) || !('userId' in source)) return null
|
||||
let {groupId, userId} = source
|
||||
let arr = txt.split(' ')
|
||||
if (arr.length < 2 || !isFinite(arr[1])) return null
|
||||
let name = arr[0]
|
||||
let notify = arr[1]
|
||||
if (typeof notify === 'string') notify = parseInt(notify)
|
||||
if (notify !== 0 && notify !== 1) return null
|
||||
let text = `select "id" from "public"."line_group" where "id" = $1`
|
||||
let values = [groupId]
|
||||
let result = await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
if (result.rowCount > 0) {
|
||||
let reply = 'group exists'
|
||||
return { reply }
|
||||
}
|
||||
text = `insert into "public"."line_group" ("id", "name", "notify", "owner", "ctime", "mtime") values ($1, $2, $3, $4, now(), now())`
|
||||
values = [groupId, name, (notify === 1), userId]
|
||||
await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
let reply = 'add success'
|
||||
return { reply }
|
||||
}
|
||||
|
||||
/**
|
||||
* add facebook page notify to group
|
||||
* @param {string} txt command body format => pageid name tmpl
|
||||
* @param {object} source
|
||||
* @param {object} db
|
||||
*/
|
||||
const addPage = async (txt, source = {}, db) => {
|
||||
if (!db) return null
|
||||
if (!('type' in source) || !('groupId' in source) || !('userId' in source)) return null
|
||||
let {groupId, userId} = source
|
||||
let arr = txt.split(' ')
|
||||
if (arr.length < 3) return null
|
||||
let page = arr[0]
|
||||
let name = arr[1]
|
||||
let tmpl = arr.slice(2).join(' ')
|
||||
let text = `select "id","owner" from "public"."line_group" where "id" = $1`
|
||||
let values = [groupId]
|
||||
let result = await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
if (result.rowCount === 0) {
|
||||
let reply = 'group not register'
|
||||
return { reply }
|
||||
}
|
||||
if (result.rows[0].owner !== userId) {
|
||||
let reply = 'not owner'
|
||||
return { reply }
|
||||
}
|
||||
// check pageid in group
|
||||
text = `select fb."id" from "public"."facebook_page" fb
|
||||
left join "public"."line_group" line
|
||||
on fb."groupid" = line."id"
|
||||
where
|
||||
fb."pageid" = $1`
|
||||
values = [page]
|
||||
result = await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
if (result.rowCount > 0) {
|
||||
let reply = 'page exists'
|
||||
return { reply }
|
||||
}
|
||||
// check page name in group
|
||||
text = `select fb."id" from "public"."facebook_page" fb
|
||||
left join "public"."line_group" line
|
||||
on fb."groupid" = line."id"
|
||||
where
|
||||
fb."name" = $1`
|
||||
values = [name]
|
||||
result = await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
if (result.rowCount > 0) {
|
||||
let reply = 'page name exists'
|
||||
return { reply }
|
||||
}
|
||||
text = `insert into "public"."facebook_page" ("groupid", "pageid", "name", "tmpl", "ctime", "mtime") values
|
||||
($1, $2, $3, $4, now(), now())`
|
||||
values = [groupId, page, name, tmpl]
|
||||
await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
let reply = 'add page success'
|
||||
return { reply }
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
addgroup: addGroup,
|
||||
addpage: addPage
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
const DB = require('../../database')
|
||||
const groupCMD = require('./group')
|
||||
|
||||
const cmds = {}
|
||||
|
||||
for (let i in groupCMD) {
|
||||
cmds[i] = groupCMD[i]
|
||||
}
|
||||
|
||||
const parseCMD = async (text = '', source = {}) => {
|
||||
if (typeof text !== 'string' || text.trim().length === 0) return null
|
||||
if (!source || typeof source !== 'object' || !('type' in source)) return null
|
||||
let txt = text.trim()
|
||||
let arr = txt.split(' ').map(t => t.trim())
|
||||
if (arr.length === 0) return null
|
||||
if (arr[0][0] !== '!') return null
|
||||
let cmd = arr[0].replace(/^!/, '')
|
||||
if (!(cmd in cmds)) return null
|
||||
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
|
||||
}
|
||||
|
||||
module.exports = parseCMD
|
||||
+21
-18
@@ -1,6 +1,6 @@
|
||||
const axios = require('axios')
|
||||
const config = require('../../config')
|
||||
const DB = require('../database')
|
||||
const commands = require('./commands')
|
||||
|
||||
const client = axios.create({
|
||||
baseURL: 'https://api.line.me/v2/bot',
|
||||
@@ -33,31 +33,34 @@ const pushMessage = async (target, message = '') => {
|
||||
}
|
||||
|
||||
const textMessage = async (evt) => {
|
||||
let replyURL = '/message/reply'
|
||||
let url = '/message/reply'
|
||||
let {replyToken, source, message} = evt
|
||||
if (!source || !('type' in source) || source.type !== 'group') return
|
||||
if (!message || message.type !== 'text') return
|
||||
let {text} = message
|
||||
if (typeof text !== 'string') return
|
||||
text = text.trim()
|
||||
if (text.length === 0) return
|
||||
let db = await DB.connect()
|
||||
|
||||
// let opts = {
|
||||
// method: 'post',
|
||||
// url: replyURL,
|
||||
// data: {
|
||||
// replyToken,
|
||||
// messages: [
|
||||
// {
|
||||
// type: 'text',
|
||||
// text: 'test message'
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// }
|
||||
let result = await commands(text, source)
|
||||
if (result === null) return
|
||||
if (typeof result === 'object' && 'reply' in result) {
|
||||
let opts = {
|
||||
method: 'post',
|
||||
url,
|
||||
data: {
|
||||
replyToken,
|
||||
messages: [
|
||||
{
|
||||
type: 'text',
|
||||
text: result.reply
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
// await client(opts)
|
||||
db.release()
|
||||
await client(opts)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user