Process OTRS queue via external script

Moderator: crythias

Locked
pwaring
Znuny newbie
Posts: 9
Joined: 02 Apr 2014, 13:45
Znuny Version: 3.2.10
Contact:

Process OTRS queue via external script

Post by pwaring »

I have an existing OTRS installation and want to run a script (via cron) to email all new/open tickets from a given queue to an email address and then mark them as resolved (this is the simplest way I can think of to transfer tickets from OTRS to another request management system).

However, I'm struggling to find any information about this in the documentation. I'm fairly new to OTRS, and I wasn't the person who installed/configured the system, so it may be that I've overlooked something which would otherwise be 'obvious'.

What I want the script to do is:

Grab a bunch of tickets with specific characteristics, similar to this method in Kernel/Config/GenericAgent.pm

Code: Select all

'forward' => {n
  Queue => 'Forward'
  States => ['new', 'open'],
  Locks => ['unlock'],
  TicketCreateTimeOtherMinutes => 30
}
Perform the following actions on each of the tickets:
  • Send the ticket subject and text to another email address (I know how to do this in Perl once I've got the ticket data).
  • Mark the ticket as closed/successful.
Is it even possible to do this in OTRS? I have thought about creating a new module and placing it under Kernel/System/GenericAgent, but I'd prefer to run a script as that's much easier to test and has the advantage of keeping the code completely separate from the OTRS tree (for updating).
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Process OTRS queue via external script

Post by reneeb »

you could create a module like

Code: Select all

# --
# Kernel/System/GenericAgent/SendTicketToOtherSystem.pm
# Copyright (C) 2014 Perl-Services.de, http://perl-services.de
# --
# 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::GenericAgent::SendTicketToOtherSystem;

use strict;
use warnings;

use Kernel::System::Email;

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

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

    # check needed objects
    for (qw(DBObject ConfigObject LogObject MainObject EncodeObject TicketObject TimeObject)) {
        $Self->{$_} = $Param{$_} || die "Got no $_!";
    }

    $Self->{EmailObject} = Kernel::System::Email->new( %Param );

    # 0=off; 1=on;
    $Self->{Debug} = $Param{Debug} || 0;

    return $Self;
}

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

    # get ticket data
    my %Ticket = $Self->{TicketObject}->ArticleFirstArticle(
        %Param,
        DynamicFields => 0,
    );

    $Self->{EmailObject}->Send(
        From => 'your@otrs.local',
        To     => 'other.system@anything.example',
        Subject => $Ticket{Title},
        Charset => $Ticket{Charset},
        MimeType => $Ticket{MimeType},
        Body     => $Ticket{Body},
    );

    $Self->{TicketObject}->TicketTicketSet(
        TicketID   => $Param{TicketID},
        State => 'closed successful',
        UserID     => 1,
    );

    return 1;
}

1;
In the GenericAgent administration you can select the ticket filter based on your criteria and set this module as "user defined module"

Note: this module is untested
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
pwaring
Znuny newbie
Posts: 9
Joined: 02 Apr 2014, 13:45
Znuny Version: 3.2.10
Contact:

Re: Process OTRS queue via external script

Post by pwaring »

I don't particularly want to use a module, as that's harder to test iteratively - with a separate script I can run it again and again until I'm happy it does what I want, plus I can keep it separate from the OTRS source.

I have figured out roughly how to do this now though.
Locked