ボットからPUSHメッセージを行いたいのですが、メッセージを送信することができません。
必要なトークンやIDは間違い無さそうです。
何か原因になる処理はあるのでしょうか?
エラー内容
curlで送信した結果を出力すると次の様な返答がありました。
request_bodyが読めないというエラーの様です。
string(462) "HTTP/1.1 400 Bad Request Server: nginx Date: Sat, 04 May 2019 09:59:06 GMT Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive x-line-request-id: *** x-content-type-options: nosniff x-xss-protection: 1; mode=block cache-control: no-cache, no-store, max-age=0, must-revalidate pragma: no-cache expires: 0 x-frame-options: DENY {"message":"Could not read the request body"}"
ソース
php
1<?php 2 3 require_once "vendor/autoload.php"; 4 5 const LINEACCESSTOKEN = 'LINEのアクセストークン'; 6 const LINESECRETTOKEN = 'LINEのシークレットトークン'; 7 8 const SINGLEMESSAGEURL = 'https://api.line.me/v2/bot/message/push'; 9 const MULTIMESSAGEURL = 'https://api.line.me/v2/bot/message/multicast'; 10 const SAMPLEUSERID = '送信するユーザーID'; 11 12 // コール 13 // ポスト値を取得 14 $postTo = SAMPLEUSERID; 15 $message = "test"; 16 17 pushLineMessage($message, $postTo); 18 19 // LINEでメッセージを送信する 20 function pushLineMessage($pushMessage, $to) { 21 $headers = [ 22 'Content-Type: application/json; charset=utf-8', 23 'Authorization: Bearer ' . LINEACCESSTOKEN 24 ]; 25 26 $messages = [ 27 'type' => 'text', 28 'text' => $pushMessage 29 ]; 30 31 $body = [ 32 [ 33 'to' => $to, 34 'messages' => [ 35 $messages 36 ] 37 ] 38 ]; 39 $body = json_encode($body); 40 41 // メッセージ送信 42 $curl = curl_init(SINGLEMESSAGEURL); 43 curl_setopt_array($curl, [ 44 CURLOPT_CUSTOMREQUEST => 'POST', 45 CURLOPT_HTTPHEADER => $headers, 46 CURLOPT_RETURNTRANSFER => true, 47 CURLOPT_BINARYTRANSFER => true, 48 CURLOPT_HEADER => true, 49 CURLOPT_POSTFIELDS => $body 50 ]); 51 $rowData = curl_exec($curl); 52 curl_close($curl); 53 54 // 前項で記載したエラーが表示されます 55 var_dump($rowData); 56 57 } 58 59 ?>
LINEが提供しているライブラリを使った場合
下記のライブラリを使って検証してみましたが、同じ様なエラーで動きませんでした。
おそらく、根本的な部分に問題があるのだとは思います。
https://github.com/line/line-bot-sdk-php
エラー内容
object(LINE\LINEBot\Response)#6 (3) { ["httpStatus":"LINE\LINEBot\Response":private]=> int(415) ["body":"LINE\LINEBot\Response":private]=> string(56) "{"message":"The content type, 'null', is not supported"}" ["headers":"LINE\LINEBot\Response":private]=> array(13) { ["Server"]=> string(5) "nginx" ["Date"]=> string(29) "Sat, 04 May 2019 11:09:23 GMT" ["Content-Type"]=> string(30) "application/json;charset=UTF-8" ["Transfer-Encoding"]=> string(7) "chunked" ["Connection"]=> string(10) "keep-alive" ["x-line-request-id"]=> string(36) "***" ["x-content-type-options"]=> string(7) "nosniff" ["x-xss-protection"]=> string(13) "1; mode=block" ["cache-control"]=> string(46) "no-cache, no-store, max-age=0, must-revalidate" ["pragma"]=> string(8) "no-cache" ["expires"]=> string(1) "0" ["x-frame-options"]=> string(4) "DENY" ["accept"]=> string(16) "application/json" } }
ソース
php
1<?php 2 3 require_once "vendor/autoload.php"; 4 5 const LINEACCESSTOKEN = 'LINEのアクセストークン'; 6 const LINESECRETTOKEN = 'LINEのシークレットトークン'; 7 const SAMPLEUSERID = '送信するユーザーID'; 8 9 // コール 10 // ポスト値を取得 11 $postTo = SAMPLEUSERID; 12 $message = "test"; 13 14 pushLineMessage($message, $postTo); 15 16 // LINEでメッセージを送信する 17 function pushLineMessage($pushMessage, $to) { 18 19 // botを作成 20 $httpClient = new \LINE\LINEBot\HTTPClient\CurlHTTPClient(LINEACCESSTOKEN); 21 $bot = new \LINE\LINEBot($httpClient, [ 22 'channelSecret' => LINESECRETTOKEN 23 ]); 24 25 // メッセージを作成 26 $textMessageBuilder = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder($pushMessage); 27 28 // メール送信 29 $response = $bot->pushMessage($to, $textMessageBuilder); 30 31 var_dump($response); 32 } 33 34 ?>
ターミナルから直接送信する
プログラムではなく、シェルで直接呼び出せるか確認しましたが、結果送信できませんでした。
$ curl -v -X POST https://api.line.me/v2/bot/message/push \ -H 'Content-Type:application/json' \ -H 'Authorization: Bearer <チャンネルのアクセストークン>' \ -d '{ "to": "<送り先のユーザーID>", "messages":{ { "type": "text", "text": "Hello, world1" }, { "type":"text", "text":"Hello, world2" } } }'
エラー内容
400版エラーで、Could not read the request body
が返ってきており、
エラー文からは、本文が読み込めない形式になっているといった内容になっています。
< HTTP/1.1 400 Bad Request < Server: nginx < Date: Sun, 05 May 2019 08:03:56 GMT < Content-Type: application/json;charset=UTF-8 < Transfer-Encoding: chunked < Connection: keep-alive < x-line-request-id: xxx < x-content-type-options: nosniff < x-xss-protection: 1; mode=block < cache-control: no-cache, no-store, max-age=0, must-revalidate < pragma: no-cache < expires: 0 < x-frame-options: DENY < * Connection #0 to host api.line.me left intact {"message":"Could not read the request body"}

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/08 08:49