-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomActionCommit.pl
98 lines (76 loc) · 3.15 KB
/
CustomActionCommit.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
## https://github.com/reed-college/slack-rt-linker
## Script must be Applied to the queues you want to integrate
## Invite @RT to the slack channels you expect it to post to
use IO::Socket::SSL;
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use JSON;
##################################
## Configure required variables:
my $slackURL = 'https://YOUR-SLACK-DOMAIN.slack.com/api/';
my $rtURL = 'https://YOUR-RT-DOMAIN/Ticket/Display.html';
# slack app Bearer token:
my $token = "Bearer YOUR-SLACK-BEARER-TOKEN";
# RT's "slack_timestamp" custom field that you need to create
my $rtSlackTimestampCF = "slack_timestamp";
##################################
## Configure Queue to Channel mapping:
## This allows one set of scrips to function against all of your queues using a single slack app.
## You can get your channel id by visiting slack in a browser and viewing a link to your channel
# get queue of this ticket:
my $queue = $self->TicketObj->QueueObj->Name;
my $channel = "";
if ($queue eq "RT-QUEUE1") {
$channel = "SLACK-CHANNEL-ID1";
} elsif ($queue eq "RT-QUEUE2") {
$channel = "SLACK-CHANNEL-ID2";
} elsif ($queue eq "RT-QUEUE3") {
$channel = "SLACK-CHANNEL-ID3";
}
################################
########## You should not need to modify anything below this, but obviously feel free to make it your own! ##################
my $ticketID = $self->TicketObj->id;
my $ticketURL = '<'.$rtURL.'?id='.$ticketID.'|#'.$ticketID.'>';
my $ticketSubject = $self->TicketObj->Subject;
my $requestorName = "";
$requestorName = eval { $self->TicketObj->Requestors->UserMembersObj->First->RealName };
my $owner = $self->TicketObj->OwnerObj->Name;
# if not owned, show Take link. If owned, show Steal link & text
my $stealText = "";
my $rtAction = "";
if ($owner eq "Nobody"){
$stealText = "";
$rtAction = "Take";
} else {
$stealText = "from ".$owner;
$rtAction = "Steal";
}
my $ticketActionURL = '<'.$rtURL.'?Action='.$rtAction.';id='.$ticketID.'|'.$rtAction.'>';
my $data = {channel => $channel, text => $requestorName.' ['.$ticketURL.' '.$ticketSubject.'] '.$ticketActionURL.' '.$stealText};
# Are we posting a new message or updating an existing one?
my $slackTimestampCFValue = $self->TicketObj->FirstCustomFieldValue('slack_timestamp');
if ( defined $slackTimestampCFValue ) {
$slackURL .= "chat.update";
$data->{'ts'} = $slackTimestampCFValue;
} else {
$slackURL .= "chat.postMessage";
}
my $header = ['Content-type' => 'application/json', 'Authorization' => $token];
my $encoded_data = encode_json($data);
my $r = HTTP::Request->new('POST', $slackURL, $header, $encoded_data);
my $ua = LWP::UserAgent->new;
$ua->timeout(15);
my $resp = $ua->request($r);
if ($resp->is_success) {
RT::Logger->debug('Posted to slack!');
} else {
RT::Logger->debug("Failed post to slack, status is:" . $resp->status_line);
}
my $decoded_resp = $resp->decoded_content;
#print $decoded_resp;
my $json_decoded_resp = decode_json($decoded_resp);
# extract slack message timestamp
my $ts = $json_decoded_resp->{'ts'};
# Record the slack timestamp to RT
my ($status, $msg) = $self->TicketObj->AddCustomFieldValue( Field =>$rtSlackTimestampCF, Value => $ts );
return 1;