Skip to content

Commit 9b05f4b

Browse files
Slackerbot ip address info (#450)
* Create fetch IP Address Information.js /* File: fetch IP Address Information.js Description: This script extracts an IP address from user input, validates the format, and retrieves geographic and network details (e.g., city, region, country, ISP) using the ipinfo.io API. The result is then sent back to the user via a chat interface. If the input is invalid, it returns an error message. Activation Example: Triggered by the command "!ipinfo" followed by an IP address. */ * Update fetch IP Address Information.js Change the formatting to slack mark down instead of sending json as output to slack
1 parent 5bbdb59 commit 9b05f4b

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)