Skip to content

Commit

Permalink
replyCalc
Browse files Browse the repository at this point in the history
  • Loading branch information
taichunmin committed Jul 17, 2024
1 parent d88ec39 commit 7e7511f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions line/handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const lineEventHander = middlewareCompose([
require('./cmd'), // 處理指令
require('./replyFlexFromText'), // 嘗試回傳 flex
require('./noReplyUrl'), // 如果是一個合法的 URL 就不作回應
require('./replyCalc'), // 計算數學式
require('./replyEventJson'), // 把事件用 json 回傳
])

Expand Down
13 changes: 13 additions & 0 deletions line/handler/replyCalc.js
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 7e7511f

Please sign in to comment.