From 61fc4535a8b9f2a7cbcc04fa4db0bbec6a57d269 Mon Sep 17 00:00:00 2001 From: Aaron Date: Sat, 14 Oct 2017 12:05:07 +1100 Subject: [PATCH] Added reply/update posts Removed some error_log calls.. moved things around again. --- README.md | 10 +++++-- slack.php | 78 ++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 76 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 568a0b0..42c02c5 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,12 @@ You should also receive an email from Slack telling you about the new Integratio ## Test! Create a ticket! -You should see something like: +You should see something like the following appear in your Slack channel: -![slack](https://user-images.githubusercontent.com/5077391/31570760-2a5bec20-b0d3-11e7-9429-c25e8cdcf328.png) \ No newline at end of file +![slack](https://user-images.githubusercontent.com/5077391/31570760-2a5bec20-b0d3-11e7-9429-c25e8cdcf328.png) + +When a user replies, you'll get something like: + +![update-slack](https://user-images.githubusercontent.com/5077391/31571023-47477b16-b0d7-11e7-8f2f-334680904260.png) + +Notes, Replies from Agents and System messages shouldn't appear. \ No newline at end of file diff --git a/slack.php b/slack.php index 5a58187..5fa114a 100644 --- a/slack.php +++ b/slack.php @@ -12,16 +12,13 @@ class SlackPlugin extends Plugin { var $config_class = "SlackPluginConfig"; function bootstrap() { - Signal::connect('ticket.created', function(Ticket $ticket) { - $this->onTicketCreated($ticket); - }); + Signal::connect('ticket.created', array($this, 'onTicketCreated')); + Signal::connect('threadentry.created', array($this, 'onTicketUpdated')); } function onTicketCreated(Ticket $ticket) { - $c = $this->getConfig(); - - global $ost, $cfg; - if (!$ost instanceof osTicket || !$cfg instanceof OsticketConfig) { + global $cfg; + if (!$cfg instanceof OsticketConfig) { error_log("Slack plugin called too early."); return; } @@ -43,7 +40,48 @@ function onTicketCreated(Ticket $ticket) { , __('on') , strtotime($ticket->getCreateDate()) , $ticket->getCreateDate()); + $this->sendToSlack($ticket, $msg, $body); + } + + function onTicketUpdated(ThreadEntry $entry) { + global $cfg; + if (!$cfg instanceof OsticketConfig) { + error_log("Slack plugin called too early."); + return; + } + if (!$entry instanceof MessageThreadEntry) { + // this was a reply or a system entry.. not a message from a user + return; + } + $ticket = $this->getTicket($entry); + $msg = sprintf('%s CONTROLSTART%sscp/tickets.php?id=%d|#%sCONTROLEND %s' + , __("Ticket") + , $cfg->getBaseUrl() + , $ticket->getId() + , $ticket->getNumber() + , __("updated")); + $body = sprintf('%s %s (%s) %s %s (%s) %s %s %s CONTROLSTART!date^%d^{date} {time}|%sCONTROLEND %s' + , __("by") + , $entry->getPoster() + , $ticket->getEmail() + , __('in') + , $ticket->getDeptName() + , __('Department') + , __('via') + , $ticket->getSource() + , __('on') + , strtotime($entry->getUpdateDate()) + , $entry->getUpdateDate() + , "\n\n" . $entry->getBody()->getClean()); + $this->sendToSlack($ticket, $msg, $body); + } + function sendToSlack(Ticket $ticket, $msg, $body) { + global $ost, $cfg; + if (!$ost instanceof osTicket || !$cfg instanceof OsticketConfig) { + error_log("Slack plugin called too early."); + return; + } // Obey message formatting rules:https://api.slack.com/docs/message-formatting $formatter = ['<' => '<', '>' => '>', '&' => '&']; $msg = str_replace(array_keys($formatter), array_values($formatter), $msg); @@ -52,8 +90,6 @@ function onTicketCreated(Ticket $ticket) { $moreformatter = ['CONTROLSTART' => '<', 'CONTROLEND' => '>']; $msg = str_replace(array_keys($moreformatter), array_values($moreformatter), $msg); $body = str_replace(array_keys($moreformatter), array_values($moreformatter), $body); - error_log("Message: $msg"); - error_log("BODY: $body"); try { $payload['attachments'][] = [ @@ -72,7 +108,7 @@ function onTicketCreated(Ticket $ticket) { ]; $data_string = utf8_encode(json_encode($payload)); - $url = $c->get('slack-webhook-url'); + $url = $this->getConfig()->get('slack-webhook-url'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); @@ -98,4 +134,26 @@ function onTicketCreated(Ticket $ticket) { } } + /** + * Fetches a ticket from a ThreadEntry + * + * @param ThreadEntry $entry + * @return Ticket + */ + private static function getTicket(ThreadEntry $entry) { + static $ticket; + if (!$ticket) { + // aquire ticket from $entry.. I suspect there is a more efficient way. + $ticket_id = Thread::objects()->filter([ + 'id' => $entry->getThreadId() + ])->values_flat('object_id')->first() [0]; + + // Force lookup rather than use cached data.. + $ticket = Ticket::lookup(array( + 'ticket_id' => $ticket_id + )); + } + return $ticket; + } + }