|
1 | 1 | /* |
2 | 2 | activation_example:!converttoDecimal <binary_code> |
3 | | -regex:^!converttoDecimal |
| 3 | +regex:!bin2dec |
4 | 4 | flags:i |
5 | 5 | */ |
6 | 6 |
|
7 | | -// get the user input in format !converttoDecimal binary_code for example - !converttoDecimal 1011 |
8 | | -var input = current.text.trim(); |
9 | | - |
| 7 | +// get the user input in format !bin2dec binary_code for example - !bin2dec 00110101 11111111 |
| 8 | +var input = current.text.trim(); |
10 | 9 |
|
11 | 10 | // Extract the Binary expression from the input |
12 | | -var binaryCheckregx = /^[01]+$/; // regular expresion for valid binary code |
13 | | -var match = input.match(/!converttoDecimal (\d+)/); // check input format |
| 11 | + |
| 12 | +var binaryCheckregx = /^[01\s]+$/; // Regular expression to check if input contains only binary characters and spaces |
| 13 | +var commandRegex = /^!bin2dec ([01\s]+)$/; // Extract the binary expression, including spaces between bytes. |
| 14 | +var match = input.match(commandRegex); // Updated commandRegex to include spaces between binary code |
14 | 15 | var expression = match ? match[1] : ""; |
15 | 16 |
|
16 | 17 |
|
17 | 18 |
|
18 | 19 | function evaluateBinaryExpression(binary_code) { |
19 | | - binary_code = binary_code.replace(/\s+/g, ''); // Remove whitespace |
20 | | - // execute the api to get the equivalent decimal value of binary |
| 20 | + // Remove whitespace |
| 21 | + binary_code = binary_code.replace(/\s+/g, ''); |
| 22 | + // execute the api to get the equivalent decimal value of binary |
21 | 23 | var converter = new sn_ws.RESTMessageV2(); |
22 | 24 | var apiurl = 'https://networkcalc.com/api/binary/' + binary_code; |
23 | 25 | converter.setEndpoint(apiurl); // set endpoint url |
24 | 26 | converter.setHttpMethod('GET'); // GET method |
25 | | - var response = converter.execute(); |
26 | | - var result = JSON.parse(response.getBody()); // parse the response object |
27 | | - return result; |
| 27 | + var response = converter.execute(); |
| 28 | + var result = JSON.parse(response.getBody()); // parse the response object |
| 29 | + return result; |
28 | 30 | } |
29 | 31 |
|
30 | | -try{ |
31 | | - // check the valid binary code in if statement |
32 | | - if(binaryCheckregx.test(expression)){ |
33 | | - var result = evaluateBinaryExpression(expression); // call to evaluateBinaryExpression |
34 | | - var DecimalresultSlackMessage = "* Binary to Decimal Conversion :*\n" + "• *Binary value*: " + expression + "\n" + "• *Decimal value*: " + result.converted + "\n"; // slack markdown message format |
35 | | - new x_snc_slackerbot.Slacker().send_chat(current, DecimalresultSlackMessage , false); // display the output to user |
36 | | - } |
37 | | - else{ |
38 | | - new x_snc_slackerbot.Slacker().send_chat(current, "Oops! I couldn't understand that. Please provide a valid Binary code.", false); |
39 | | - } |
40 | | -} |
41 | | -catch(error){ |
42 | | - new x_snc_slackerbot.Slacker().send_chat(current, "Oops! Facing Issue while converting Binary to Decimal with error" + error, false); //handling exception in try block |
43 | | -} |
| 32 | +try { |
| 33 | + // check the valid binary code in if statement |
| 34 | + if (binaryCheckregx.test(expression)) { |
| 35 | + var result = evaluateBinaryExpression(expression); // call to evaluateBinaryExpression |
| 36 | + |
| 37 | + var DecimalresultSlackMessage = "Decimal Value: " + result.converted; |
| 38 | + new x_snc_slackerbot.Slacker().send_chat(current, DecimalresultSlackMessage , false); // display the output to user |
44 | 39 |
|
45 | 40 |
|
| 41 | + } else { |
| 42 | + new x_snc_slackerbot.Slacker().send_chat(current, "Oops! I couldn't understand that. Please provide a valid Binary code.", false); |
| 43 | + |
| 44 | + } |
| 45 | +} catch (error) { |
| 46 | + |
| 47 | + new x_snc_slackerbot.Slacker().send_chat(current, "Oops! Facing Issue while converting Binary to Decimal with error" + error, false); //handling exception in try block |
| 48 | +} |
| 49 | + |
| 50 | + |
46 | 51 |
|
47 | 52 |
|
0 commit comments