0

I am just starting to use Dialogflow to build some simple apps for my Google Home and I am having trouble creating an app that would simply returns a random name with a sentence.

For example: I say "give us a challenge": I want the app to return something like $random_name should do 10 push ups.

Is this possible to achieve?

Thank you!

Nick Felker
  • 11,536
  • 1
  • 21
  • 35
  • It is possible, but it requires a fulfillment webhook. What have you tried so far and what problems are you having? – Prisoner Mar 20 '18 at 00:56

1 Answers1

0

As the comment above, you need to use your fulfillment to determine the name and reply it to the user. Simply, you can use like the code below to return the name from fixed name's array:

"use strict";

process.env.DEBUG = "actions-on-google:*";

const App = require("actions-on-google").DialogflowApp;
const request = require("request");

const nameListFromConst = [
    "name1", "name2", "name3", "name4", "name5",
    "name6", "name7", "name8", "name9", "name10"
];

exports.foobar = (req, res) => {
    const app = new App({request: req, response: res});

    const inputWelcome = app => {
        const index = Math.floor(Math.random() * 10);
        const name = nameListFromConst[index];
        app.ask(name);
    };

    const actionMap = new Map();
    actionMap.set("input.welcome", inputWelcome);
    app.handleRequest(actionMap);
};

But, it seems that you want to determine the name from entities you registered into your agent of Dialogflow. If true, you can retrieve your entities with Dialogflow API dynamically in the fulfillment code like the following:

exports.foobar = (req, res) => {
    const app = new App({request: req, response: res});

    const inputWelcome = app => {
        const options = {
            url: "https://api.dialogflow.com/v1/entities/{YOUR_ENTITY_ID}",
            method: "GET",
            headers: {
                "Authorization": "Bearer {YOUR_DEVELOPER_ACCESS_TOKEN}",
                "Content-type": "application/json; charset=UTF-8"
            },
            json: true
        };
        request(options, (error, response, body) => {
            if (error) {
                console.log(error);
                app.ask("Error occurred.");
            } else {
                const nameListFromEntity = body.entries.map(x => {
                    return x.value;
                });
                const index = Math.floor(Math.random() * 10);
                const name = nameListFromEntity[index];
                app.ask(name);
            }
        });
    };

    const actionMap = new Map();
    actionMap.set("input.welcome", inputWelcome);
    app.handleRequest(actionMap);
};
Yoichiro Tanaka
  • 835
  • 7
  • 14