Skip to content

Commit

Permalink
Refactored, upgraded, enhanced!
Browse files Browse the repository at this point in the history
tested on 1.10.1, works a treat.
  • Loading branch information
clonemeagain committed Oct 14, 2017
1 parent 5852fac commit c78303b
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 98 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![Slack](https://a.slack-edge.com/ae57/img/slack_api_logo.png)

osTicket-slack
==============
An plugin for [osTicket](https://osticket.com) which posts notifications to a [Slack](https://slack.com) channel.
Expand All @@ -8,4 +10,30 @@ Clone this repo or download the zip file and place the contents into your `inclu

Info
------
This plugin uses CURL and tested on osTicket-1.8.
This plugin uses CURL and tested on osTicket-1.10.1

== Requirements ==
- php_curl
- A slack account

== To Install into Slack ==
-Navigate to https://api.slack.com/ select "Start Building"
-Name your App `osTicket Notification`, select your Workspace from the drop-down
-Select "Incoming Webhooks"
-Activate the Webhooks with the link (it defaults to Off, just click Off to change it to On)
-Scroll to the bottom and select "Add a new Webhook to Workspace"
-Select the endpoint of the webhook, (ie, channel to post to)
-Select "Authorize"
-Scroll down and copy the Webhook URL entirely, paste this into the Plugin config.

The channel you select will receive an event notice, like:
```
Aaron [10:56 AM] added an integration to this channel: osTicket Notification
```
You should also receive an email from Slack telling you about the new Integration.

== Test! ==
Create a ticket!

You should see something like:
![slack](https://user-images.githubusercontent.com/5077391/31570760-2a5bec20-b0d3-11e7-9429-c25e8cdcf328.png)
41 changes: 41 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

require_once INCLUDE_DIR . 'class.plugin.php';

class SlackPluginConfig extends PluginConfig {

// Provide compatibility function for versions of osTicket prior to
// translation support (v1.9.4)
function translate() {
if (!method_exists('Plugin', 'translate')) {
return array(
function ($x) {
return $x;
},
function ($x, $y, $n) {
return $n != 1 ? $y : $x;
}
);
}
return Plugin::translate('slack');
}

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

return array(
'slack' => new SectionBreakField(array(
'label' => $__('Slack notifier'),
'hint' => $__('Create a new app for your workspace first!')
)),
'slack-webhook-url' => new TextboxField(array(
'label' => 'Webhook URL',
'configuration' => array(
'size' => 100,
'length' => 200
),
)),
);
}

}
11 changes: 11 additions & 0 deletions plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return array(
'id' => 'osticket:slack',
'version' => '0.2',
'name' => 'Slack notifier',
'author' => 'Thammanna Jammada',
'description' => 'Notify Slack on new ticket.',
'url' => 'https://github.com/thammanna/osticket-slack',
'plugin' => 'slack.php:SlackPlugin',
);
101 changes: 101 additions & 0 deletions slack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

require_once(INCLUDE_DIR . 'class.signal.php');
require_once(INCLUDE_DIR . 'class.plugin.php');
require_once(INCLUDE_DIR . 'class.ticket.php');
require_once(INCLUDE_DIR . 'class.osticket.php');
require_once(INCLUDE_DIR . 'class.config.php');
require_once('config.php');

class SlackPlugin extends Plugin {

var $config_class = "SlackPluginConfig";

function bootstrap() {
Signal::connect('ticket.created', function(Ticket $ticket) {
$this->onTicketCreated($ticket);
});
}

function onTicketCreated(Ticket $ticket) {
$c = $this->getConfig();

global $ost, $cfg;
if (!$ost instanceof osTicket || !$cfg instanceof OsticketConfig) {
error_log("Slack plugin called too early.");
return;
}
$msg = sprintf('%s CONTROLSTART%sscp/tickets.php?id=%d|#%sCONTROLEND %s'
, __("New Ticket")
, $cfg->getBaseUrl()
, $ticket->getId()
, $ticket->getNumber()
, __("created"));
$body = sprintf('%s %s (%s) %s %s (%s) %s %s %s CONTROLSTART!date^%d^{date} {time}|%sCONTROLEND'
, __("created by")
, $ticket->getName()
, $ticket->getEmail()
, __('in')
, $ticket->getDeptName()
, __('Department')
, __('via')
, $ticket->getSource()
, __('on')
, strtotime($ticket->getCreateDate())
, $ticket->getCreateDate());

// Obey message formatting rules:https://api.slack.com/docs/message-formatting
$formatter = ['<' => '&lt;', '>' => '&gt;', '&' => '&amp;'];
$msg = str_replace(array_keys($formatter), array_values($formatter), $msg);
$body = str_replace(array_keys($formatter), array_values($formatter), $body);
// put the <>'s control characters back in
$moreformatter = ['CONTROLSTART' => '<', 'CONTROLEND' => '>'];
$msg = str_replace(array_keys($moreformatter), array_values($moreformatter), $msg);
$body = str_replace(array_keys($moreformatter), array_values($moreformatter), $body);
error_log("Message: $msg");
error_log("BODY: $body");

try {
$payload['attachments'][] = [
'pretext' => $msg,
'fallback' => $msg,
'color' => "#D00000",
'fields' =>
[
[
'title' => $ticket->getSubject(),
'value' => $body,
'short' => FALSE,
'mrkdwn' => false,
],
],
];

$data_string = utf8_encode(json_encode($payload));
$url = $c->get('slack-webhook-url');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);

if (curl_exec($ch) === false) {
throw new \Exception($url . ' - ' . curl_error($ch));
} else {
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($statusCode != '200') {
throw new \Exception($url . ' Http code: ' . $statusCode);
}
}
curl_close($ch);
} catch (\Exception $e) {
$ost->logError('Slack posting issue!', $e->getMessage(), true);
error_log('Error posting to Slack. ' . $e->getMessage());
}
}

}
17 changes: 0 additions & 17 deletions slack/config.php

This file was deleted.

13 changes: 0 additions & 13 deletions slack/plugin.php

This file was deleted.

67 changes: 0 additions & 67 deletions slack/slack.php

This file was deleted.

0 comments on commit c78303b

Please sign in to comment.