diff --git a/line/handler/index.js b/line/handler/index.js index 817f2d4..6759870 100644 --- a/line/handler/index.js +++ b/line/handler/index.js @@ -8,6 +8,7 @@ const lineEventHander = middlewareCompose([ require('./cmd'), // 處理指令 require('./replyFlexFromText'), // 嘗試回傳 flex require('./noReplyUrl'), // 如果是一個合法的 URL 就不作回應 + require('./replyCalc'), // 計算數學式 require('./replyEventJson'), // 把事件用 json 回傳 ]) diff --git a/line/handler/replyCalc.js b/line/handler/replyCalc.js new file mode 100644 index 0000000..5d9e2e4 --- /dev/null +++ b/line/handler/replyCalc.js @@ -0,0 +1,13 @@ +const _ = require('lodash') + +module.exports = async (ctx, next) => { + const text = ctx?.event?.message?.text + try { + if (!_.isString(text)) throw new Error('Not a string') + if (/[^0-9!.()*/&%^+<>|~eE -]/.test(text)) throw new Error('Not a arithmetic string') + return new Function(`return ${text}`)().toString() // eslint-disable-line no-new-func + } catch (err) { + // console.log(err) + return await next() + } +}