Skip to content

Commit

Permalink
Merge pull request clonemeagain#36 from JensEB/patch-1
Browse files Browse the repository at this point in the history
Add support for instances to work with osTicket v1.17

With thanks!
  • Loading branch information
clonemeagain authored Oct 28, 2022
2 parents 1365d59 + c409db2 commit 15c0cad
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@ class SlackPlugin extends Plugin {

var $config_class = "SlackPluginConfig";

static $pluginInstance = null;

private function getPluginInstance(?int $id) {
if($id && ($i = $this->getInstance($id)))
return $i;

return $this->getInstances()->first();
}

/**
* The entrypoint of the plugin, keep short, always runs.
*/
function bootstrap() {
$updateTypes = $this->getConfig()->get('slack-update-types');
// get plugin instances
self::$pluginInstance = self::getPluginInstance(null);

$updateTypes = $this->getConfig(self::$pluginInstance)->get('slack-update-types');

// Listen for osTicket to tell us it's made a new ticket or updated
// an existing ticket:
Expand Down Expand Up @@ -45,7 +57,7 @@ function onTicketCreated(Ticket $ticket) {
}

// if slack-update-types is "updatesOnly", then don't send this!
if($this->getConfig()->get('slack-update-types') == 'updatesOnly') {return;}
if($this->getConfig(self::$pluginInstance)->get('slack-update-types') == 'updatesOnly') {return;}

// Convert any HTML in the message into text
$plaintext = Format::html2text($ticket->getMessages()[0]->getBody()->getClean());
Expand Down Expand Up @@ -75,7 +87,7 @@ function onTicketUpdated(ThreadEntry $entry) {
}

// if slack-update-types is "newOnly", then don't send this!
if($this->getConfig()->get('slack-update-types') == 'newOnly') {return;}
if($this->getConfig(self::$pluginInstance)->get('slack-update-types') == 'newOnly') {return;}

if (!$entry instanceof MessageThreadEntry) {
// this was a reply or a system entry.. not a message from a user
Expand Down Expand Up @@ -124,13 +136,13 @@ function sendToSlack(Ticket $ticket, $heading, $body, $colour = 'good') {
error_log("Slack plugin called too early.");
return;
}
$url = $this->getConfig()->get('slack-webhook-url');
$url = $this->getConfig(self::$pluginInstance)->get('slack-webhook-url');
if (!$url) {
$ost->logError('Slack Plugin not configured', 'You need to read the Readme and configure a webhook URL before using this.');
}

// Check the subject, see if we want to filter it.
$regex_subject_ignore = $this->getConfig()->get('slack-regex-subject-ignore');
$regex_subject_ignore = $this->getConfig(self::$pluginInstance)->get('slack-regex-subject-ignore');
// Filter on subject, and validate regex:
if ($regex_subject_ignore && preg_match("/$regex_subject_ignore/i", $ticket->getSubject())) {
$ost->logDebug('Ignored Message', 'Slack notification was not sent because the subject (' . $ticket->getSubject() . ') matched regex (' . htmlspecialchars($regex_subject_ignore) . ').');
Expand All @@ -140,7 +152,7 @@ function sendToSlack(Ticket $ticket, $heading, $body, $colour = 'good') {
$heading = $this->format_text($heading);

// Pull template from config, and use that.
$template = $this->getConfig()->get('message-template');
$template = $this->getConfig(self::$pluginInstance)->get('message-template');
// Add our custom var
$custom_vars = [
'slack_safe_message' => $this->format_text($body),
Expand Down

0 comments on commit 15c0cad

Please sign in to comment.