how can get ticket data with generic agent?

English! place to talk about development, programming and coding
Post Reply
crynof
Znuny newbie
Posts: 24
Joined: 20 Mar 2020, 20:23
Znuny Version: 6.0
Real Name: Camilo

how can get ticket data with generic agent?

Post by crynof »

Hi!, i trying to return a ticket data through GenericAgent custom script without success.

This is my code (have some fragments of code that i tried but nothing works xD):

Code: Select all

#!/usr/bin/perl

# This module is triggered by a generic agent

package Modulos::AlertaTicket;

use strict;
use warnings;
use utf8;
use lib '/opt/otrs/';
use lib '/opt/otrs/Kernel/cpan-lib';
use lib '/opt/otrs/Custom';

use Kernel::System::ObjectManager;
local $Kernel::OM = Kernel::System::ObjectManager->new(
    'Kernel::System::Log' => {
        LogPrefix => "MyLogPrefix",
    },
);

sub new {
    my ( $Type, %Param ) = @_;

    # allocate new hash for object
    my $Self = {%Param};
    bless( $Self, $Type );

    return $Self;
}

sub Run {
    my ( $Self, %Param ) = @_;

    my $TicketObject = $Kernel::OM->Get('Kernel::System::Ticket');
    my $UserObject = $Kernel::OM->Get('Kernel::System::User');
    my %Ticket = $TicketObject->TicketGet(
         TicketID => $Param{TicketID},
         UserID => 1,
         DynamicFields => 1,
		 Priority => 1,
    );

    # TODO Put here some if's and elses



    # get ticket data
  #  my %TicketData = $TicketObject->TicketGet(
   #     TicketID      => $TicketID,
    #    DynamicFields => 1,
     #   UserID        => $Param{UserID},
   # );



    my $Success = $TicketObject->TicketGet(
        TicketID => 123,
        UserID   => 123,
        Extended => 1,
    );


    # Update the TicketCustomer
	
 #   my $Success = $TicketObject->TicketCustomerSet(
   #     No       => 'client123',        # Use Number
   #     User     => 'client-user-123',  # OR customer-user
   #     TicketID => $Param{TicketID},
   #     UserID   => 1,                  # 1 for the default root account or maybe $Param{UserID}
 #   );
	

    # Return a truthy value (usually the result of your update command)
    return $Success;
}
1;
I dont kno how to print the value to be showed in chrome console (i dont know if that is possible), but i need to get the ticket data to send after by JSON.

How can i to get and print this data??
I hope you can help me.
Many thanks!!
RStraub
Znuny guru
Posts: 2210
Joined: 13 Mar 2014, 09:16
Znuny Version: 6.0.14
Real Name: Rolf Straub

Re: how can get ticket data with generic agent?

Post by RStraub »

Hey,

I think if you easily want to show it in the browser console, you'll need the "Fred" Addon from OTRS. But only use that on a test/dev system as it will kill the performance and you don't want to have it active for all users.

What I use is this in the code:

Code: Select all

    use Data::Dumper;
    $Kernel::OM->Get('Kernel::System::Log')->Log(
        Priority => 'error',
        Message  => Dumper($HashRefIwantToDump),
    );
And then just tail the OTRS or apache/nginx log.
Currently using: OTRS 6.0.14 -- MariaDB -- Ubuntu 16 LTS
crynof
Znuny newbie
Posts: 24
Joined: 20 Mar 2020, 20:23
Znuny Version: 6.0
Real Name: Camilo

Re: how can get ticket data with generic agent?

Post by crynof »

RStraub wrote: 02 Apr 2020, 14:47 Hey,

I think if you easily want to show it in the browser console, you'll need the "Fred" Addon from OTRS. But only use that on a test/dev system as it will kill the performance and you don't want to have it active for all users.

What I use is this in the code:

Code: Select all

    use Data::Dumper;
    $Kernel::OM->Get('Kernel::System::Log')->Log(
        Priority => 'error',
        Message  => Dumper($HashRefIwantToDump),
    );
And then just tail the OTRS or apache/nginx log.

Many thanks RStraub

I installed Fred, but i dont know how to get output data from ticket or any signal.

Where should to implement this code that you post?.

How can i to get a ticket data through genetic agent?

Cheers
RStraub
Znuny guru
Posts: 2210
Joined: 13 Mar 2014, 09:16
Znuny Version: 6.0.14
Real Name: Rolf Straub

Re: how can get ticket data with generic agent?

Post by RStraub »

Hey,

attach the dumper-snippet to your "package Modulos::AlertaTicket;", tail the log files and run the generic agent.

If it throws information in the log, the module is run and you should see some information from the variable you are dumping.
If it does not show anything in the logs, the generic agent does not find or run the module.
Currently using: OTRS 6.0.14 -- MariaDB -- Ubuntu 16 LTS
crynof
Znuny newbie
Posts: 24
Joined: 20 Mar 2020, 20:23
Znuny Version: 6.0
Real Name: Camilo

Re: how can get ticket data with generic agent?

Post by crynof »

RStraub wrote: 06 Apr 2020, 10:43 Hey,

attach the dumper-snippet to your "package Modulos::AlertaTicket;", tail the log files and run the generic agent.

If it throws information in the log, the module is run and you should see some information from the variable you are dumping.
If it does not show anything in the logs, the generic agent does not find or run the module.

Many thanks RStraub, y tried that but dont know how to try it. In console i enter to /opt/otrs/var/log/Daemon/, and show 5 files, i supossed that SchedulerGenericAgentTaskManagerERR.log is the correct file to monitoring with tail command, but not work.

Whats is the correct file?

I tried with less +F /var/log/messages and works, but show messages like on admin OTRS panel only, not like returned data.

Do you have a basic file to get ticket data to test?.

That is my file but i dont know if was ok.

Code: Select all

package Modulos::GetTicket;

use strict;
use warnings;
use utf8;
use lib '/opt/otrs/';
use lib '/opt/otrs/Kernel/cpan-lib';
use lib '/opt/otrs/Custom';

use Kernel::System::ObjectManager;
local $Kernel::OM = Kernel::System::ObjectManager->new(
    'Kernel::System::Log' => {
        LogPrefix => "MyLogPrefix",
    },
);

    use Data::Dumper;
    $Kernel::OM->Get('Kernel::System::Log')->Log(
        Priority => 'error',
        Message  => Dumper($HashRefIwantToDump),
    );


sub new {
    my ( $Type, %Param ) = @_;

    # allocate new hash for object
    my $Self = {%Param};
    bless( $Self, $Type );

    return $Self;
}
   
   
   my $Result = $OperationObject->Run(
        Data => {
            UserLogin            => 'some agent login',                            # UserLogin or CustomerUserLogin or AccessToken is
                                                                                   #   required
            CustomerUserLogin    => 'some customer login',
            AccessToken          => 123,

            Password             => 'some password',                               # if UserLogin or customerUserLogin is sent then
                                                                                   #   Password is required
            TicketID             => '32,33',                                       # required, could be coma separated IDs or an Array
            DynamicFields        => 0,                                             # Optional, 0 as default. Indicate if Dynamic Fields
                                                                                   #     should be included or not on the ticket content.
            Extended             => 1,                                             # Optional, 0 as default
            AllArticles          => 1,                                             # Optional, 0 as default. Set as 1 will include articles
                                                                                   #     for tickets.
            ArticleSenderType    => [ $ArticleSenderType1, $ArticleSenderType2 ],  # Optional, only requested article sender types
            ArticleOrder         => 'DESC',                                        # Optional, DESC,ASC - default is ASC
            ArticleLimit         => 5,                                             # Optional
            Attachments          => 1,                                             # Optional, 0 as default. If it's set with the value 1,
                                                                                   # attachments for articles will be included on ticket data
            GetAttachmentContents = 1                                              # Optional, 1 as default. 0|1,
            HTMLBodyAsAttachment => 1                                              # Optional, If enabled the HTML body version of each article
                                                                                   #    is added to the attachments list
        },
    );

    $Result = {
        Success      => 1,                                # 0 or 1
        ErrorMessage => '',                               # In case of an error
        Data         => {
            Ticket => [
                {
                    TicketNumber       => '20101027000001',
                    Title              => 'some title',
                    TicketID           => 123,
                    State              => 'some state',
                    StateID            => 123,
                    StateType          => 'some state type',
                    Priority           => 'some priority',
                    PriorityID         => 123,
                    Lock               => 'lock',
                    LockID             => 123,
                    Queue              => 'some queue',
                    QueueID            => 123,
                    CustomerID         => 'customer_id_123',
                    CustomerUserID     => 'customer_user_id_123',
                    Owner              => 'some_owner_login',
                    OwnerID            => 123,
                    Type               => 'some ticket type',
                    TypeID             => 123,
                    SLA                => 'some sla',
                    SLAID              => 123,
                    Service            => 'some service',
                    ServiceID          => 123,
                    Responsible        => 'some_responsible_login',
                    ResponsibleID      => 123,
                    Age                => 3456,
                    Created            => '2010-10-27 20:15:00'
                    CreateBy           => 123,
                    Changed            => '2010-10-27 20:15:15',
                    ChangeBy           => 123,
                    ArchiveFlag        => 'y',
                    TimeUnit           => 123,

                    # If DynamicFields => 1 was passed, you'll get an entry like this for each dynamic field:
                    DynamicField => [
                        {
                            Name  => 'some name',
                            Value => 'some value',
                        },
                    ],

                    # (time stamps of expected escalations)
                    EscalationResponseTime           (unix time stamp of response time escalation)
                    EscalationUpdateTime             (unix time stamp of update time escalation)
                    EscalationSolutionTime           (unix time stamp of solution time escalation)

                    # (general escalation info of nearest escalation type)
                    EscalationDestinationIn          (escalation in e. g. 1h 4m)
                    EscalationDestinationTime        (date of escalation in unix time, e. g. 72193292)
                    EscalationDestinationDate        (date of escalation, e. g. "2009-02-14 18:00:00")
                    EscalationTimeWorkingTime        (seconds of working/service time till escalation, e. g. "1800")
                    EscalationTime                   (seconds total till escalation of nearest escalation time type - response, update or solution time, e. g. "3600")

                    # (detailed escalation info about first response, update and solution time)
                    FirstResponseTimeEscalation      (if true, ticket is escalated)
                    FirstResponseTimeNotification    (if true, notify - x% of escalation has reached)
                    FirstResponseTimeDestinationTime (date of escalation in unix time, e. g. 72193292)
                    FirstResponseTimeDestinationDate (date of escalation, e. g. "2009-02-14 18:00:00")
                    FirstResponseTimeWorkingTime     (seconds of working/service time till escalation, e. g. "1800")
                    FirstResponseTime                (seconds total till escalation, e. g. "3600")

                    UpdateTimeEscalation             (if true, ticket is escalated)
                    UpdateTimeNotification           (if true, notify - x% of escalation has reached)
                    UpdateTimeDestinationTime        (date of escalation in unix time, e. g. 72193292)
                    UpdateTimeDestinationDate        (date of escalation, e. g. "2009-02-14 18:00:00")
                    UpdateTimeWorkingTime            (seconds of working/service time till escalation, e. g. "1800")
                    UpdateTime                       (seconds total till escalation, e. g. "3600")

                    SolutionTimeEscalation           (if true, ticket is escalated)
                    SolutionTimeNotification         (if true, notify - x% of escalation has reached)
                    SolutionTimeDestinationTime      (date of escalation in unix time, e. g. 72193292)
                    SolutionTimeDestinationDate      (date of escalation, e. g. "2009-02-14 18:00:00")
                    SolutionTimeWorkingTime          (seconds of working/service time till escalation, e. g. "1800")
                    SolutionTime                     (seconds total till escalation, e. g. "3600")

                    # if you use param Extended to get extended ticket attributes
                    FirstResponse                   (timestamp of first response, first contact with customer)
                    FirstResponseInMin              (minutes till first response)
                    FirstResponseDiffInMin          (minutes till or over first response)

                    SolutionInMin                   (minutes till solution time)
                    SolutionDiffInMin               (minutes till or over solution time)

                    FirstLock                       (timestamp of first lock)

                    Article => [
                        {
                            ArticleID
                            From
                            To
                            Cc
                            Subject
                            Body
                            ReplyTo
                            MessageID
                            InReplyTo
                            References
                            SenderType
                            SenderTypeID
                            IsVisibleForCustomer
                            ContentType
                            Charset
                            MimeType
                            IncomingTime
                            TimeUnit

                            # If DynamicFields => 1 was passed, you'll get an entry like this for each dynamic field:
                            DynamicField => [
                                {
                                    Name  => 'some name',
                                    Value => 'some value',
                                },
                            ],

                            Attachment => [
                                {
                                    Content            => "xxxx",     # actual attachment contents, base64 enconded
                                    ContentAlternative => "",
                                    ContentID          => "",
                                    ContentType        => "application/pdf",
                                    FileID             => 34,
                                    Filename           => "StdAttachment-Test1.pdf",
                                    FilesizeRaw        => 4722,
                                },
                                {
                                   # . . .
                                },
                            ]
                        },
                        {
                            #. . .
                        },
                    ],
                },
                {
                    #. . .
                },
            ]
        },
    };

Many thanks again!
RStraub
Znuny guru
Posts: 2210
Joined: 13 Mar 2014, 09:16
Znuny Version: 6.0.14
Real Name: Rolf Straub

Re: how can get ticket data with generic agent?

Post by RStraub »

Hey,

try this module:

Code: Select all

package Modulos::GetTicket;

use strict;
use warnings;
use utf8;
use lib '/opt/otrs/';
use lib '/opt/otrs/Kernel/cpan-lib';
use lib '/opt/otrs/Custom';

use Kernel::System::ObjectManager;
local $Kernel::OM = Kernel::System::ObjectManager->new(
    'Kernel::System::Log' => {
        LogPrefix => "$0",
    },
);


sub new {
    my ( $Type, %Param ) = @_;

    # allocate new hash for object
    my $Self = {%Param};
    bless( $Self, $Type );

    return $Self;
}
   
sub Run {
    my ( $Self, %Param ) = @_;

    # get needed objects
    my $TicketObject = $Kernel::OM->Get('Kernel::System::Ticket');

    my %Ticket = $TicketObject->TicketGet(
        TicketID => $Param{TicketID},
        DynamicFields => 1,
    );
		
    use Data::Dumper;
    my $message = Dumper(\%Ticket);
    $Kernel::OM->Get('Kernel::System::Log')->Log(
        Priority => 'error',
        Message  => $message,
    );
    return 1;

}
1
And follow either of this logs:

Code: Select all

tail -f ~otrs/var/log/otrs.log.$NEWESTDATE
tail -f /var/log/apache2/error.log
Currently using: OTRS 6.0.14 -- MariaDB -- Ubuntu 16 LTS
Post Reply