|
| 1 | +/* |
| 2 | +activation_example:!ipinfo |
| 3 | +regex:^!ipinfo |
| 4 | +flags:i |
| 5 | +*/ |
| 6 | + |
| 7 | +var input = current.text.trim(); // getting the user input |
| 8 | + |
| 9 | +// regular expression to check the correct IP Address |
| 10 | +var regexp = /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$/; |
| 11 | + |
| 12 | +// regular expression to fetch the IP Address from information added by user |
| 13 | +var extractedip = input.match(/\b(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\b/g); |
| 14 | + |
| 15 | + |
| 16 | +var ipaddress = extractedip ? extractedip : " "; // set the value of ipaddress with the ipaddress given by user or remain empty |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +// fetch the IP Address Information like(city,region,country,loc,org,postal,timezone) from API and return result |
| 21 | +function fetchinfo(ipaddress) { |
| 22 | + var url = "https://ipinfo.io/" + ipaddress + "/json"; |
| 23 | + var r = new sn_ws.RESTMessageV2(); |
| 24 | + r.setEndpoint(url); |
| 25 | + r.setHttpMethod('GET'); |
| 26 | + var ipinfo = r.execute(); |
| 27 | + var result = JSON.parse(ipinfo.getBody()); |
| 28 | + return result; |
| 29 | +} |
| 30 | + |
| 31 | +try { |
| 32 | + if (regexp.test(ipaddress)) { |
| 33 | + var ipdata = fetchinfo(ipaddress); // calling the fetchinfo function |
| 34 | + var ipInformationSlackMessage = "*IP Information:*\n" + |
| 35 | + "• *IP*: " + ipdata.ip + "\n" + |
| 36 | + "• *City*: " + ipdata.city + "\n" + |
| 37 | + "• *Region*: " + ipdata.region + "\n" + |
| 38 | + "• *Country*: " + ipdata.country + "\n" + |
| 39 | + "• *Location*: " + ipdata.loc + "\n" + |
| 40 | + "• *Organization*: " + ipdata.org + "\n" + |
| 41 | + "• *Postal Code*: " + ipdata.postal + "\n" + |
| 42 | + "• *Timezone*: " + ipdata.timezone; //formatting for slack mark down |
| 43 | + new x_snc_slackerbot.Slacker().send_chat(current, ipInformationSlackMessage , false); // display the output to user |
| 44 | + |
| 45 | + } else { |
| 46 | + new x_snc_slackerbot.Slacker().send_chat(current, "Oops! I couldn't understand that. Please provide a valid IP Address.", false); // Message to show when IP is invalid |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +} catch (error) { |
| 51 | + new x_snc_slackerbot.Slacker().send_chat(current, "Oops! Facing Issue while fetching information about IP Address.", false); //handling exception in try block |
| 52 | +} |
0 commit comments