将快速路线与Google Assistant意图匹配

时间:2018-08-04 16:44:49

标签: node.js express actions-on-google dialogflow

我有一个要与Google Assistant集成的Express应用。

我已经安装了https://www.npmjs.com/package/actions-on-google,并遵循了https://codelabs.developers.google.com/codelabs/actions-1/#0的用法,该功能将功能部署到Firebase-但是我想从自托管的Express服务器上运行它们。

在我的app.js中,我进行了如下设置:

const {
  dialogflow,
  Image,
} = require('actions-on-google')

// Create an app instance

const gapp = dialogflow();
});

但是我不确定如何创建在Dialogflow控制台中添加为Webhook的路由-是否使用以下格式?

app.post('/webhook', function(req, res){
    gapp.intent('favorite color', (conv, {color}) => {
        const luckyNumber = color.length;
        // Respond with the user's lucky number and end the conversation.
        conv.close('Your lucky number is ' + luckyNumber);
    });
});

如果是的话,我所有的意图都在这条路线之内吗?

编辑 更新为回答:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

const {
  dialogflow,
  Image,
} = require('actions-on-google')

// Create a  google app instance

const gapp = dialogflow()

// Register handlers for Dialogflow intents

gapp.intent('Default Welcome Intent', conv => {
  conv.ask('Hi, how is it going?')
  conv.ask(`Here's a picture of a cat`)
  conv.ask(new Image({
    url: 'https://developers.google.com/web/fundamentals/accessibility/semantics-builtin/imgs/160204193356-01-cat-500.jpg',
    alt: 'A cat',
  }))
})

// Intent in Dialogflow called `Goodbye`
gapp.intent('Goodbye', conv => {
  conv.close('See you later!')
})

gapp.intent('Default Fallback Intent', conv => {
  conv.ask(`I didn't understand. Can you tell me something else?`)
})

app.post('/ga/webhook', gapp)

1 个答案:

答案 0 :(得分:4)

您可以在Express服务器的开始定义所有gapp意图,然后可以将gapp对象传递到您定义的Webhook中:

const express = require('express')
const bodyParser = require('body-parser')

// ... gapp code here

const expressApp = express().use(bodyParser.json())
expressApp.post('/webhook', gapp)
expressApp.listen(3000)