From d831069e500670ba658bacdc8e23d89f5ca28f7e Mon Sep 17 00:00:00 2001 From: bibi21000 Date: Fri, 13 Sep 2019 18:21:58 +0200 Subject: [PATCH] First release --- plugin/config.php | 53 ++++++++++++++++++++++++++ plugin/discord.php | 92 +++++++++++++++++++++++++++++++++++++++------- plugin/plugin.php | 2 +- 3 files changed, 133 insertions(+), 14 deletions(-) diff --git a/plugin/config.php b/plugin/config.php index 1a9372b..2d8f610 100644 --- a/plugin/config.php +++ b/plugin/config.php @@ -1,6 +1,9 @@ 200, ), )), + + 'notifs' => new SectionBreakField(array( + 'label' => 'Notifications', + )), + 'notify-new' => new BooleanField(array( + 'label' => 'Notify on New Ticket', + 'hint' => 'Send a discord notification to the above webhook whenever a new ticket is created.', + 'default' => TRUE, + )), + 'notify-update' => new BooleanField(array( + 'label' => 'Notify on updated Ticket', + 'hint' => 'Send a discord notification to the above webhook whenever a ticket is updated.', + 'default' => FALSE, + )), + 'alerts' => new SectionBreakField(array( + 'label' => 'Alerts', + )), + 'alert-active' => new BooleanField(array( + 'label' => 'Activate the alert managment.', + 'hint' => 'Send a discord notification and close ticket for a specific-department.', + 'default' => FALSE, + )), + 'alert-delay' => new TextboxField(array( + 'label' => 'The delay before changing state of the alerts.', + 'hint' => 'The delay before changing state of the alerts. 0 for on ticket creation or a value in minutes.', + 'configuration' => array( + 'size' => 5, + 'length' => 5, + ), + 'default' => 0, + )), + 'alert-dept_id' => new ChoiceField(array( + 'default' => 0, + 'required' => true, + 'label' => 'The Department used for alerts', + 'hint' => 'All tickets in this Department are considered as alerts', + 'choices' => + array(0 => '— '.'Primary Department'.' —') + + Dept::getDepartments(), + )), + 'alert-status_id' => new ChoiceField(array( + 'default' => 0, + 'required' => true, + 'label' => 'Status to set alerts to', + 'hint' => 'Status to use for alerts', + 'choices' => + array(0 => '— '.'Status to use'.' —') + + TicketStatusList::getStatuses()->all(), + )), + ); } } diff --git a/plugin/discord.php b/plugin/discord.php index c669ff9..254fffe 100644 --- a/plugin/discord.php +++ b/plugin/discord.php @@ -9,11 +9,17 @@ class DiscordPlugin extends Plugin { function bootstrap() { Signal::connect('ticket.created', array($this, 'onTicketCreated')); - #Signal::connect('threadentry.created', array($this, 'onTicketUpdated')); + Signal::connect('threadentry.created', array($this, 'onTicketUpdated')); + Signal::connect('cron', array($this, 'onCron')); } - function onTicketCreated($ticket){ + function onTicketCreated(Ticket $ticket){ global $ost, $cfg; + if ($this->getConfig()->get('notify-new') == False ) { + $ost->logDebug(_S('Discord plugin'), + _S('notify-new is disabled. Exiting')); + return; + } try { $author['name'] = sprintf('%s (%s)', $ticket->getEmail()->getName(), $ticket->getEmail()); @@ -33,7 +39,14 @@ function onTicketCreated($ticket){ $payload['embeds'] = $embeds; $this->discordMessage($payload); - + + if ( $this->getConfig()->get('alert-active') and + $ticket->getDeptId()== $this->getConfig()->get('alert-dept_id') and + intval($this->getConfig()->get('alert-delay'))==0) { + $ost->logDebug(_S('Discord plugin'), + sprintf(_S('Matching dept_id %s'), $ticket->getDeptId())); + $ticket->setStatus($this->getConfig()->get('alert-status_id'), $comments='Closed by discord-notifier'); + } } catch(Exception $e) { error_log(sprintf('Error onTicketCreated to Discord Webhook. %s', $e->getMessage())); @@ -43,13 +56,30 @@ function onTicketCreated($ticket){ } } - function onTicketUpdated($ticket){ + function onTicketUpdated(ThreadEntry $entry){ global $ost, $cfg; + if ( $this->getConfig()->get('notify-update') == False ) { + $ost->logDebug(_S('Discord plugin'), + _S('notify-update is disabled. Exiting')); + return; + } try { - - $author['name'] = sprintf('%s (%s)', $ticket->getEmail()->getName(), $ticket->getEmail()); + // Need to fetch the ticket from the ThreadEntry + $ticket = $this->getTicket($entry); + if (!$ticket instanceof Ticket) { + // Admin created ticket's won't work here. + $ost->logDebug("Discord ignoring message", "Because there is no associated ticket."); + return; + } + // Check to make sure this entry isn't the first (ie: a New ticket) + $first_entry = $ticket->getMessages()[0]; + if ($entry->getId() == $first_entry->getId()) { + $ost->logDebug("Discord ignoring message", "Because we don't want to notify twice on new Tickets"); + return; + } + $author['name'] = sprintf('%s (%s)', $entry->getEmail()->getName(), $entry->getEmail()); $ost->logDebug(_S('Discord plugin'), - sprintf(_S("Email %s"), $ticket->getEmail())); + sprintf(_S("Email %s"), $entry->getEmail())); $fields[0]['name'] = 'field name'; $fields[0]['value'] = 'field value'; @@ -58,24 +88,26 @@ function onTicketUpdated($ticket){ $embeds[0]['author'] = $author; $embeds[0]['type'] = 'rich'; $embeds[0]['color'] = 0xff0000; - $embeds[0]['title'] = $ticket->getSubject(); - $embeds[0]['url'] = $cfg->getUrl() . 'scp/tickets.php?id=' . $ticket->getId(); - $embeds[0]['description'] = strip_tags($ticket->getLastMessage()->getBody()->getClean()); + $embeds[0]['title'] = $entry->getSubject(); + $embeds[0]['url'] = $cfg->getUrl() . 'scp/tickets.php?id=' . $entry->getId(); + $embeds[0]['description'] = strip_tags($entry->getLastMessage()->getBody()->getClean()); $payload['embeds'] = $embeds; $this->discordMessage($payload); - + + } catch(Exception $e) { - error_log(sprintf('Error onTicketCreated to Discord Webhook. %s', $e->getMessage())); + error_log(sprintf('Error onTicketUpdated to Discord Webhook. %s', $e->getMessage())); $ost->logError(_S('Discord plugin'), - sprintf(_S('Error onTicketCreated to Discord Webhook. %s'), + sprintf(_S('Error onTicketUpdated to Discord Webhook. %s'), $e->getMessage())); } } function discordMessage($payload){ global $ost, $cfg; + try { $data_string = utf8_encode(json_encode($payload)); @@ -129,4 +161,38 @@ function discordMessage($payload){ } } + /** + * Cooperates with the cron system to automatically find content that is + * not indexed in the _search table and add it to the index. + */ + function onCron() { + global $ost, $cfg; + if ($this->getConfig()->get('alert-active') and + intval($this->getConfig()->get('alert-delay'))>0) { + + $sql = "SELECT `ticket_id` FROM `".TICKET_TABLE."` + WHERE dept_id = " . $this->getConfig()->get('alert-dept_id') . " + AND status_id != " . $this->getConfig()->get('alert-status_id') . " + AND DATE_ADD(created, INTERVAL " . $this->getConfig()->get('alert-delay') ." MINUTE) < NOW() + ORDER BY `ticket_id` DESC LIMIT 10"; + // AND DATE_ADD(created, INTERVAL " . $this->getConfig()->get('alert-delay') ." MINUTE) < NOW() > + + if (!($res = db_query_unbuffered($sql, $auto_create))) { + $ost->logDebug(_S('Discord plugin'), + _S('Error in select for tickets')); + return false; + } + + while ($row = db_fetch_row($res)) { + if (!($ticket = Ticket::lookup($row[0]))) + continue; + $ost->logDebug(_S('Discord plugin'), + sprintf(_S('Matching ticket %s'), $ticket->ticket_id)); + + $ticket->setStatus($this->getConfig()->get('alert-status_id'), $comments='Closed by discord-notifier'); + } + + } + + } } diff --git a/plugin/plugin.php b/plugin/plugin.php index 1524df4..c7e5bb9 100644 --- a/plugin/plugin.php +++ b/plugin/plugin.php @@ -2,7 +2,7 @@ return array( 'id' => 'osticket:discord', - 'version' => '1.0.0', + 'version' => '1.1.0', 'name' => 'Discord notifier', 'author' => 'Sebastien GALLET', 'description' => 'Notify Discord on new ticket or new message.',