-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathCalculator.js
49 lines (37 loc) · 1.38 KB
/
Calculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
activation_example:!calculate <expression>
regex:!calculate (.+)
flags:gmi
*/
var input = current.text.trim();
// Extract the mathematical expression from the input
var match = input.match(/!calculate (.+)/);
var expression = match ? match[1] : "";
function evaluateExpression(expr) {
// Remove whitespace
expr = expr.replace(/\s+/g, '');
// Basic validation to allow only numbers and operators
if (!/^[\d+\-*/().]+$/.test(expr)) {
throw new Error("Invalid expression");
}
// Use Free API to calculate
var calc = new sn_ws.RESTMessageV2();
var url = 'http://api.mathjs.org/v4/?expr=' + encodeURIComponent(expr);
calc.setEndpoint(url);
calc.setHttpMethod("GET");
var chatResponse = calc.execute();
var result = JSON.parse(chatResponse.getBody());
result = parseFloat(result);
// Return the evaluated result
return result;
}
try {
var result = evaluateExpression(expression);
// Round the result to 2 decimal places
var roundedResult = result.toFixed(2);
// Send the calculated result back to the user
new x_snc_slackerbot.Slacker().send_chat(current, "The result is: " + roundedResult, false);
} catch (error) {
// Handle errors if the expression is invalid
new x_snc_slackerbot.Slacker().send_chat(current, "Oops! I couldn't understand that. Please provide a valid mathematical expression.", false);
}