1
votes

So I am trying to create a conversation about Vitamins from Dialogflow. But, I keep getting the same error and because of that, the same response from the AI. So what we want to happen is, (User) - Give me more information about Vitamins (AI) - Sure. which Vitamin (User) - *here we specify which Vitamin, for example - * Vitamin A. (AI) - Then the AI gives us the specified response for Vitamin A

Please help

Here's our code in fulfillment

const functions = require('firebase-functions');
const {dialogflow} = require('actions-on-google')

const VITAMIN_INTENT = 'Vitamin'
const VITAMINS_ENTITY = 'Vitamins'

const app = dialogflow()

app.intent(VITAMIN_INTENT, (conv) => {
    const vitamin_type = conv.parameters[VITAMINS_ENTITY].toLowerCase();
    if (vitamin_type == 'Vitamin A') {
    conv.ask("Sources: carrots, sweet potato, green leafy vegetable, squash")
    } else if (vitamin_type == 'Vitamin B') {
        conv.ask("Sources: Animal products (such as fish, poultry, meat, eggs, or dairy); Also found in fortified breakfast cereals and enriched soy or rice milk.")
    } else if (vitamin_type == 'Vitamin B1') {
        conv.ask("Sources: Sunflower seeds, asparagus, lettuce, mushrooms, black beans, navy beans, lentils, spinach, peas, pinto beans, lima beans, eggplant, Brussels sprouts, tomatoes, tuna, whole wheat, soybeans.")
    } else if (vitamin_type == 'Vitamin B2') {
        conv.ask("Sources:B2 ")
    } 
})
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app)
1
Please update your question to include a screen shot of the "Vitamin" intent in the screen editor and the error logs from your program's run. See stackoverflow.com/help/how-to-ask and medium.com/google-developer-experts/… - Prisoner

1 Answers

1
votes

Error 500 usually indicates your program has crashed for some reason, although without looking at the logs, it is difficult to tell exactly why.

My guess is that in the part

const vitamin_type = conv.parameters[VITAMINS_ENTITY].toLowerCase();

you don't have a parameter named "Vitamins". Parameter names are case sensitive, and these are usually all lowercase. So conv.parameters[VITAMINS_ENTITY] evaluates to undefined and undefined does not have a function .toLowerCase().

Additionally, you have at least one logic problem in your code. The line

const vitamin_type = conv.parameters[VITAMINS_ENTITY].toLowerCase();

which makes sure the string vitamin_type is in lower case. So values such as "vitamin a".

But when you test the values, you use comparisons such as

if (vitamin_type == 'Vitamin A') {

where you are comparing it to values like "Vitamin A". So the values will never match.

Since none of the values will match, you'll exit out of the function without having called conv.ask(), which will generate an error. (Although not an error 500.)