Jay Patel
Jay Patel

Reputation: 2451

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.

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);
});

Upvotes: 1

Views: 3832

Answers (2)

mattcarrollcode
mattcarrollcode

Reputation: 3469

Dialogflow has a create intent API, so you can create an intent from your Dialogflow fulfillment with the create intent API

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). 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.

Upvotes: 1

gmolau
gmolau

Reputation: 3005

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.

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. To create an intent you would want to have a look at the projects.agent.intents.create method.

Upvotes: 3

Related Questions