Merge Ticket as a note

Moderator: crythias

Locked
gilfavftconsult
Znuny newbie
Posts: 8
Joined: 15 Feb 2013, 15:33
Znuny Version: OTRS 4.0.8
Real Name: Gilmar Favarin
Company: FT Consultoria

Merge Ticket as a note

Post by gilfavftconsult »

Hi reneeb,

I think that I have the same problem, but the cause is the type of body content. If you send 2 e-mails identical with content type HTML it is working great, but if you send 2 e-mails identical with type plain/text it is not working, it never merge.

I am trying to solve this but I am not specialist in perl, see below my code, if you do the line (if ( $Article{Body} eq $Mail{Body} )) works I think this solve the problem:

Code: Select all

# --
# Kernel/System/PostMaster/Filter/MergeIdenticalTickets.pm - sub part of PostMaster.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::PostMaster::Filter::MergeIdenticalTickets;

use strict;
use warnings;

use List::Util qw(first);

our @ObjectDependencies = qw(
    Kernel::System::Ticket
    Kernel::System::Log
);

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

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

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

    return $Self;
}

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

    my $LogObject = $Kernel::OM->Get('Kernel::System::Log');

    # check needed stuff
    for my $Needed (qw(JobConfig GetParam)) {
        if ( !$Param{$Needed} ) {
            $LogObject->Log(
                Priority => 'error',
                Message => "Need $Needed!",
            );
            return;
        }
    }

    # get config options
    my %Config;
    my %Metrics;

    if ( $Param{JobConfig} && ref $Param{JobConfig} eq 'HASH' ) {
        %Config  = %{ $Param{JobConfig} };
        %Metrics = %{ $Param{JobConfig}->{Metric} || {} };
    }

    return 1 if !%Config;
    return 1 if !%Metrics;

    my %Mail = %{ $Param{GetParam} };

    my %SearchCriteria = ( StateType => 'Open' );
    if ( $Metrics{From} ) {
        $SearchCriteria{From} = $Mail{From}
    }

    if ( $Metrics{Subject} ) {
        $SearchCriteria{Subject} = $Mail{Subject}
    }

    if ( $Metrics{Body} && !$Metrics{HTMLBody} ) {
        $SearchCriteria{Body} = $Mail{Body}
    }

    my $TicketObject = $Kernel::OM->Get('Kernel::System::Ticket');
    my @TicketIDs    = $TicketObject->TicketSearch(
        %SearchCriteria,
        Result => 'ARRAY',
        UserID => 1,
    );

    return 1 if !@TicketIDs;

    my ($TicketID) = first { $_ ne $Param{TicketID} } reverse @TicketIDs;
    my ($HTMLFile) = first{ $_->{Filename} eq 'file-2' }@{ $Mail{Attachment} || [] };

    if ( $Metrics{HTMLBody} && $HTMLFile ) {
        my $Found = 0;

        POSSIBLETICKET:
        for my $PossibleTicket ( reverse @TicketIDs ) {
            next POSSIBLETICKET if $PossibleTicket eq $Param{TicketID};

            my %Article = $TicketObject->ArticleFirstArticle(
                TicketID => $PossibleTicket,
                UserID   => 1,
            );

            my %AttachmentIndex = $TicketObject->ArticleAttachmentIndex(
                ArticleID => $Article{ArticleID},
                UserID    => 1,
            );

            my ($FileID) = first { $AttachmentIndex{$_}->{Filename} eq 'file-2' }keys %AttachmentIndex;
            my %File     = $TicketObject->ArticleAttachment(
                FileID    => $FileID,
                ArticleID => $Article{ArticleID},
                UserID    => 1,
            );

            if ( $File{Content} eq $HTMLFile->{Content} ) {
                $Found++;
                $TicketID = $PossibleTicket;
                last POSSIBLETICKET;
            }
        }

        $TicketID = undef if !$Found;
    }
    else {
        my $Found = 0;

        POSSIBLETICKET:
        for my $PossibleTicket ( reverse @TicketIDs ) {
            next POSSIBLETICKET if $PossibleTicket eq $Param{TicketID};

            my %Article = $TicketObject->ArticleFirstArticle(
                TicketID => $PossibleTicket,
                UserID   => 1,
            );

            if ( $Article{Body} eq $Mail{Body} ) {
                $Found++;
                $TicketID = $PossibleTicket;
                last POSSIBLETICKET;
            }

        }

        $TicketID = undef if !$Found;

    }

    if ( $TicketID ) {
        $TicketObject->TicketMerge(
            MainTicketID  => $TicketID,
            MergeTicketID => $Param{TicketID},
            UserID        => 1,
        );

    }

    return 1;
}

1;
P.S. Sorry for newbee code, but I need this ASAP and I tryed with all my power hehe...
Last edited by crythias on 19 Aug 2015, 13:31, edited 1 time in total.
Reason: Topic split because it was hijacking someone else's thread
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: Merge Ticket as a note

Post by reneeb »

can you send me a sample email that causes problems? I've checked with a simple text email and it works as expected...
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
gilfavftconsult
Znuny newbie
Posts: 8
Joined: 15 Feb 2013, 15:33
Znuny Version: OTRS 4.0.8
Real Name: Gilmar Favarin
Company: FT Consultoria

Re: Merge Ticket as a note

Post by gilfavftconsult »

I my case if I active plain text mode in gmail like in the attached image, and send 2 emails with the same subject but different body, this emails will be merged by MergeIdenticalTickets.
You do not have the required permissions to view the files attached to this post.
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: Merge Ticket as a note

Post by reneeb »

Can you send two mails to your own OTRS instance, activate PlainView in SysConfig, open the ticket(s) and download the mails in the ticket zoom via "unformatted view"? I do not use Gmail and I can't reproduce the error without some sample mails as I don't know what gmail is sending...
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
gilfavftconsult
Znuny newbie
Posts: 8
Joined: 15 Feb 2013, 15:33
Znuny Version: OTRS 4.0.8
Real Name: Gilmar Favarin
Company: FT Consultoria

Re: Merge Ticket as a note

Post by gilfavftconsult »

You do not need to use gmail, in all of testes I used a lots of email senders, clients and webmails of different servers like hotmail, gmail, yahoo, etc

You can reproduce sending from any server domain, but you need to set the type to "plain/text"

If this way works fine for you, could be some configuration of my instance.
gilfavftconsult
Znuny newbie
Posts: 8
Joined: 15 Feb 2013, 15:33
Znuny Version: OTRS 4.0.8
Real Name: Gilmar Favarin
Company: FT Consultoria

Re: Merge Ticket as a note

Post by gilfavftconsult »

reneeb can you help me with this?
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: Merge Ticket as a note

Post by reneeb »

As I already said, I tested it with simple plain text emails and it worked. It would be great to get sample emails...

What's your configuration? Do the mails have a html part? ...
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
gilfavftconsult
Znuny newbie
Posts: 8
Joined: 15 Feb 2013, 15:33
Znuny Version: OTRS 4.0.8
Real Name: Gilmar Favarin
Company: FT Consultoria

Re: Merge Ticket as a note

Post by gilfavftconsult »

With actual configuration, text/plain mails don't have html part. There is a setting for this? To force all mails received to be a html even though a original text/plain mail?
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: Merge Ticket as a note

Post by reneeb »

As long as I can't review your configuration and some sample mails it is not possible to help. As I already said, I tested it with plain text mails (without html part) and it worked...
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
gilfavftconsult
Znuny newbie
Posts: 8
Joined: 15 Feb 2013, 15:33
Znuny Version: OTRS 4.0.8
Real Name: Gilmar Favarin
Company: FT Consultoria

Re: Merge Ticket as a note

Post by gilfavftconsult »

Sorry, is attached the exported configuration
gilfavftconsult
Znuny newbie
Posts: 8
Joined: 15 Feb 2013, 15:33
Znuny Version: OTRS 4.0.8
Real Name: Gilmar Favarin
Company: FT Consultoria

Re: Merge Ticket as a note

Post by gilfavftconsult »

Is attached the examples that was merged by the addon with the same subject but different body and is attached my sysconfig.
You do not have the required permissions to view the files attached to this post.
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: Merge Ticket as a note

Post by reneeb »

I can't find anything about the addon in the sysconfig options. And in standard configuration the tickets are merged when the subject, the body and the sender are identical. But in your samples, the body is different. So there is no reason to merge the tickets.
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
gilfavftconsult
Znuny newbie
Posts: 8
Joined: 15 Feb 2013, 15:33
Znuny Version: OTRS 4.0.8
Real Name: Gilmar Favarin
Company: FT Consultoria

Re: Merge Ticket as a note

Post by gilfavftconsult »

But the addon MergeIdenticalTickets 4.0.2 is installed, is attached the print of sysconfig. Do you want my ZZZAuto and ZZZAAuto files?

I agree with you that has no reason to merge the tickets, but is merging, debugging the problem I have found that these text/plain emails are saved without the file-2 to be compared later in the code line before:

Code: Select all

...
    my ($HTMLFile) = first{ $_->{Filename} eq 'file-2' }@{ $Mail{Attachment} || [] };

    if ( $Metrics{HTMLBody} && $HTMLFile ) {
...
Without this file-2 the list of tickets searched with only sender and subject before this code lines, is true for this last lines:

Code: Select all

    if ( $TicketID ) {
        $TicketObject->TicketMerge(
            MainTicketID  => $TicketID,
            MergeTicketID => $Param{TicketID},
            UserID        => 1,
        );
    }
You do not have the required permissions to view the files attached to this post.
Locked