-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushover-freshdesk.php
138 lines (122 loc) · 4.81 KB
/
pushover-freshdesk.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/php -q
<?php
/**
* Copyright (C) 2014, Dan Vatca <[email protected]>
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
include_once(__DIR__ . '/pushover-freshdesk-config.php');
function getFreshDeskTicketsPage($url, $userPassword = '') {
$curlHandle = curl_init();
$timeout = 5;
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlHandle, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curlHandle, CURLOPT_USERPWD, $userPassword);
$rawTickets = curl_exec($curlHandle);
curl_close($curlHandle);
return json_decode($rawTickets);
}
function getAllFreshDeskOpenedTickets() {
global $FRESHDESK_URL;
global $FRESHDESK_USER;
global $FRESHDESK_PASS;
$no = 0;
$page = 0;
$tickets = [];
do {
$page++;
$currentPageOfTickets = getFreshDeskTicketsPage(
"http://$FRESHDESK_URL/helpdesk/tickets/filter/open?format=json&page=$page",
"$FRESHDESK_USER:$FRESHDESK_PASS"
);
array_walk($currentPageOfTickets, function ($ticket) use (&$tickets) { $tickets[] = $ticket; });
$numTickets = count($currentPageOfTickets);
$no += $numTickets;
} while ($numTickets > 0);
return $tickets;
}
function compare_by_updated_at($a, $b) {
return strcmp($a->updated_at, $b->updated_at);
}
function compare_by_display_id($a, $b) {
return strcmp($a->display_id, $b->display_id);
}
function sendNotification($title, $message, $message_url = '') {
global $PUSHOVER_API_KEY;
global $PUSHOVER_USER_KEY;
$url = 'https://api.pushover.net/1/messages.json';
$curlHandle = curl_init();
$timeout = 5;
$priority = 1;
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlHandle, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curlHandle, CURLOPT_POST, 1);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, http_build_query(array(
'token' => $PUSHOVER_API_KEY,
'user' => $PUSHOVER_USER_KEY,
'message' => $message,
'title' => $title,
'priority' => $priority,
'url' => $message_url
)));
$jsonResponse = curl_exec($curlHandle);
curl_close($curlHandle);
$response = json_decode($jsonResponse);
if ($response->status != 1) {
fprintf(STDERR, "ERROR: Failed to send notification. Details follow: ");
var_dump($response);
}
}
function main_loop() {
global $FRESHDESK_URL;
do {
$currentTickets = getAllFreshDeskOpenedTickets();
if (isset($currentTickets) && isset($beforeTickets)) {
$added = array_udiff($currentTickets, $beforeTickets, 'compare_by_display_id');
$removed = array_udiff($beforeTickets, $currentTickets, 'compare_by_display_id');
$modified = array_udiff(
array_uintersect($currentTickets, $beforeTickets, 'compare_by_display_id'),
array_uintersect($beforeTickets, $currentTickets, 'compare_by_display_id'),
'compare_by_updated_at');
foreach ($added as $ticket) {
sendNotification('New', $ticket->subject,
sprintf("http://%s/helpdesk/tickets/%d",$FRESHDESK_URL, $ticket->display_id)
);
printf("[%s] New: %s\n", $ticket->updated_at, $ticket->subject);
}
foreach ($removed as $ticket) {
sendNotification('Gone', $ticket->subject);
printf("[%s] Gone: %s\n", $ticket->updated_at, $ticket->subject);
}
foreach ($modified as $ticket) {
sendNotification('Updated', $ticket->subject,
sprintf("http://%s/helpdesk/tickets/%d",$FRESHDESK_URL, $ticket->display_id)
);
printf("[%s] Updated: %s\n", $ticket->updated_at, $ticket->subject);
}
}
$beforeTickets = $currentTickets;
sleep(60); // We are rate limited by Freshdesk to 1000 requests per hour. Do not go below 5 seconds.
} while (true);
}
main_loop();