简体   繁体   English

如何通过代码在DialogFlow实现中创建意图?

[英]How to create intent in DialogFlow Fulfillment by code?

I've started learning DialogFlow, I want to know that how can I create an Intent in Fulfillment by code, instead of creating it by GUI. 我已经开始学习DialogFlow,我想知道如何通过代码而不是通过GUI创建实现的意图。

here is my existing Fulfillment code: 这是我现有的实现代码:

'use strict';

const functions = require('firebase-functions');
const { WebhookClient } = require('dialogflow-fulfillment');
const { Card, Suggestion } = require('dialogflow-fulfillment');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
    const agent = new WebhookClient({ request, response });
    console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
    console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

    function welcome(agent) {
        agent.add(`Welcome to my firstagent!`);
    }

    function fallback(agent) {
        agent.add(`I didn't understand!`);
        agent.add(`I'm sorry, can you try again??`);
        agent.add(`Can you please tell me again??`);
        agent.add(`Sorry, I don't understand`);
    }

let intentMap = new Map();
    //these are the intents that I've already created in dialog flow, But I want to create a new intent in code!
    intentMap.set('Default Welcome Intent', welcome);
    intentMap.set('Default Fallback Intent', fallback);
    agent.handleRequest(intentMap);
});

You cannot create an Intent "in Fulfillment" as the fulfillment webhook is intended to answer to whatever intents are currently set up in your agent, not manipulate them. 您无法创建“实现中的意图”,因为实现Web挂钩旨在回答您代理中当前设置的任何意图,而不是对其进行操作。

You can however manipulate the agent programmatically with the Dialogflow API , which is a normal Google Cloud API that has client libraries in many different programming languages . 但是,您可以使用Dialogflow API以编程方式操作代理,这是一个普通的Google Cloud API,具有许多不同编程语言的客户端库。 To create an intent you would want to have a look at the projects.agent.intents.create method. 要创建一个意图,您需要看一下projects.agent.intents.create方法。

Dialogflow has a create intent API, so you can create an intent from your Dialogflow fulfillment with the create intent API Dialogflow具有创建意图API,因此您可以使用create intent API从Dialogflow实现中创建意图

User request --> Dialogflow --> fulfillment 
                     |               |

                     -<-<- create intent API

While the other answer correctly points out that the newly created intent cannot be used to respond to the fulfillment request (the agent must be trained before the new intent can be matched by Dialogflow). 尽管另一个答案正确指出,新创建的意图不能用于响应实现请求(在Dialogflow可以匹配新意图之前,必须对代理进行培训)。 It can be useful to create a learning or dynamically changing agent over time. 随着时间的推移创建学习型或动态更改的代理可能很有用。 For instance if you have a lot of user queries for a particular subject like soccer that are matched to you fallback intent you can programmatically create a new intent with those queries as training phrases and note that soccer queries will be supported soon when you build that feature. 例如,如果您有很多针对特定主题(例如足球)的用户查询与您的后备意图相匹配,则可以以编程方式用这些查询作为训练短语来创建新的意图,并请注意,构建该功能后不久将支持足球查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM