API notifications


#1

Hello

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

br
miha


#2

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


#3

Hi

Can You maybe give me a simple example?

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


#4

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;

#5

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


#6

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!


#7

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.


#8

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


#9

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!


#10

@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


#11

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);

#12

@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!


#13

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;


#14

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?


#15

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);

#16

perfect! everything is ok, my mistake!

Thank you so much for all this and your help!