[rt-users] Slack integration?

Maciej Dobrzanski reg.bestpractical at posterus.com
Fri Jun 12 13:44:29 EDT 2015


I wrote a module for this a while back. You just need to save it as 
local/lib/RT/Extension/Slack.pm and in RT_SiteConfig.pm you need to 
define SlackIncomingWebhookURL and SlackIncomingWebhookToken. Then you 
can simply call RT::Extension::Slack::Notify(%paramhash) wherever you 
need.

For example:

     RT::Extension::Slack::Notify(username   => 'Mr. RT',
                                  icon_emoji => 
':heavy_exclamation_mark:',
                                  text       => $text);

--- Slack.pm ---
package RT::Extension::Slack;

use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use JSON;

sub Notify {
   my %args = @_;
   my $payload = {
     username   => 'Mr. RT',
     icon_emoji => ':ghost:',
     text       => 'I have forgotten to say something',
   };
   my $service_endpoint;
   my $service_token;

   foreach (keys %args) {
     $payload->{$_} = $args{$_};
   }
   if (!$payload->{text}) {
     return;
   }
   my $payload_json = encode_json($payload);

   $service_endpoint = RT->Config->Get('SlackIncomingWebhookURL');
   $service_token = RT->Config->Get('SlackIncomingWebhookToken');
   if (!$service_endpoint || !$service_token) {
     return;
   }

   my $ua = LWP::UserAgent->new;
   $ua->timeout(10);

   $RT::Logger->info('Pushing notification to Slack: '. $payload_json);
   my $r = POST($service_endpoint .'?token='. $service_token,
                [ 'payload' => $payload_json ]);

   my $response = $ua->request($r);
   if ($response->is_success) {
     $RT::Logger->info('Successfully pushed notification to Slack');
   }
   else {
     $RT::Logger->error('Failed to push notification to Slack ('.
                        $response->code .': '. $response->message .')');
   }
}

1;
--- Slack.pm ---

This is an example action module that uses the Slack extension:

--- NotifySlackOnCreate.pm ---
use strict;
use warnings;

use base qw(RT::Action);

=head2 Prepare

=cut

sub Prepare {
     my $self = shift;

     return 1;
}

=head2 Commit

=cut
sub Commit {
     my $self = shift;
     my $text;
     my $requestor;
     my $ticket = $self->TicketObj;
     my $queue = $ticket->QueueObj;
     my $url = join '',
                    RT->Config->Get('WebPort') == 443 ? 'https' : 'http', 
'://',
                    RT->Config->Get('WebDomain'),
                    RT->Config->Get('WebPath'),
                    '/Ticket/Display.html?id=',
                    $ticket->Id;

     $requestor = $ticket->RequestorAddresses || 'unknown';
     $text = sprintf('Ticket #%d by %s has been created in %s | '.
                     '<%s|click to open>', $ticket->Id, $requestor,
                     $queue->Name, $url);

     RT::Extension::Slack::Notify(icon_emoji => 
':heavy_exclamation_mark:',
                                  text       => $text);

     return 1;
}

1;
--- NotifySlackOnCreate.pm ---

Of course you can just turn the action module into a Scrip easily and 
use it for any event.

Maciek




More information about the rt-users mailing list