Facebook Messenger平台发送URL按钮给出“错误请求”错误

时间:2017-05-02 08:35:01

标签: json perl facebook-messenger

我正在接收收件人的ID并尝试发回一个URL按钮但我收到“错误请求”错误。

我认为问题是JSON,但我无法理解。

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";
    }
}

1 个答案:

答案 0 :(得分:0)

问题是我忘了把[]放在按钮字段中。

    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";
    }
}