Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi21000 committed Sep 13, 2019
1 parent 44c8ec7 commit d831069
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 14 deletions.
53 changes: 53 additions & 0 deletions plugin/config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

require_once INCLUDE_DIR . 'class.plugin.php';
include_once(INCLUDE_DIR . 'class.dept.php');
include_once(INCLUDE_DIR . 'class.list.php');


class DiscordPluginConfig extends PluginConfig {
function getOptions() {
Expand All @@ -15,6 +18,56 @@ function getOptions() {
'length' => 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(),
)),

);
}
}
92 changes: 79 additions & 13 deletions plugin/discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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()));
Expand All @@ -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';
Expand All @@ -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));
Expand Down Expand Up @@ -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');
}

}

}
}
2 changes: 1 addition & 1 deletion plugin/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down

0 comments on commit d831069

Please sign in to comment.