[SOLVED] Export Tickets upon event

Moderator: crythias

Locked
Giulio Soleni
Znuny wizard
Posts: 392
Joined: 30 Dec 2010, 14:35
Znuny Version: 6.0.x and 5.0.x
Real Name: Giulio Soleni
Company: IKS srl

[SOLVED] Export Tickets upon event

Post by Giulio Soleni »

Hi,
I have read crythias' tutorial on Ticket Events http://forums.otrs.org/viewtopic.php?f=60&t=10090 and I found it very straightforward and useful. I think it can aid me to address my new problem:
I need to dump on a text file the content of each new ticket and any of its articles upon the ticket creation and then after any update.
I'm going to use this text file to feed another application.
Looking into /opt/otrs/bin/ I found otrs.GetTicketThread.pl and I adapt the code as for crythias tutorial.

1. I defined the following /opt/otrs/Kernel/Config/Files/GSOTest.xml

Code: Select all

<?xml version="1.0" encoding="utf-8" ?>
<otrs_config version="1.0" init="Changes">
    <ConfigItem Name="Ticket::EventModulePost###GSOTest" Required="0" Valid="1">
        <Description Lang="en"></Description>
        <Description Lang="de"></Description>
        <Group>GSOTest</Group>
        <SubGroup>EventModule</SubGroup>
        <Setting>
            <Hash>
                <Item Key="Module">Kernel::System::Ticket::Event::GSOTest</Item>
                <Item Key="Event">(ArticleAgentNotification|ArticleAutoResponse|ArticleBounce|ArticleCreate|ArticleCustomerNotification|ArticleFlagDelete|ArticleFlagSet|ArticleFreeTextUpdate|ArticleSend|ArticleUpdate|HistoryAdd|HistoryDelete|TicketAccountTime|TicketCreate|TicketCustomerUpdate|TicketDelete|TicketFreeTextUpdate|TicketFreeTimeUpdate|TicketLockUpdate|TicketMerge|TicketOwnerUpdate|TicketPendingTimeUpdate|TicketPriorityUpdate|TicketQueueUpdate|TicketResponsibleUpdate|TicketServiceUpdate|TicketSLAUpdate|TicketStateUpdate|TicketTitleUpdate|TicketTypeUpdate|TicketUnlockTimeoutUpdate)</Item>
            </Hash>
        </Setting>
    </ConfigItem>
</otrs_config>
2. I defined the following /opt/otrs/Kernel/System/Ticket/Event/GSOTest.pm

Code: Select all

# --
# Kernel/System/Ticket/Event/EventModulePostTemplate.pm - event module
# Copyright (C) 2001-2010 xxx, http://otrs.org/
# --
# $Id: EventModulePostTemplate.pm,v 1.1 2010/05/10 17:56:20 sb Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::System::Ticket::Event::GSOTest;

use strict;
use warnings;

use vars qw($VERSION);
$VERSION = qw($Revision: 1.1 $) [1];

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

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

    # get needed objects
    for (
        qw(
        ConfigObject CustomerGroupObject CustomerUserObject DBObject EncodeObject GroupObject
        HTMLUtilsObject LinkObject LockObject LogObject LoopProtectionObject MainObject
        PriorityObject QueueObject SendmailObject ServiceObject SLAObject StateObject
        TicketObject TimeObject TypeObject UserObject ValidObject
        )
        )
    {
        $Self->{$_} = $Param{$_} || die "Got no $_!";
    }

    return $Self;
}

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

    # check needed stuff
    for (qw(TicketID Event Config)) {
        if ( !$Param{$_} ) {
            $Self->{LogObject}->Log( Priority => 'error', Message => "Need $_!" );
            return;
        }
    }

#    return 1 if $Param{Event} ne 'TicketCreate';

    my %Ticket = $Self->{TicketObject}->TicketGet(
        TicketID => $Param{TicketID},
        UserID   => 1,
    );
    return 1 if !%Ticket;
#    return 1 if $Ticket{State} eq 'Open';

    # do some stuff
#    $Self->{TicketObject}->HistoryAdd(
#        TicketID     => $Param{TicketID},
#        CreateUserID => 1,
#        HistoryType  => 'Misc',
#        Name         => 'New ticket with state other than Open!',
#    );

my $OutFileName='/tmp/'.$Ticket{TicketNumber}.'_OTRS.log';

if (!open (WLOG,"> $OutFileName"))  {
return 1;
}

print WLOG "=====================================================================\n";
for (qw(TicketNumber TicketID Created Queue State Priority Lock CustomerID CustomerUserID)) {
    print WLOG "$_: $Ticket{$_}\n" if ( $Ticket{$_} );
}
print WLOG "---------------------------------------------------------------------\n";

my @Index = $Self->{TicketObject}->ArticleIndex( TicketID => $TicketID );
for (@Index) {
    my %Article = $Self->{TicketObject}->ArticleGet( ArticleID => $_ );
    for (qw(ArticleID From To Cc Subject ReplyTo InReplyTo Created SenderType)) {
        print WLOG "$_: $Article{$_}\n" if ( $Article{$_} );
    }
    print WLOG "Body:\n";
    print WLOG "$Article{Body}\n";
    print WLOG "---------------------------------------------------------------------\n";
}
close(WLOG);

    return 1;
}

1;
Now I would expect that as soon as I create a ticket or I update it, a new txt file should be written into /tmp/ ... but it seems not to work. :(

May you please help me...
Do you see something missing?
Am I completely far afield or there is some error on the code?

Thank you
Giulio

EDIT: ... found an error in my code: $TicketID must be substituted with $Param{TicketID}.

Code: Select all

...
my @Index = $Self->{TicketObject}->ArticleIndex( TicketID => $Param{TicketID} );
...
Still however it does not work.
Last edited by Giulio Soleni on 17 Jun 2011, 12:58, edited 1 time in total.
OTRS 6.0.x on CentOS 7.x with MariaDB 10.2.x database connected to an Active Directory for Agents and Customers.
ITSM and FAQ modules installed.
MichaelR
Znuny expert
Posts: 250
Joined: 12 Oct 2010, 01:35
Znuny Version: 3.0.9
Company: LRS Health

Re: Export Tickets upon event

Post by MichaelR »

Create some log events containing your variables during the execution and narrow it down as to where the problem is occurring. See if they are being set properly etc...

Is OTRS/Apache throwing any weird errors during the execution of the script atm?
OTRS: 3.0.9 & ITSM 3.0.4 - OS: Windows 7 - DB: MySQL - Heaps of random/useful hacks :)
[Major Code Changes]
ArticleFreeTime1-3
Ability to search ArticleFreeText
Giulio Soleni
Znuny wizard
Posts: 392
Joined: 30 Dec 2010, 14:35
Znuny Version: 6.0.x and 5.0.x
Real Name: Giulio Soleni
Company: IKS srl

Re: Export Tickets upon event

Post by Giulio Soleni »

I have almost solved for my purposes but I had to review part of the code taking
/opt/otrs/Kernel/System/Ticket/Event/Test.pm as a template.

This is my present pm file:

Code: Select all

# --
# Kernel/System/Ticket/Event/GSOTest.pm - event module
# Copyright (C) 2001-2010 xxx, http://otrs.org/
# --
# $Id: EventModulePostTemplate.pm,v 1.1 2010/05/10 17:56:20 sb Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::System::Ticket::Event::GSOTest;

use strict;
use warnings;

use vars qw($VERSION);
$VERSION = qw($Revision: 1.11 $) [1];

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

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

    # get needed objects
    for (qw(ConfigObject TicketObject LogObject UserObject CustomerUserObject SendmailObject)) {
        $Self->{$_} = $Param{$_} || die "Got no $_!";
    }

    return $Self;
}

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

    # check needed stuff
    for (qw(Data Event Config)) {
        if ( !$Param{$_} ) {
            $Self->{LogObject}->Log( Priority => 'error', Message => "Need $_!" );
            return;
        }
    }
    for (qw(TicketID)) {
        if ( !$Param{Data}->{$_} ) {
            $Self->{LogObject}->Log( Priority => 'error', Message => "Need $_ in Data!" );
            return;
        }
    }
    
    my $OutFileName;
	
    # Action on Ticket Creation	
    if ( $Param{Event} eq 'TicketCreate' ) {
        my %Ticket = $Self->{TicketObject}->TicketGet( TicketID => $Param{Data}->{TicketID} );
       
    	$OutFileName='/tmp/'.$Ticket{TicketNumber}.'_OTRS.log';
		if (!open (WLOG,"> $OutFileName"))  {
			$Self->{LogObject}->Log( Priority => 'error', Message => "CANNOT create $OutFileName!" );
			return;
		}

        print WLOG "=====================================================================\n";
        
		foreach my $TicketProperty (keys %Ticket) {
			print WLOG "$TicketProperty: $Ticket{$TicketProperty}\n";
		}        
#		for (qw(TicketNumber TicketID Created Queue State Priority Lock CustomerID CustomerUserID)) {
#    		print WLOG "$_: $Ticket{$_}\n" if ( $Ticket{$_} );
#		}
		print WLOG "---------------------------------------------------------------------\n";
		close(WLOG);
		
	}
	elsif ( $Param{Event} eq 'ArticleCreate' ) {
		
        my %Ticket = $Self->{TicketObject}->TicketGet( TicketID => $Param{Data}->{TicketID} );
       
    	$OutFileName='/tmp/'.$Ticket{TicketNumber}.'_OTRS.log';
		if (!open (WLOG,"> $OutFileName"))  {
			$Self->{LogObject}->Log( Priority => 'error', Message => "CANNOT create $OutFileName!" );
			return;
		}
		
		print WLOG  "$Param{Event} ON $Ticket{TicketNumber}\n";
		
		print WLOG "=====================================================================\n";
        
		foreach my $TicketProperty (keys %Ticket) {
			print WLOG "$TicketProperty: $Ticket{$TicketProperty}\n";
		}
		
		print WLOG "---------------------------------------------------------------------\n";        

		
		my @Index = $Self->{TicketObject}->ArticleIndex( TicketID => $Param{Data}->{TicketID} );
		for (@Index) {
			
   		my %Article = $Self->{TicketObject}->ArticleGet( ArticleID => $_ );
    		for (qw(ArticleID From To Cc Subject ReplyTo InReplyTo Created SenderType)) {
        		print WLOG "$_: $Article{$_}\n" if ( $Article{$_} );
    		}
    		print WLOG "Body:\n";
    		print WLOG "$Article{Body}\n";
    		print WLOG "---------------------------------------------------------------------\n";
		}		
		close(WLOG);
		
	}
	else  {
		
        my %Ticket = $Self->{TicketObject}->TicketGet( TicketID => $Param{Data}->{TicketID} );
       
    	$OutFileName='/tmp/'.$Ticket{TicketNumber}.'_OTRS.log';
		if (!open (WLOG,">> $OutFileName"))  {
			$Self->{LogObject}->Log( Priority => 'error', Message => "CANNOT create $OutFileName!" );
			return;
		}
		
		print WLOG  "$Param{Event} ON $Ticket{TicketNumber}\n";
		
		close(WLOG);
	}
    
    return 1; 
}

1;

Actually still a draft with several improvement to be done, but running with no errors.
OTRS 6.0.x on CentOS 7.x with MariaDB 10.2.x database connected to an Active Directory for Agents and Customers.
ITSM and FAQ modules installed.
Locked