Generic Agent - Execute Custom Module

Moderator: crythias

Post Reply
sbtech
Znuny newbie
Posts: 3
Joined: 05 Jun 2013, 16:55
Znuny Version: 3.2.1
Real Name: Erin O'Meara
Company: Salmon Bay Technology

Generic Agent - Execute Custom Module

Post by sbtech »

I have googled and looked thru doc.otrs.org till my fingers were sore, I can seem to find any basic examples of the Generic Agent -> Execute Custom Module.

My goal is that when I order hardware, I merge the order email from say NewEgg or other online stores to a ticket for a customer computer repair. Well after the initial order email, most sites send at least 2-3 more emails related to that order.

I would love to have a simple module that full text searches tickets for an order number and then merges the followup emails into the customer ticket with the order email.

Does this sound like something that a Custom Module for the Generic Agent would be appropriate? or is this the hard way?
Can the Execute Custom Module Params be variables like ticket number?
sbtech
Znuny newbie
Posts: 3
Joined: 05 Jun 2013, 16:55
Znuny Version: 3.2.1
Real Name: Erin O'Meara
Company: Salmon Bay Technology

Re: Generic Agent - Execute Custom Module

Post by sbtech »

Bump, Someone have a simple generic agent custom module that they can share with me?
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: Generic Agent - Execute Custom Module

Post by reneeb »

As you'd like to change the way an email is processed, you should write an Postmaster filter.

Code: Select all

# --
# Kernel/System/PostMaster/Filter/MergeOrderMails.pm - merge tickets based on order numbers in article body
# Copyright (C) 2013 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::PostMaster::Filter::MergeOrderMails;

use strict;

use vars qw($VERSION);
$VERSION = 0.01;

use Kernel::System::Ticket;

sub new {
    my $Type  = shift;
    my %Param = @_;

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

    $Self->{Debug} = $Param{Debug} || 0;

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

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

    return $Self;
}

sub Run {
    my ( $Self, %Param ) = @_;
    for my $Needed (qw(JobConfig GetParam TicketID)) {
        if ( !$Param{$Needed} ) {
            $Self->{LogObject}->Log(
                Priority => 'error',
                Message  => "Need $Needed!",
            );
            return;
        }
    }

    # get order number from new ticket
    # this is just an example as I don't know how the mails from the online shops look like
    # (if they put the order number in the subject or body, how the order number is formatted,...)
    my ($OrderNumber) = $GetParam{Body} =~ /Order (\d+)/;

    # search existing tickets for that order number.
    # What should happen if it finds more than one ticket?
    # here the first ticket is taken
    my ($TicketID) = $Self->{TicketObject}->TicketSearch(
        Subject => '%' . $OrderNumber . '%',    # assuming the order number is in the subject
    );

    # merge the tickets
    if ( $TicketID ) {
        $Self->{TicketObject}->TicketMerge(
            MainTicketID => $TicketID,
            MergeTicketID => $Param{TicketID},
            UserID => 1,
        );
    }

    return 1;
}

1;
Then you have to activate it in the Kernel/Config.pm

Code: Select all

$Self->{'PostMaster::PostFilterModule'}->{'0001-MergeOrderMails'} = { Module => 'Kernel::System::PostMaster::Filter::MergeOderMails' };
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
sbtech
Znuny newbie
Posts: 3
Joined: 05 Jun 2013, 16:55
Znuny Version: 3.2.1
Real Name: Erin O'Meara
Company: Salmon Bay Technology

Re: Generic Agent - Execute Custom Module

Post by sbtech »

reneeb - Thanks! this was the right direction, I now have a mod that merges order tickets.

not the prettiest code, but working great :)

Code: Select all

package Kernel::System::PostMaster::Filter::MergeOrders;

use strict;

use vars qw($VERSION);
$VERSION = 0.01;

use Kernel::System::Ticket;

sub new {
    my $Type  = shift;
    my %Param = @_;

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

    $Self->{Debug} = $Param{Debug} || 0;

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

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

    return $Self;
}

sub Run {
    my ( $Self, %Param ) = @_;
    for my $Needed (qw(JobConfig GetParam TicketID)) {
        if ( !$Param{$Needed} ) {
            $Self->{LogObject}->Log(
                Priority => 'error',
                Message  => "Need $Needed!",
            );
            return;
        }
    }
	
	
	my $I_Search = 0;
	my $S_Search = '';
	my $TicketID = 0;
	my $B_Debug = 1;
	
	if ($Param{GetParam}->{Subject} eq 'testing') {
	
		#Testing

	}
	
	# New Egg
	if ($Param{GetParam}->{From} =~ /\@newegg\.com/) {
	
		$Param{GetParam}->{Body} =~ /Sales Order Number:.*?(\d{9})/is;
		$S_Search = $1;
		
		if (length($S_Search) > 8) { $I_Search = 1; }
	}
	
	# Amazon.com
	if ($Param{GetParam}->{From} =~ /\@amazon\.com/) {
	
		$Param{GetParam}->{Body} =~ /(\d{3}\-\d{7}\-\d{7})/;
		$S_Search = $1;
		
		if (length($S_Search) == 19) { $I_Search = 1; }
	}
	
	# Ebay.com & Paypal auctions
	if ($Param{GetParam}->{From} =~ /(ebay\.com|paypal\.com)/) {
		$B_Debug = 1;
	
		$Param{GetParam}->{Body} =~ /item(| number)(:|\#) (\d{9,12})/i;
		$S_Search = $3;	
		if (length($S_Search) > 8) { $I_Search = 1; }
	}
	
	#// Body Search
	if ($I_Search == 1) {
	
		($TicketID) = $Self->{TicketObject}->TicketSearch(
				Result => 'ARRAY',
				UserID => 3,
				SortBy => 'Age',
				OrderBy => 'Up',
				Limit => 1,
			    Body => '%' . $S_Search . '%',    # assuming the order number is in the subject
			);
	}
		
	#// Merge ticket if we can
	if ($TicketID > 0 and $TicketID != $Param{TicketID}) {
			
		$Self->{TicketObject}->TicketMerge(
						MainTicketID => $TicketID,
						MergeTicketID => $Param{TicketID},
						UserID => 3,
						);
	}
	
	my $I_Time = time();
	#// Capture new emails
	if ($Param{GetParam}->{From} =~ /\@(newegg\.com|ebay\.com)/ or $B_Debug == 1) {
	
		open(OUT, '>>/tmp/' . $1 . '-' . $I_Time . '.txt');
		print OUT $Param{GetParam}->{Subject}  . "\n";
		print OUT $Param{GetParam}->{From} . "\n\n";
		print OUT $Param{GetParam}->{Body};
	
		close(OUT);
	}
	
	if ($B_Debug == 1) {
	
		open(OUT2, '>>/tmp/' . $I_Time . '-2.txt');
		print OUT2 $S_Search  . ':' . length($S_Search) . ':' . $I_Search . ':' . $Param{TicketID} . ':';
		
		use Data::Dumper;
		$Data::Dumper::Indent = 1;
		$Data::Dumper::Sortkeys = 1;
		print OUT2 Dumper($TicketID);
		
		close(OUT2);
	}
		
	return 1;
}

1;
IgorD
Znuny newbie
Posts: 14
Joined: 15 Dec 2016, 16:47
Znuny Version: 5s Free

Re: Generic Agent - Execute Custom Module

Post by IgorD »

Do you have experience of using "Generic Agent - Execute Custom Module". How to read parameters "Param 1 Key" and "Param 1 value"?
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: Generic Agent - Execute Custom Module

Post by reneeb »

That's what comes to the sub Run...

If you define Param1 key = Username and Param 1 value = IgorD and Param2 key = Date and Param2 value = 2016-12-23 22:45:00 then you can get it with $Param{Username} and $Param{Date} in the sub Run
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
IgorD
Znuny newbie
Posts: 14
Joined: 15 Dec 2016, 16:47
Znuny Version: 5s Free

Re: Generic Agent - Execute Custom Module

Post by IgorD »

I did as advised. The result - empty : Mon Dec 26 09:30:10 2016 info SendmailChief MySubj=

Image

Code: Select all

#!/usr/bin/perl -w

package Custom::SendmailChief;

use strict;
use warnings;

use vars qw($VERSION);
$VERSION = 0.01;

use Kernel::System::ObjectManager;

our $ObjectManagerDisabled = 1;

sub new {

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

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

    # debug level
    $Self->{Debug} = $Param{Debug} || 0;


    # get configured backend module
    my $GenericModule = $Kernel::OM->Get('Kernel::Config')->Get('SendmailModule')
        || 'Kernel::System::Email::Sendmail';

    # get backend object
    $Self->{Backend} = $Kernel::OM->Get($GenericModule);

    return $Self;
}

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

    # var
    my $EmailAddr = '';
    my $EmailObject = $Kernel::OM->Get('Kernel::System::Email');
    my $LogObject = $Kernel::OM->Get('Kernel::System::Log');
    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
    my $DBObject = $Kernel::OM->Get('Kernel::System::DB');

    my $vvv = $Param{MySubj};

    $LogObject->{LogPrefix} = 'SendmailChief';

    $LogObject->Log( Priority => 'info', Message  => 'MySubj=' . $vvv, );

}

1;
IgorD
Znuny newbie
Posts: 14
Joined: 15 Dec 2016, 16:47
Znuny Version: 5s Free

Re: Generic Agent - Execute Custom Module

Post by IgorD »

The must be "ParamKey" described in SysConfig or file .xml?
sansflance
Znuny newbie
Posts: 14
Joined: 18 Dec 2018, 16:38
Znuny Version: OTRS6.0
Real Name: Sam

Re: Generic Agent - Execute Custom Module

Post by sansflance »

How can we send the ticket id as a variable to custom module in GUI section. It allows to input only text.. What is the exact variable representation for the value of ticket id in below image.
.Image
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: Generic Agent - Execute Custom Module

Post by reneeb »

The ticket id is automatically passed to the Run subroutine...

Code: Select all

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

    print STDERR $Param{TicketID};
}
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
sansflance
Znuny newbie
Posts: 14
Joined: 18 Dec 2018, 16:38
Znuny Version: OTRS6.0
Real Name: Sam

Re: Generic Agent - Execute Custom Module

Post by sansflance »

Thankyou Reneeb
Post Reply