Skip to content

Commit eee1c53

Browse files
committed
Incorporating regex filter
Might as well merge all the pull's! Props to @ramonfincken for the original
1 parent 5ff589a commit eee1c53

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ When a user replies, you'll get something like:
4545

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

48-
Notes, Replies from Agents and System messages shouldn't appear.
48+
Notes, Replies from Agents and System messages shouldn't appear.
49+
50+
## Adding pull's from original repo:
51+
+0.2 - 17 december 2016
52+
+[feature] "Ignore when subject equals regex" by @ramonfincken

config.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,41 @@ function ($x, $y, $n) {
2020
return Plugin::translate('slack');
2121
}
2222

23+
function pre_save($config, &$errors) {
24+
if ($config['slack-regex-subject-ignore'] && false === @preg_match("/{$config['slack-regex-subject-ignore']}/i", null)) {
25+
$errors['err'] = 'Your regex was invalid, try something like "spam", it will become: "/spam/i" when we use it.';
26+
return FALSE;
27+
}
28+
return TRUE;
29+
}
30+
2331
function getOptions() {
2432
list ($__, $_N) = self::translate();
2533

2634
return array(
27-
'slack' => new SectionBreakField(array(
35+
'slack' => new SectionBreakField(array(
2836
'label' => $__('Slack notifier'),
29-
'hint' => $__('Create a new app for your workspace first!')
37+
'hint' => $__('Readme first: https://github.com/clonemeagain/osticket-slack')
3038
)),
31-
'slack-webhook-url' => new TextboxField(array(
32-
'label' => 'Webhook URL',
39+
'slack-webhook-url' => new TextboxField(array(
40+
'label' => $__('Webhook URL'),
3341
'configuration' => array(
3442
'size' => 100,
3543
'length' => 200
3644
),
3745
)),
38-
'display-dept' => new BooleanField([
46+
'display-dept' => new BooleanField([
3947
'label' => $__('Add field for Department in Slack notice'),
4048
'default' => FALSE,
41-
])
49+
]),
50+
'slack-regex-subject-ignore' => new TextboxField([
51+
'label' => $__('Ignore when subject equals regex'),
52+
'hint' => $__('Auto delimited, always case-insensitive'),
53+
'configuration' => [
54+
'size' => 30,
55+
'length' => 200
56+
],
57+
]),
4258
);
4359
}
4460

slack.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ function onTicketCreated(Ticket $ticket) {
4040
$plaintext = Format::html2text($ticket->getMessages()[0]->getBody()->getClean());
4141

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

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

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

8891
// Format the messages we'll send
89-
$heading = sprintf('%s CONTROLSTART%sscp/tickets.php?id=%d|#%s %sCONTROLEND %s'
92+
$heading = sprintf('%s CONTROLSTART%sscp/tickets.php?id=%d|#%sCONTROLEND %s'
9093
, __("Ticket")
9194
, $cfg->getBaseUrl()
9295
, $ticket->getId()
9396
, $ticket->getNumber()
94-
, $ticket->getSubject()
9597
, __("updated"));
9698
$body = sprintf('%s %s (%s) %s %s %s'
9799
, __("by")
@@ -125,6 +127,16 @@ function sendToSlack(Ticket $ticket, $heading, $body, $colour = 'good') {
125127
$ost->logError('Slack Plugin not configured', 'You need to read the Readme and configure a webhook URL before using this.');
126128
}
127129

130+
// Check the subject, see if we want to filter it.
131+
$regex_subject_ignore = $this->getConfig()->get('slack-regex-subject-ignore');
132+
// Filter on subject, and validate regex:
133+
if ($regex_subject_ignore && preg_match("/$regex_subject_ignore/i", $ticket->getSubject())) {
134+
$ost->logDebug('Ignored Message', 'Slack notification was not sent because the subject (' . $ticket->getSubject() . ') matched regex (' . htmlspecialchars($regex_subject_ignore) . ').');
135+
return;
136+
} else {
137+
error_log("$ticket_subject didn't trigger $regex_subject_ignore");
138+
}
139+
128140
$heading = $this->format_text($heading);
129141
$body = $this->format_text($body);
130142

0 commit comments

Comments
 (0)