-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendNewVisitorSlackMessage.php
More file actions
66 lines (54 loc) · 1.64 KB
/
SendNewVisitorSlackMessage.php
File metadata and controls
66 lines (54 loc) · 1.64 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
$requestMethod = $_SERVER['REQUEST_METHOD'];
switch($requestMethod) {
case 'GET':
GetMethod();
break;
case 'POST':
PostMethod();
break;
default:
echo 'Unhandled request';
}
function GetMethod()
{
//Slack API Key
$slackAPIKey = "MySlackAPIKey";
//The ID of the channel where the message should be posted
$slackChannelID = "MySlackChannelID";
//The username of the bot which will send the message
$botUsername = "MyBotUsername";
//The message to send
$message = "You%20have%20a%20website%20visitor";
$slackSendMessageURL = "https://slack.com/api/chat.postMessage?token=slackAPIKey&channel=$slackChannelID&username=$botUsername&as_user=false&text=$message";
$response = file_get_contents($slackSendMessageURL);
}
function PostMethod()
{
//Read the IP address from the Post request
$ipAddress = $_POST["ip"];
//Get the location data from IP API
$gpsResponse = file_get_contents("http://ip-api.com/json/$ipAddress");
$gpsJSON = json_decode($gpsResponse);
//A Slack webhook to allow a bot to send messages to a channel
$slackWebhook = "MySlackWebhook";
//Build JSON for Slack request
$attachments = array([
"fallback" => "Something went wrong",
"pretext" => "New Website Visitor",
"mrkdown" => "true",
"title" => $gpsJSON->query,
"text" => "City: ".$gpsJSON->city."\nZip: ".$gpsJSON->zip." \nCountry: ".$gpsJSON->country,
]);
$data = json_encode(
array(
"attachments" => $attachments
));
//Execute Slack Webhook
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $slackWebhook);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
}
?>