API notifications

Hello

is there any way to get notification regarding new ticket/change of status/… via API?

br
miha

You can write a scrip that sends a payload to an API endpoint if that is what you’re trying to do?

Hi

Can You maybe give me a simple example?

I would very greatfull, just to know how it is done.

There is a good example here of creating a custom action to hit an external API (Slacks API in this instance):

If you want to stick to just RT::Scrips you can use something like the following in your custom action code and have a blank template:

use LWP::UserAgent;

my $ua = LWP::UserAgent->new;

my $server_endpoint = "http://192.168.1.1:8000/service";

# set custom HTTP request header fields
my $req = HTTP::Request->new(GET => $server_endpoint);
$req->header('content-type' => 'application/json');
$req->header('x-auth-token' => 'kfksj48sdfj4jd9d');

my $resp = $ua->request($req);
if ($resp->is_success) {
    my $message = $resp->decoded_content;
    RT::Logger->debug("Received reply: $message\n");
}
else {
    RT::Logger->error("HTTP GET error code: ", $resp->code, "\n");
    RT::Logger->error("HTTP GET error message: ", $resp->message, "\n");
    return 0;
}

return 1;

If you have some more information on what you are trying to doI may be able to offer some more assistance.

Hi

Thank you so much for your help.

What i need is just to get notficiation via http post (json would be the best way) for every new ticket, change of status of ticket, new comment of the ticket.

And maybe to somehow filter for which new ticket (create, status change,…) i would like to get this:)

That is all :slight_smile:

Tnx!

Using the Slack extension as an example you can create a custom action (like NotifySlack) called Notify<SomeAPI>.

Then you can create scrips that use this action and any condition you want OnCreate, OnComment ect.

You can start by creating your action module file:

local/lib/RT/Action/Notify<SomeAPI>.pm

Then in there you will need to use the RT::Action base:

 use base 'RT::Action';

Then you will need to defined two sub routines “sub Prepare” and “sub Commit”, you can add code to the commit sub routine to send the POST/GET requests to your external API.

Hello

I do like this. Admin->Scripts->Create

Description: Something

Condition: On Create (or on Comment, I create multiple Scripts with same action but different condition)

Action: UserDefined

Template: Blank

Stage: Normal

What about here, i put code under: Costume condition or Costume action preparation code or costume action commit code?

Did I understand you correctly?

Can you please explain to me a little bit more: “Then you will need to defined two sub routines “sub Prepare” and “sub Commit”, you can add code to the commit sub routine to send the POST/GET requests to your external API.”

br

miha

If you would like to do it in the web UI then you will not need to define any sub routines. The reason that I suggested creating a custom action file is so that the same code can be used with multiple conditions.

If it is easier to just work off the web UI then that is fine, you can just copy and paste the custom action code when you wish to create a new scrip.

For the action preparation code you can just have:

return 1;

Then in the custom commit code you can write the code that does the GET/POST request like the example using LWP::UserAgent from above.

Hopefully this makes things a little more clear!

@craig. I tried your first exp and it worked. What I did is that I changed from “GET” to “POST”.
on the server side, to where request is send I can see that I get all information but it is not json request it is urlencoded type of request.

What I would like to know is:

  • why it is not json if it is defined like json?
    $req-&gt;header('content-type' =&gt; 'application/json');

  • how data is set? There is no defenition in request for data, what should be send? How do I get this info reqarding new ticket that I would like to send in request?

$response = $ua->post($url, Content => $json);

in your exp there is only: my $req = HTTP::Request-&gt;new(GET =&gt; $server_endpoint); I changed to POST

The example I posted was one I found off of Google, you can look at some documentation for HTTP::Request here:

They have this as an example POST:

#!/usr/bin/env perl
 
use strict;
use warnings;
 
use Encode qw(encode_utf8);
use HTTP::Request ();
use JSON::MaybeXS qw(encode_json);
 
my $url = 'https://www.example.com/api/user/123';
my $header = ['Content-Type' => 'application/json; charset=UTF-8'];
my $data = {foo => 'bar', baz => 'quux'};
my $encoded_data = encode_utf8(encode_json($data));
 
my $r = HTTP::Request->new('POST', $url, $header, $encoded_data);
# at this point, we could send it via LWP::UserAgent
# my $ua = LWP::UserAgent->new();
# my $res = $ua->request($r);

@craig thank you so much for all your help!
This is clear, just one thing that bothers me. I guess I get data from: $self->TicketObj ?

Can you just please help me, as i am new to perl, how to get values from this HASH data, as this is multiple dimension array :slight_smile:

tnx!

Glad to help, when in doubt check the docs :wink:

https://docs.bestpractical.com/rt/4.4.4/RT/Ticket.html
https://docs.bestpractical.com/rt/4.4.4/RT/Transaction.html

In a scrip you have $self->TicketObj and $self->TransactionObj that you usually work with. If you wanted to get the status of your ticket you can do this:

my $status = $self->TicketObj->Status;

For testing purposes you can also add debug/error logging by adding:

RT::Logger->error("Could not get status") unless $status;

This is interesting. Under script I have this:

use strict;
use warnings;
 
use Encode qw(encode_utf8);
use HTTP::Request ();
use JSON::MaybeXS qw(encode_json);


my $ua = LWP::UserAgent->new;





my $url = 'http://172.31.1.121:8888/test';
my $header = ['Content-Type' => 'application/json; charset=UTF-8'];
my $data = {status => $self->TicketObj->Status};
my $encoded_data = encode_utf8(encode_json($data));
 
my $r = HTTP::Request->new('POST', $url, $header, $encoded_data);

return 1;

I am pasting picture of wireshark trace. You see what is sending to server?

I am not familiar with wireshark output, but I believe you’ve only created the request object. You now need to send it:

my $r = HTTP::Request->new('POST', $url, $header, $encoded_data);
# at this point, we could send it via LWP::UserAgent
# my $ua = LWP::UserAgent->new();
# my $res = $ua->request($r);

perfect! everything is ok, my mistake!

Thank you so much for all this and your help!

I installed RT::Extension::NotifySlack from the Github repo. It installed easily, and I see the “On create alert slack channel” scrip available. I modified my RT_SiteConfig.pm file to include the desired Slack channel and included my webhook:

**#RT_SiteConfig.pm:**

** Plugin(‘RT::Extension::NotifySlack’);**
** Set( %SlackWebHookUrls,**
** ‘#soc’ => “Customize your workspace | Slack”**
** );**

However it is still trying to access #general, as per my error logs:
#Error log:
** [32583] [Tue Jul 28 13:56:24 2020] [error]: Slack channel: #general not found. Check %SlackWebHookUrls config values. (/opt/rt4/local/plugins/RT-Extension-NotifySlack/lib/RT/Action/NotifySlack.pm:38)**

The only thing I can think that might be causing a hitch is that the #soc channel is protected, but I am able to post to it with cURL, so I wouldn’t think that would be the issue. Anyone have any idea what I am missing?

Thank you,

Peter

How are you calling this action? With an RT::Scrip or using the rt-crontool?

I’m using RT::Scrip with the scrip that was installed by the make install process.

The default scrip action is setup to point to channel general:

  @ScripActions = (
        {
            Name        => 'Notify Slack',
            Description => 'Post ticket updates to slack channel',
            ExecModule  => 'NotifySlack',
            Argument    => '#general',
        },
   );

You can create a new action or update the existing one ( I don’t recall offhand 100% if you can modify an existing action created by code ) to point to your new slack channel.

If you’re on RT 4.4.4 and up you should be able to go to Admin->Global->Actions in the web UI