雑談APIを導入しよう!
docomoが提供する雑談対話APIとは
雑談対話APIは、ユーザの発話テキストを受け付け、その入力に対して自然な会話となる雑談を提供します。
データの流れ
導入の流れ
- docomoAPI利用申請
雑談対話API申請
API key取得 - callback.phpを編集する
- docomoAPIへユーザーメッセージを渡す
- docomoAPIからレスポンス(雑談の返答)を受け取る
- 2をLINE APIに渡す
コードにいれる
- 以下のコードを使う
- コードの解説(ざっくりどの部分でなにをしてるか)
- GETした返答をユーザに返したい。いままでのコードでどこで返してた?
/*docomoAPIからデータ取得する*/ function chat($text) { // docomo chatAPI $api_key = 'xxxxxxxxxxxxxxxx'; $api_url = sprintf('https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?APIKEY=%s', $api_key); $req_body = array( 'utt' => $text, 't'=> 20, ); $headers = array( 'Content-Type: application/json; charset=UTF-8', ); $options = array( 'http'=>array( 'method' => 'POST', 'header' => implode("\r\n", $headers), 'content' => json_encode($req_body), ) ); $stream = stream_context_create($options); $res = json_decode(file_get_contents($api_url, false, $stream)); return $res; }