Skip to content

Commit

Permalink
Added reply/update posts
Browse files Browse the repository at this point in the history
Removed some error_log calls.. moved things around again.
  • Loading branch information
clonemeagain committed Oct 14, 2017
1 parent 0974af8 commit 61fc453
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
![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.
78 changes: 68 additions & 10 deletions slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 = ['<' => '&lt;', '>' => '&gt;', '&' => '&amp;'];
$msg = str_replace(array_keys($formatter), array_values($formatter), $msg);
Expand All @@ -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'][] = [
Expand All @@ -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");
Expand All @@ -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;
}

}

0 comments on commit 61fc453

Please sign in to comment.