LoginSignup
0
2

More than 5 years have passed since last update.

api.aiのwebhookを受けるサーバでパラメータを受け取る

Posted at

api.aiではfulfillmentの設定で、webhookで呼び出すエンドポイントを指定できる。
Intent内の設定でユーザの入力に対応するパラメータを定義して、webhook時にリクエストボディに含めることができる。

api.aiからのリクエストを受けるサーバではどのようにしてパラメータを取得するかメモ。今回はデモアプリ用にherokuでnode.js/expressフレームワークのサーバを立てて検証した。

パラメータの抽出

req.body.result.parametersでパラメータオブジェクトを取得できる。
レスポンスはパラメータオブジェクトを文字列にして返すだけの例。

router.all('/', function(req, res, next) {
  console.log('[REQUEST]', req.body.result.parameters);
  res.set('Content-Type', 'application/json');
  res.json({
    displayText: JSON.stringify(req.body.result.parameters),
    speech: JSON.stringify(req.body.result.parameters),
  });
});

ひとくちメモ

  • herokuのログを見る方法 heroku logs -t
  • レスポンスボディのspeechに設定した値がwebデモなどで確認できるテキストになる
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2