-1

I am taking the recipient's ID and trying to send back a URL button but I get a "bad request" error.

I think the problem is the JSON but I can't figure it out.

sub post_url_button_to_facebook {
    my $reply_recipient = shift;

   my %hash = ('recipient'=>{'id'=>$reply_recipient},'message'=>{'attachment'=>{'type'=>'template','payload'=>{'template_type'=>'button','text'=>"This my link",'buttons'=>{'type'=>'web_url','url'=>'https://www.arx.gr/','title'=>"Dev's Website"} }}});

    my $post_json_data = JSON::encode_json( \%hash );

    my $ua = LWP::UserAgent->new;

    my $url = "https://graph.facebook.com/v2.9/me/messages?access_token=" . $permanent_token;

    my $req = HTTP::Request->new( POST => $url );
    $req->header( 'Content-type' => 'application/json' );
    $req->content( $post_json_data );

    my $resp = $ua->request( $req );

    if ( $resp->is_success ) {

        my $message = $resp->decoded_content;
        send_status_ok();

        warn "Received reply: $message\n";
    }
    else {
        warn "HTTP POST error code: ",    $resp->code,    "\n";
        warn "HTTP POST error message: ", $resp->message, "\n";
    }
}
  • Is `$perlmanent_token` defined? Is it a global variable? It's not lexical in your sub. You can look at the full request by adding a `print $req->as_string` or `$req->dump` before you send it. Alternatively, you can load LWP::ConsoleLogger::Everywhere to inspect the whole transaction to see what's wrong. – simbabque May 02 '17 at 08:43
  • 1
    yeah it's defined. – Haris Tsantas May 02 '17 at 08:49
  • I don't see a `messages` edge from the `user` node. Did you mean `inbox`? – Borodin May 02 '17 at 12:50

1 Answers1

0

The problem was that i forgot to put [] in the buttons field.

    sub post_url_button_to_facebook {
        my $reply_recipient = shift;



      my %hash = ('recipient'=>{'id'=>$reply_recipient},'message'=>{'attachment'=>{'type'=>'template','payload'=>{'template_type'=>'button','text'=>"This my link",'buttons'=>[{'type'=>'web_url','url'=>'https://www.arx.gr/','title'=>"Dev's Website"}] }}});

    my $post_json_data = JSON::encode_json( \%hash );

    my $ua = LWP::UserAgent->new;

    my $url = "https://graph.facebook.com/v2.9/me/messages?access_token=" . $permanent_token;

    my $req = HTTP::Request->new( POST => $url );
    $req->header( 'Content-type' => 'application/json' );
    $req->content( $post_json_data );

    my $resp = $ua->request( $req );

    if ( $resp->is_success ) {

        my $message = $resp->decoded_content;
        send_status_ok();

        warn "Received reply: $message\n";
    }
    else {
        warn "HTTP POST error code: ",    $resp->code,    "\n";
        warn "HTTP POST error message: ", $resp->message, "\n";
    }
}