Dialogflow v1とv2のInline Editorを使った会話サンプル


Dialogflowがv1 -> v2にバージョンアップしましたね。それにともない、FulfillmentのInline Editorの書き方が結構変わりました。ということで、v1とv2の会話サンプルを比較しつつまとめてみました。

v1とv2のInline Editorを使った会話サンプル

v1
'use strict';

process.env.DEBUG = 'actions-on-google:*';
const functions = require('firebase-functions');
const { DialogflowApp } = require('actions-on-google');

const actionMap = new Map();

// ユーザからの入力パラメータを利用しない場合
// 'input.welcome' -> IntentsのAction
actionMap.set('input.welcome', app => {
  // 会話を継続させる場合
  app.ask('How are you?');
  // 会話を終了させる場合
  app.tell('Thanks for talking to me!');
});

// ユーザからの入力パラメータを利用する場合
// 'input.make_name' -> IntentsのAction
actionMap.set('input.make_name', app => {
  // color, num -> Intentsのparameters
  const color = app.getArgument('color');
  const num = app.getArgument('num');

  // 会話を継続させる場合
  app.ask('Alright, your silly name is ' +
    color + ' ' + num +
    '! Are there any other numbers and colors you like?');
  // 会話を終了させる場合
  app.tell('Alright, your silly name is ' +
    color + ' ' + num +
    '! I hope you like it. See you next time.');
});

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((req, res) => {
  const app = new DialogflowApp({ request: req, response: res });
  app.handleRequest(actionMap);
});
v2
'use strict';

process.env.DEBUG = 'actions-on-google:*';
const functions = require('firebase-functions');
const { dialogflow } = require('actions-on-google');

const app = dialogflow();

// ユーザからの入力パラメータを利用しない場合
// 'Default Welcome Intent' -> Intents
app.intent('Default Welcome Intent', conv => {
  // 会話を継続させる場合
  conv.ask('How are you?');
  // 会話を終了させる場合
  conv.close('Thanks for talking to me!');
});

// ユーザからの入力パラメータを利用する場合
// 'make_name' -> Intents
// color, num  -> Intentsのparameters
app.intent('make_name', (conv, { color, num }) => {
  // 会話を継続させる場合
  conv.ask(`Alright, your silly name is ${color} ${num} ! Are there any other numbers and colors you like?`);
  // 会話を終了させる場合
  conv.close(`Alright, your silly name is ${color} ${num} ! I hope you like it. See you next time.`);
});

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

まとめ

  • v1よりv2の方がコードがすっきりしている
  • 会話の単位
    • v1:アクション単位
    • v2:インテント単位

参考リンク