1

Hi i have created a dialogflow nodejs backend which detects intents using the client library for nodejs.

 const sessionPath = this.sessionClient.sessionPath(this.configService.get('dialogFlowProjectId'), sessionId);

        const request = {
            session: sessionPath,
            queryInput: {
                text: {
                    text: query,
                    languageCode: "en-US"
                }
            }
        };
        // Send request and log result
        Logger.log(request);
        const responses = await this.sessionClient.detectIntent(request);

this works fine but I also want to trigger a fulfillment for certain intents.

I have setup a webhook url - this works fine when you use the chat in the dialogflow console. But when I use the method that I have created to send the request to dialogflow the webhook doesn't get called and goes to fallback intent. I'm calling the same intent through the dialogflow console chat and through my own API.

How can I trigger the webhook call when I use the client library API?

Prisoner
  • 49,922
  • 7
  • 53
  • 105
SujithaW
  • 440
  • 5
  • 16

2 Answers2

0

First, remember that you don't "call an Intent", either through the test console or through the API. What you can do is send query text that should be matched by an Intent.

If you have set the Intent so that it should call the Fulfillment webhook, then this should happen no matter the source, as long as the Intent itself is triggered.

That the Fallback Intent is getting triggered instead suggests that something about the query text isn't matching the Intent you think it should be matching. I would check to make sure the text you're sending in query should match the training phrases for the Intent in question, that you have no Input Contexts, and that your agent is set for the "en-US" language code.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
0

For routing multiple intents through a webhook you're going to want to have a handler.js file with an express router in it...

const def = require('./intents/default')
const compression = require('compression')

const serverless = require('serverless-http')
const bodyParser = require('body-parser')

const { WebhookClient } = require('dialogflow-fulfillment')

const express = require('express')
const app = express()

// register middleware
app.use(bodyParser.json({ strict: false }))
app.use(function (err, req, res, next) {
  res.status(500)
  res.send(err)
})
app.use(compression())
app.post('/', async function (req, res) {
  // Instantiate agent
  const agent = new WebhookClient({ request: req, response: res })
  const intentMap = new Map()

  intentMap.set('Default Welcome Intent', def.defaultWelcome)

  await agent.handleRequest(intentMap)
})

module.exports.server = serverless(app)

As you can see, this handler.js is using serverless-http and express.

Your intent.js function could look something like this ...

module.exports.defaultWelcome = async function DefaultWelcome (agent) {

  const responses = [
    'Hi! How are you doing?',
    'Hello! How can I help you?',
    'Good day! What can I do for you today?',
    'Hello, welcoming to the Dining Bot. Would you like to know what\'s being served? Example: \'What\'s for lunch at cronkhite?\'',
    'Greetings from Dining Bot, what can I do for you?(Hint, you can ask things like: \'What\'s for breakfast at Annenberg etc..\' or \'What time is breakfast at Anennberg?\')',
    'greetings',
    'hey',
    'long time no see',
    'hello',
    "lovely day isn't it"
  ]
  const index = Math.floor((Math.random() * responses.length) + 1)
  console.log(responses[index].text)
  agent.add(responses[index].text)
}

This gives you the bare bones for routing multiple request through your webhook. Hope this helps.

Lucas Raza
  • 499
  • 1
  • 4
  • 13