Perl: Issue with blessed object

587 views Asked by At

I am creating a bot that connects to a Matrix server. For that I use Net::Async::Matrix.

The code:

#!/usr/bin/perl
use strict;
use warnings;
use Net::Async::Matrix;
use Net::Async::Matrix::Utils qw ( parse_formatted_message );
use IO::Async::Loop;
use Data::Dumper;


my $loop = IO::Async::Loop->new;

my $matrix = Net::Async::Matrix->new(
server => 'matrix.server.net',
    on_error => sub {
            my ( undef, $message ) = @_;
            warn "error: $message\n";
    },
);
$loop->add( $matrix );

$matrix->login(
    user_id  => '@bot:matrix.server.net',
    password => 'password',
)->get;

my $room = $matrix->join_room( '#Lobby:matrix.server.net' )->get;

$room->configure(
    on_message => sub {
            my ( undef, $member, $content, $event ) = @_;
            my $msg = parse_formatted_message( $content );
            my $sendername = $member->displayname;
print Dumper $sendername;
            &sendmsg("$sendername said: $msg");
    },
);

my $stream = $matrix->start;

sub sendmsg {
        my $input = shift;
        if ($input) {
                $room->send_message(
                        type => "m.text",
                        body => $input,
                 ),
        }
}

$loop->run;

Basically, I want the bot to echo what was said.

I get following output:

$VAR1 = 'm1ndgames'; Longpoll failed - encountered object 'm1ndgames said: test', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing) at /usr/local/share/perl/5.24.1/Net/Async/Matrix.pm line 292.

and I don't understand it. When I enter a string like test into the body, it gets sent to the room.

2

There are 2 answers

0
ikegami On BEST ANSWER

parse_formatted_message returns a String::Tagged object. This class overloads concatenation so that "$sendername said: $msg" also returns a String::Tagged object. This object is passed to sendmsg which tries to serialize it into JSON, but it refuses to serialize objects.

Fix: Replace

my $msg = parse_formatted_message( $content );

with

my $msg = parse_formatted_message( $content )->str;
0
Sobrique On

I'd guess that this is a quoting error. If you look at Net::Async::Matrix::Room:

sub send_message
{
   my $self = shift;
   my %args = ( @_ == 1 ) ? ( type => "m.text", body => shift ) : @_;

   my $type = $args{msgtype} = delete $args{type} or
      croak "Require a 'type' field";

   $MSG_REQUIRED_FIELDS{$type} or
      croak "Unrecognised message type '$type'";

   foreach (@{ $MSG_REQUIRED_FIELDS{$type} } ) {
      $args{$_} or croak "'$type' messages require a '$_' field";
   }

   if( defined( my $txn_id = $args{txn_id} ) ) {
      $self->_do_PUT_json( "/send/m.room.message/$txn_id", \%args )
         ->then_done()
   }
   else {
      $self->_do_POST_json( "/send/m.room.message", \%args )
         ->then_done()
   }
}

The type you sent is handled by this sub, and then the actual message gets handed off to _do_POST_json in Net::Async::Matrix.

But you've sent a string containing a :.

So I think what's happening is it's encoding like this:

use JSON;
use Data::Dumper;

my $json = encode_json ( {body => "m1ndgames: said test"});
print Dumper  $json;

But the response that's coming back, at line 292 which is:

if( length $content and $content ne q("") ) {
         eval {
            $content = decode_json( $content );
            1;
         } or
            return Future->fail( "Unable to parse JSON response $content" );
         return Future->done( $content, $response );
      }

So I think is what is happening is the remote server is sending you a broken error code, and the module isn't handling it properly - it's expecting JSON but it isn't actually getting it.

My best guess would be - try dropping the : out of your message, because I would guess there's some bad quoting happening. But without seeing the code on the server side, I can't quite tell.