2017-05-02 35 views
-1

这是我在收件人的ID,并试图发送回一个网址按钮,但我得到一个“错误的请求”的错误。的Facebook Messenger平台发送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"; 
    } 
} 
+0

是'$ perlmanent_token'定义?它是一个全局变量吗?这不是你的语言中的词汇。您可以在发送之前添加一个'print $ req-> as_string'或'$ req-> dump'来查看完整的请求。或者,您可以加载LWP :: ConsoleLogger :: Everywhere来检查整个事务以查看错误。 – simbabque

+1

是的,它的定义。 –

+0

我没有看到'用户'节点的'messages'边缘。你是不是指“收件箱”? – Borodin

回答

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"; 
    } 
} 
相关问题