OTRS downloading files from external url

Moderator: crythias

Post Reply
alipioluiz
Znuny newbie
Posts: 8
Joined: 26 Mar 2019, 16:15
Znuny Version: 6.0.17

OTRS downloading files from external url

Post by alipioluiz »

Hi there,
I use limesurvey and OTRS. I need that OTRS make download of uploaded file from limesurvey and attach that in a new article of a ticket.

For example:
1) customer fills a form on limesurvey;
2) at the end, limesurvey send an email to OTRS and it opens a ticket through postmaster filter, mapping some dynamic fields like formID and responseID;
3) based on some event like TicketCreate, OTRS calls an external url (http://www.limesurveydomain.com/downloa ... responseID>) and download the file;
4) the file downloaded is attach in a new article of the ticket.

Is there a way to do the items 3 and 4?
Perhaps using web services. But how?
May you help me?

Thanks in advance.
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: OTRS downloading files from external url

Post by reneeb »

Yes, that's possible. You could write your own TicketEvent module. An example is available at: https://gist.github.com/reneeb/9385477

You have to replace the lines 55 - 67. There you have to do the request and create a new article

* https://otrs.perl-services.de/otrs/rel- ... t::Article
* https://otrs.perl-services.de/otrs/rel- ... 3AMIMEBase
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
alipioluiz
Znuny newbie
Posts: 8
Joined: 26 Mar 2019, 16:15
Znuny Version: 6.0.17

Re: OTRS downloading files from external url

Post by alipioluiz »

Thanks for response, reneeb.

I've made a custom module as you suggested. However, I didn't use a xml file. Instead of it I used a generic agent to run the module.

When the generic agent runs the module, the article is created with the wrong content type of the file. The original file is .zip, but otrs attachs as ASCII text.

Code: Select all

package Kernel::System::Ticket::Event::LimesurveyGetFile;

#use Data::Dumper;
use strict;
use warnings;
use Kernel::System::User;
use LWP::Simple qw($ua getstore);
use POSIX qw(WNOHANG);
use MIME::Base64 qw(encode_base64);


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

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

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

    return $Self;
}

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

    # check needed stuff
    for (qw(TicketID)) {
        if ( !$Param{$_} ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => "Need $_!"
            );
            return;
        }
    }

        my $SURVEYID = URI->new( "$Param{New}->{SURVEYID}" );
        my $sURL = "http://www.limesurveydomain.com/download_uploaded_files.php&surveyid=$Param{New}->{SURVEYID}&srid=44";
        my $sSaveFile = "/opt/otrs/var/tmp/File.zip";
        my $content;

        getstore($sURL,$sSaveFile);

        open (ZIP, $sSaveFile) or die "$!";

        my $raw_string = do{ local $/ = undef; <ZIP>; };
        $content = encode_base64( $raw_string );

        unlink $sSaveFile;


        $Kernel::OM->Get('Kernel::System::Ticket::Article')->BackendForChannel(ChannelName => 'Email')->ArticleCreate(
            TicketID             => $Param{TicketID},
            SenderType           => 'system',                             # (required) agent|system|customer
            IsVisibleForCustomer => 0,                                   # (required) Is article visible for customer?

            From             => 'Solicitante',       # not required but useful
            Subject          => 'Arquivos Anexados',               # required
            Body             => 'Arquivos anexados pelo solicitante.',                     # required
            Charset          => 'ISO-8859-1',
            ContentType      => 'text/plain',
            HistoryType      => 'AddNote',                          # EmailCustomer|Move|AddNote|PriorityUpdate|WebRequestCustomer|...
            HistoryComment   => 'Arquivos anexados',
            UserID           => 2,
            Attachment => [
                {
                    Content     => $content,
                    ContentType => 'application/zip',
                    Filename    => 'Arquivos.zip',
                },
            ],
        );

        return 0;
}

1;
When I verify the file content type after download it I get:

Code: Select all

$ file Arquivos.zip 
Arquivos.zip: ASCII text
What's wrong?
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: OTRS downloading files from external url

Post by reneeb »

Why did you [c]encode_base64()[/c]?
For reading files, you should use FileRead from https://otrs.perl-services.de/otrs/rel- ... %3A%3AMain .

(untested)

old:

Code: Select all

        open (ZIP, $sSaveFile) or die "$!";

        my $raw_string = do{ local $/ = undef; <ZIP>; };
        $content = encode_base64( $raw_string );
new:

Code: Select all

my $ContentRef = $Kernel::OM->Get('Kernel::System::Main')->FileRead(
    Location => $sSaveFile,
    Mode     => 'binmode',
);
old:

Code: Select all

            Attachment => [
                {
                    Content     => $content,
                    ContentType => 'application/zip',
                    Filename    => 'Arquivos.zip',
                },
            ],
new:

Code: Select all

            Attachment => [
                {
                    Content     => ${$ContentRef},
                    ContentType => 'application/zip',
                    Filename    => 'Arquivos.zip',
                },
            ],
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
alipioluiz
Znuny newbie
Posts: 8
Joined: 26 Mar 2019, 16:15
Znuny Version: 6.0.17

Re: OTRS downloading files from external url

Post by alipioluiz »

Really nice, renneb. It worked. Thanks.

Only one more question.

How can I get a Dynamic Field from the ticket to use inside the custom module?

I need to replace the value of srid to a dynamic field 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: OTRS downloading files from external url

Post by reneeb »

Code: Select all

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

my $Srid = $Ticket{DynamicField_NameOfFieldThatStoresSrid};
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
alipioluiz
Znuny newbie
Posts: 8
Joined: 26 Mar 2019, 16:15
Znuny Version: 6.0.17

Re: OTRS downloading files from external url

Post by alipioluiz »

Everything worked. Thanks.
Post Reply