Repository navigation
Expand file tree
/
Copy pathwebhook.php
More file actions
142 lines (128 loc) · 4.54 KB
/
Copy pathwebhook.php
File metadata and controls
142 lines (128 loc) · 4.54 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
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
139
140
141
142
<?php
require_once(INCLUDE_DIR . 'class.signal.php');
require_once(INCLUDE_DIR . 'class.plugin.php');
require_once(INCLUDE_DIR . 'class.ticket.php');
require_once(INCLUDE_DIR . 'class.osticket.php');
require_once(INCLUDE_DIR . 'class.config.php');
require_once(INCLUDE_DIR . 'class.format.php');
require_once('config.php');
class WebhookPlugin extends Plugin {
var $config_class = "WebhookPluginConfig";
/**
* The entrypoint of the plugin, keep short, always runs.
*/
function bootstrap() {
Signal::connect('ticket.created', array($this, 'onTicketCreated'));
Signal::connect('threadentry.created', array($this, 'onTicketUpdated'));
}
/**
* What to do with a new Ticket?
*
* @global OsticketConfig $cfg
* @param Ticket $ticket
* @return type
*/
function onTicketCreated(Ticket $ticket) {
global $cfg;
if (!$cfg instanceof OsticketConfig) {
error_log("Webhook plugin called too early.");
return;
}
$status = "created";
$this->sendToWebhook($ticket, $status);
}
/**
* What to do with an Updated Ticket?
*
* @global OsticketConfig $cfg
* @param ThreadEntry $entry
* @return type
*/
function onTicketUpdated(ThreadEntry $entry) {
global $cfg;
if (!$cfg instanceof OsticketConfig) {
error_log("Webhook plugin called too early.");
return;
}
// Need to fetch the ticket from the ThreadEntry
$ticket = $this->getTicket($entry);
$status = "updated";
$this->sendToWebhook($ticket, $status);
}
/**
* A helper function that sends messages to webhook endpoints.
*
* @global osTicket $ost
* @global OsticketConfig $cfg
* @param Ticket $ticket
* @param Status $status
* @throws \Exception
*/
function sendToWebhook(Ticket $ticket, $status) {
global $ost, $cfg;
if (!$ost instanceof osTicket || !$cfg instanceof OsticketConfig) {
error_log("Webhook plugin called too early.");
return;
}
$url = $this->getConfig()->get('webhook-webhook-url');
if (!$url) {
$ost->logError('Webhook Plugin not configured', 'You need to read the Readme and configure a webhook URL before using this.');
}
// Build the payload with the formatted data:
$staff = $ticket->getStaff();
$payload['body'] = [
'staff' => $staff ? $staff->getUsername() : "",
'title' => $ticket->getSubject(),
'number' => $ticket->getNumber(),
'status' => $status,
'url' => $cfg->getUrl()
];
// Format the payload:
$data_string = utf8_encode(json_encode($payload));
try {
// Setup curl
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
// Actually send the payload to webhook:
if (curl_exec($ch) === false) {
throw new \Exception($url . ' - ' . curl_error($ch));
} else {
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($statusCode != '200') {
throw new \Exception(
'Error sending to: ' . $url
. ' Http code: ' . $statusCode
. ' curl-error: ' . curl_errno($ch));
}
}
} catch (\Exception $e) {
$ost->logError('Webhook posting issue!', $e->getMessage(), true);
error_log('Error posting to Webhook. ' . $e->getMessage());
} finally {
curl_close($ch);
}
}
/**
* Fetches a ticket from a ThreadEntry
*
* @param ThreadEntry $entry
* @return Ticket
*/
function getTicket(ThreadEntry $entry) {
$ticket_id = Thread::objects()->filter([
'id' => $entry->getThreadId()
])->values_flat('object_id')->first()[0];
// Force lookup rather than use cached data..
// This ensures we get the full ticket, with all
// thread entries etc..
return Ticket::lookup(array(
'ticket_id' => $ticket_id
));
}
}