雑談APIを導入しよう!

docomoが提供する雑談対話APIとは

雑談対話APIは、ユーザの発話テキストを受け付け、その入力に対して自然な会話となる雑談を提供します。

データの流れ

https://line.f-logic.jp/img/BOT_docomo.jpg

導入の流れ

  1. docomoAPI利用申請
    雑談対話API申請
    API key取得
  2. callback.phpを編集する
    1. docomoAPIへユーザーメッセージを渡す
    2. docomoAPIからレスポンス(雑談の返答)を受け取る
    3. 2をLINE APIに渡す

コードにいれる

  1. 以下のコードを使う
  2. コードの解説(ざっくりどの部分でなにをしてるか)
  3. 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;
}