From e1ad83566bbf3f3935f34d16f99dbc7ff5087d73 Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 13 Jul 2018 13:30:54 +0800 Subject: [PATCH] add module-alias , add image message --- bin/dbVersion.json | 3 ++- bin/dbtool.js | 3 ++- index.js | 1 + jsconfig.json | 11 +++++++++++ libs/api-action/google.js | 2 +- libs/api-action/line.js | 13 ++++++++++++- libs/api-action/twitch.js | 2 +- libs/database.js | 4 ++-- libs/line-message/commands/actions/group.js | 4 ++-- libs/line-message/commands/index.js | 2 +- libs/line-message/index.js | 2 +- libs/middleware/index.js | 2 +- package.json | 6 ++++++ route/google/index.js | 6 +++--- route/line/index.js | 4 ++-- route/twitch/index.js | 6 +++--- 16 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 jsconfig.json diff --git a/bin/dbVersion.json b/bin/dbVersion.json index 28bacc1..627f9be 100644 --- a/bin/dbVersion.json +++ b/bin/dbVersion.json @@ -6,7 +6,8 @@ {"file": "20180702-1.sql", "version": 4}, {"file": "20180706-1.sql", "version": 5}, {"file": "20180710-1.sql", "version": 6}, - {"file": "20180711-1.sql", "version": 7} + {"file": "20180711-1.sql", "version": 7}, + {"file": "20180712-1.sql", "version": 8} ], "test": [] } \ No newline at end of file diff --git a/bin/dbtool.js b/bin/dbtool.js index 8c4f388..3bddfa1 100644 --- a/bin/dbtool.js +++ b/bin/dbtool.js @@ -1,6 +1,7 @@ /* eslint-disable no-unused-expressions */ +require('module-alias/register') const pg = require('pg') -const config = require('../config') +const config = require('@config/index') const path = require('path') const fs = require('fs') diff --git a/index.js b/index.js index 4781824..7210190 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +require('module-alias/register') const fs = require('fs') const path = require('path') try { diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..c2ae2a2 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "moduleResolution": "node", + "baseUrl": ".", + "paths": { + "@libs/*": ["libs/*"], + "@config/*": ["config/*"], + "@route/*": ["route/*"] + } + } +} \ No newline at end of file diff --git a/libs/api-action/google.js b/libs/api-action/google.js index 22ae02a..ad76d19 100644 --- a/libs/api-action/google.js +++ b/libs/api-action/google.js @@ -1,5 +1,5 @@ const axios = require('axios') -const config = require('../../config') +const config = require('@config/index') const qs = require('querystring') const queryYoutubeName = async (id = '') => { diff --git a/libs/api-action/line.js b/libs/api-action/line.js index 26001f8..a6f31a6 100644 --- a/libs/api-action/line.js +++ b/libs/api-action/line.js @@ -1,5 +1,5 @@ const axios = require('axios') -const config = require('../../config') +const config = require('@config/index') const client = axios.create({ baseURL: 'https://api.line.me/v2/bot', @@ -44,6 +44,16 @@ const textObject = (txt = '') => { } } +const imageObject = (txt = '') => { + if (typeof txt !== 'string' || txt.trim().length === 0) return null + txt = txt.split(';') + return { + type: 'image', + originalContentUrl: txt[0], + previewImageUrl: txt[1] + } +} + /** * send reply message * @param {string} replyToken line message reply token @@ -59,6 +69,7 @@ const replyMessage = async (replyToken, message) => { if (m !== null && m.length > 1) { switch (m[1]) { case 'image': + obj = imageObject(message) break case 'text': obj = textObject(message) diff --git a/libs/api-action/twitch.js b/libs/api-action/twitch.js index 08140cd..7deff8b 100644 --- a/libs/api-action/twitch.js +++ b/libs/api-action/twitch.js @@ -1,5 +1,5 @@ const axios = require('axios') -const config = require('../../config') +const config = require('@config/index') const client = axios.create({ baseURL: 'https://api.twitch.tv/helix', headers: { diff --git a/libs/database.js b/libs/database.js index 9fba19e..c9fb2c3 100644 --- a/libs/database.js +++ b/libs/database.js @@ -1,5 +1,5 @@ const pg = require('pg') -const config = require('../config') +const config = require('@config/index') const pool = new pg.Pool({ user: config.database.user, @@ -10,4 +10,4 @@ const pool = new pg.Pool({ database: config.database.dbname }) -module.exports = pool \ No newline at end of file +module.exports = pool diff --git a/libs/line-message/commands/actions/group.js b/libs/line-message/commands/actions/group.js index f7c4806..b5d1024 100644 --- a/libs/line-message/commands/actions/group.js +++ b/libs/line-message/commands/actions/group.js @@ -1,5 +1,5 @@ -const api = require('../../../api-action') -const DB = require('../../../database') +const api = require('@libs/api-action') +const DB = require('@libs/database') const axios = require('axios') async function run (fn = null, txt, source) { diff --git a/libs/line-message/commands/index.js b/libs/line-message/commands/index.js index 0cbb219..f5e1fe3 100644 --- a/libs/line-message/commands/index.js +++ b/libs/line-message/commands/index.js @@ -1,4 +1,4 @@ -const DB = require('../../database') +const DB = require('@libs/database') const actions = require('./actions') const parseCMD = async (text = '', source = {}) => { diff --git a/libs/line-message/index.js b/libs/line-message/index.js index d911280..7e709ff 100644 --- a/libs/line-message/index.js +++ b/libs/line-message/index.js @@ -1,5 +1,5 @@ const commands = require('./commands') -const api = require('../api-action') +const api = require('@libs/api-action') /** * parse text message object diff --git a/libs/middleware/index.js b/libs/middleware/index.js index bb6d776..7a9a80f 100644 --- a/libs/middleware/index.js +++ b/libs/middleware/index.js @@ -1,4 +1,4 @@ -const config = require('../../config') +const config = require('@config/index') const rawBody = require('raw-body') const crypto = require('crypto') diff --git a/package.json b/package.json index 1c55d5f..6ddabaa 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "koa-body": "^4.0.3", "koa-logger": "^3.2.0", "koa-router": "^7.4.0", + "module-alias": "^2.1.0", "pg": "^7.4.3", "raw-body": "^2.3.3", "request": "^2.87.0", @@ -28,5 +29,10 @@ }, "devDependencies": { "standard": "^11.0.1" + }, + "_moduleAliases": { + "@libs": "libs", + "@config": "config", + "@route": "route" } } diff --git a/route/google/index.js b/route/google/index.js index b58f12b..7fd4488 100644 --- a/route/google/index.js +++ b/route/google/index.js @@ -4,9 +4,9 @@ const r = new Router() // const koaBody = require('koa-body') const { getRaw -} = require('../../libs/middleware') -const api = require('../../libs/api-action') -const DB = require('../../libs/database') +} = require('@libs/middleware') +const api = require('@libs/api-action') +const DB = require('@libs/database') r.get('/youtube/webhook', async (c, n) => { let db = await DB.connect() diff --git a/route/line/index.js b/route/line/index.js index 4fa23e2..1f14fdd 100644 --- a/route/line/index.js +++ b/route/line/index.js @@ -4,10 +4,10 @@ const r = new Router() const { verifyLine, getRaw -} = require('../../libs/middleware') +} = require('@libs/middleware') const { textMessage -} = require('../../libs/line-message') +} = require('@libs/line-message') r.post('/', getRaw, verifyLine, async (c, n) => { console.log(JSON.stringify(c.request.body, null, 2)) diff --git a/route/twitch/index.js b/route/twitch/index.js index 6b7b2d0..516fb54 100644 --- a/route/twitch/index.js +++ b/route/twitch/index.js @@ -2,9 +2,9 @@ const Router = require('koa-router') const r = new Router() const { getRaw -} = require('../../libs/middleware') -const DB = require('../../libs/database') -const api = require('../../libs/api-action') +} = require('@libs/middleware') +const DB = require('@libs/database') +const api = require('@libs/api-action') // const config = require('../../config') r.get('/webhook', async (c, n) => {