-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new API to create gist contains line msg
- Loading branch information
1 parent
81df5ae
commit 1eae3ae
Showing
15 changed files
with
1,222 additions
and
1,087 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const _ = require('lodash') | ||
const { beautifyFlex, getenv, log } = require('../libs/helper') | ||
const { octokit } = require('../libs/octokit') | ||
const dayjs = require('dayjs') | ||
const JSON5 = require('json5') | ||
const Line = require('../libs/linebotsdk').Client | ||
|
||
const LINE_MESSAGING_TOKEN = getenv('LINE_MESSAGING_TOKEN') | ||
|
||
module.exports = async (ctx, next) => { | ||
const { req, res } = ctx | ||
if (_.isNil(LINE_MESSAGING_TOKEN) || _.isNil(octokit)) return await next() // 未設定 TOKEN | ||
if (!_.isArray(req.body) && !_.isPlainObject(req.body)) return await next() // 轉交給下一個 middleware 處理 | ||
|
||
try { | ||
ctx.line = new Line({ channelAccessToken: LINE_MESSAGING_TOKEN }) | ||
|
||
// verify message | ||
const tmp1 = _.castArray(req.body) | ||
for (let i = 0; i < tmp1.length; i++) { | ||
if (_.includes(['bubble', 'carousel'], tmp1[i]?.type)) tmp1[i] = { altText: 'altText', contents: tmp1[i], type: 'flex' } | ||
const tmp2 = tmp1[i] | ||
if (!_.isNil(tmp2?.replyToken)) throw new Error(`msg[${i}].replyToken is not allowed`) | ||
if (!_.includes(['text', 'sticker', 'image', 'video', 'audio', 'location', 'imagemap', 'template', 'flex'], tmp2?.type)) throw new Error(`msg[${i}].type = ${JSON5.stringify(tmp2?.type)} is invalid`) | ||
} | ||
await ctx.line.validateReplyMessageObjects(tmp1) // 先透過 messaging api 驗證內容 | ||
.catch(err => { throw _.merge(err, { status: err?.originalError?.response?.status ?? 500, ..._.pick(err?.originalError?.response?.data, ['message', 'details']) }) }) | ||
|
||
// 上傳到 gist | ||
const nowts = dayjs() | ||
const filename = `gcf-line-devbot-${+nowts}.json5` | ||
const gist = await octokit.request('POST /gists', { | ||
description: `Upload by line-devbot /gist/createLineMessage at ${nowts.format('YYYY-MM-DD HH:mm:ss')}`, | ||
files: { [filename]: { content: JSON5.stringify(beautifyFlex(req.body)) } }, | ||
}) | ||
res.json({ rawUrl: gist.data.files[filename].raw_url }) | ||
} catch (err) { | ||
log('ERROR', err) | ||
res.status(err.status ?? 500).json(_.pick(err, ['message', 'details'])) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const _ = require('lodash') | ||
|
||
const apis = new Map([ | ||
['GET /mp/collect', require('./mpCollect')], | ||
['POST /api/createLineMsgGist', require('./createLineMsgGist')], | ||
['POST /api/lineVerifyReplyMsg', require('./lineVerifyReplyMsg')], | ||
]) | ||
|
||
module.exports = async (ctx, next) => { | ||
const { req } = ctx | ||
const apiMethodPath = `${_.toUpper(req.method)} ${req.path}` | ||
if (!apis.has(apiMethodPath)) return await next() | ||
return await apis.get(apiMethodPath)(ctx, next) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const _ = require('lodash') | ||
const { getenv } = require('../libs/helper') | ||
const { log } = require('../libs/helper') | ||
const Line = require('../libs/linebotsdk').Client | ||
|
||
const LINE_MESSAGING_TOKEN = getenv('LINE_MESSAGING_TOKEN') | ||
|
||
module.exports = async (ctx, next) => { | ||
const { req, res } = ctx | ||
if (_.isNil(LINE_MESSAGING_TOKEN)) return await next() // 未設定 TOKEN | ||
if (!_.isArray(req.body) && !_.isPlainObject(req.body)) return await next() // 轉交給下一個 middleware 處理 | ||
|
||
try { | ||
ctx.line = new Line({ channelAccessToken: LINE_MESSAGING_TOKEN }) | ||
await ctx.line.validateReplyMessageObjects(req.body) // 先透過 messaging api 驗證內容 | ||
.catch(err => { throw _.merge(err, { status: err?.originalError?.response?.status ?? 500, ..._.pick(err?.originalError?.response?.data, ['message', 'details']) }) }) | ||
res.json({}) | ||
} catch (err) { | ||
log('ERROR', err) | ||
res.status(err.status ?? 500).json(_.pick(err, ['message', 'details'])) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
require('dotenv').config() | ||
|
||
const _ = require('lodash') | ||
const { log } = require('./libs/helper') | ||
const { middlewareCompose } = require('./libs/helper') | ||
const functions = require('@google-cloud/functions-framework') | ||
|
||
const handler = middlewareCompose([ | ||
const handlers = middlewareCompose([ | ||
require('./cors'), | ||
require('./gtag'), | ||
require('./api/index'), | ||
require('./line/handler/index'), | ||
]) | ||
|
||
functions.http('main', async (req, res) => { | ||
try { | ||
await handler({ req, res }) | ||
await handlers({ req, res }) | ||
} catch (err) { | ||
log('ERROR', err) | ||
res.status(err.status || 500).send(err.message) | ||
res.status(err.status ?? 500).json(_.pick(err, ['message'])) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
const _ = require('lodash') | ||
const { Client } = require('@line/bot-sdk') | ||
const axios = require('axios') | ||
|
||
const LINE_API_APIBASE = 'https://api.line.me' | ||
|
||
exports.validateReplyMessage = async (line, msg) => { | ||
Client.prototype.validateReplyMessageObjects = async function (msg) { | ||
try { | ||
if (!_.isArray(msg)) msg = [msg] | ||
return await axios.post(`${LINE_API_APIBASE}/v2/bot/message/validate/reply`, { messages: msg }, { | ||
headers: { | ||
Authorization: `Bearer ${line.config.channelAccessToken}`, | ||
Authorization: `Bearer ${this.config.channelAccessToken}`, | ||
}, | ||
}) | ||
} catch (err) { | ||
err.message = err?.response?.data?.message ?? err.message | ||
throw err | ||
throw _.merge(new Error('Failed to validate reply message objects'), { originalError: err }) | ||
} | ||
} | ||
|
||
exports.Client = Client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,29 +6,29 @@ | |
"author": "taichunmin <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@google-cloud/functions-framework": "^3.1.3", | ||
"@google-cloud/functions-framework": "^3.3.0", | ||
"@josephg/resolvable": "^1.0.1", | ||
"@line/bot-sdk": "^7.5.2", | ||
"axios": "^1.3.4", | ||
"axios": "^1.4.0", | ||
"crypto-js": "^4.1.1", | ||
"dayjs": "^1.11.7", | ||
"dotenv": "^16.0.3", | ||
"dayjs": "^1.11.9", | ||
"dotenv": "^16.3.1", | ||
"json5": "^2.2.3", | ||
"lodash": "^4.17.21", | ||
"octokit": "^2.0.14", | ||
"qs": "^6.11.1" | ||
"octokit": "^3.1.0", | ||
"qs": "^6.11.2" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.36.0", | ||
"eslint-config-standard": "^17.0.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-n": "^15.6.1", | ||
"eslint": "^8.47.0", | ||
"eslint-config-standard": "^17.1.0", | ||
"eslint-plugin-import": "^2.28.0", | ||
"eslint-plugin-n": "^16.0.1", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^6.1.1", | ||
"jest": "^29.5.0" | ||
"jest": "^29.6.2" | ||
}, | ||
"scripts": { | ||
"deploy": "gcloud functions deploy gcf-line-devbot --allow-unauthenticated --entry-point=main --env-vars-file=.env.yaml --gen2 --max-instances=1 --memory=128Mi --no-user-output-enabled --region=us-central1 --runtime=nodejs16 --timeout=60s --trigger-http && gcloud run services update gcf-line-devbot --region=us-central1 --cpu 1 --concurrency 80", | ||
"deploy": "gcloud functions deploy gcf-line-devbot --allow-unauthenticated --entry-point=main --env-vars-file=.env.yaml --gen2 --max-instances=1 --memory=128Mi --no-user-output-enabled --region=us-central1 --runtime=nodejs18 --timeout=60s --trigger-http && gcloud run services update gcf-line-devbot --region=us-central1 --cpu 1 --concurrency 80", | ||
"lint": "eslint --ext .js --fix .", | ||
"localhost-run": "autossh -M 0 -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o StrictHostKeyChecking=no -R 80:localhost:3000 [email protected]", | ||
"repl": "node --experimental-repl-await repl.js", | ||
|
Oops, something went wrong.