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 a37b2fe
Show file tree
Hide file tree
Showing 2 changed files with 16 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
15 changes: 15 additions & 0 deletions line/handler/replyCalc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const _ = require('lodash')
const msgText = require('../../msg/text')

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')
const result = new Function(`return ${text}`)() // eslint-disable-line no-new-func
await ctx.replyMessage(msgText(`${result}`))
} catch (err) {
// console.log(err)
return await next()
}
}

0 comments on commit a37b2fe

Please sign in to comment.