Skip to content

Commit

Permalink
Incorporating regex filter
Browse files Browse the repository at this point in the history
Might as well merge all the pull's!

Props to @ramonfincken for the original
  • Loading branch information
clonemeagain committed Oct 14, 2017
1 parent 5ff589a commit eee1c53
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ When a user replies, you'll get something like:

![slack-reply](https://user-images.githubusercontent.com/5077391/31572648-9279eb18-b0f6-11e7-97da-9a9c63a200d4.png)

Notes, Replies from Agents and System messages shouldn't appear.
Notes, Replies from Agents and System messages shouldn't appear.

## Adding pull's from original repo:
+0.2 - 17 december 2016
+[feature] "Ignore when subject equals regex" by @ramonfincken
28 changes: 22 additions & 6 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,41 @@ function ($x, $y, $n) {
return Plugin::translate('slack');
}

function pre_save($config, &$errors) {
if ($config['slack-regex-subject-ignore'] && false === @preg_match("/{$config['slack-regex-subject-ignore']}/i", null)) {
$errors['err'] = 'Your regex was invalid, try something like "spam", it will become: "/spam/i" when we use it.';
return FALSE;
}
return TRUE;
}

function getOptions() {
list ($__, $_N) = self::translate();

return array(
'slack' => new SectionBreakField(array(
'slack' => new SectionBreakField(array(
'label' => $__('Slack notifier'),
'hint' => $__('Create a new app for your workspace first!')
'hint' => $__('Readme first: https://github.com/clonemeagain/osticket-slack')
)),
'slack-webhook-url' => new TextboxField(array(
'label' => 'Webhook URL',
'slack-webhook-url' => new TextboxField(array(
'label' => $__('Webhook URL'),
'configuration' => array(
'size' => 100,
'length' => 200
),
)),
'display-dept' => new BooleanField([
'display-dept' => new BooleanField([
'label' => $__('Add field for Department in Slack notice'),
'default' => FALSE,
])
]),
'slack-regex-subject-ignore' => new TextboxField([
'label' => $__('Ignore when subject equals regex'),
'hint' => $__('Auto delimited, always case-insensitive'),
'configuration' => [
'size' => 30,
'length' => 200
],
]),
);
}

Expand Down
20 changes: 16 additions & 4 deletions slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ function onTicketCreated(Ticket $ticket) {
$plaintext = Format::html2text($ticket->getMessages()[0]->getBody()->getClean());

// Format the messages we'll send.
$heading = sprintf('%s CONTROLSTART%sscp/tickets.php?id=%d|#%s - %sCONTROLEND %s'
$heading = sprintf('%s CONTROLSTART%sscp/tickets.php?id=%d|#%sCONTROLEND %s'
, __("New Ticket")
, $cfg->getBaseUrl()
, $ticket->getId()
, $ticket->getNumber()
, $ticket->getSubject()
, __("created"));
$body = sprintf('%s %s (%s) %s'
, __("Created by")
Expand Down Expand Up @@ -75,6 +74,10 @@ function onTicketUpdated(ThreadEntry $entry) {

// Need to fetch the ticket from the ThreadEntry
$ticket = $this->getTicket($entry);
if (!$ticket instanceof Ticket) {
// Admin created ticket's won't work here.
return;
}

// Check to make sure this entry isn't the first (ie: a New ticket)
$first_entry = $ticket->getMessages()[0];
Expand All @@ -86,12 +89,11 @@ function onTicketUpdated(ThreadEntry $entry) {
$plaintext = Format::html2text($entry->getBody()->getClean());

// Format the messages we'll send
$heading = sprintf('%s CONTROLSTART%sscp/tickets.php?id=%d|#%s %sCONTROLEND %s'
$heading = sprintf('%s CONTROLSTART%sscp/tickets.php?id=%d|#%sCONTROLEND %s'
, __("Ticket")
, $cfg->getBaseUrl()
, $ticket->getId()
, $ticket->getNumber()
, $ticket->getSubject()
, __("updated"));
$body = sprintf('%s %s (%s) %s %s %s'
, __("by")
Expand Down Expand Up @@ -125,6 +127,16 @@ function sendToSlack(Ticket $ticket, $heading, $body, $colour = 'good') {
$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');
// 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) . ').');
return;
} else {
error_log("$ticket_subject didn't trigger $regex_subject_ignore");
}

$heading = $this->format_text($heading);
$body = $this->format_text($body);

Expand Down

0 comments on commit eee1c53

Please sign in to comment.