-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecologi-to-slack.php
61 lines (51 loc) · 1.34 KB
/
ecologi-to-slack.php
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
<?php
/**
* Your Ecologi username
*/
$username = 'change-me';
/**
* Your Slack Webhook URL
* https://api.slack.com/messaging/webhooks
*/
$slackWebhookUrl = 'change-me';
/**
* ---- No need to edit below this point ----
*/
$data = json_decode(file_get_contents('https://public.ecologi.com/users/' . $username . '/impact'));
if (empty($data)) {
exit;
}
$curlHandle = curl_init($slackWebhookUrl);
# Setup request to send json via POST.
$payload = json_encode([
"blocks" => [
[
"type" => "section",
"text" => [
"type" => "mrkdwn",
"text" => "Our positive impact to date:",
]
],
],
"attachments" => [
[
'text' => '🌱 ' . $data->trees . ' trees planted',
'color' => 'good',
],
[
'text' => '♻️ ' . $data->carbonOffset . 'T of CO2 offset',
'color' => 'good',
],
[
'text' => 'https://ecologi.com/' . $username,
],
],
]);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $payload);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curlHandle);
curl_close($curlHandle);
if ($result === false) {
echo 'There was an error posting to Slack';
}