dialogflow / dialogflow-fulfillment-nodejs

Dialogflow agent fulfillment library supporting v1&v2, 8 platforms, and text, card, image, suggestion, custom responses
Apache License 2.0
598 stars 281 forks source link

how to send a response? #275

Open dcsan opened 4 years ago

dcsan commented 4 years ago

probably the most basic question, but the docs (?) suck. I want to send a basic text response from an agent. something like:

router.post('/api/webhook', async (request, response) => {
  const agent = new WebhookClient({ request, response })
  agent.add('hello world')

  // now how do i tell dialogflow to handle the response? none of these work:
  response.send(agent)
  agent.resolve()

this works but its dufus

  let intentMap = new Map()
  intentMap.set('reply', () => {
    agent.add('hello world')
  })
  agent.intent = 'reply'
  agent.handleRequest(intentMap)

I don't want to use the intentMap.set or ideally agent.handleRequest(intentMap) And I certainly don't want to use google cloud funcs, plain express is fine.

dcsan commented 4 years ago

actually even the above doesn't seem to work, something to do with calling context?

I'm using some async request/response express routes, would that break the DF library?

(node:11008) UnhandledPromiseRejectionWarning: Error: Unknown response type: "undefined"
    at WebhookClient.addResponse_ (/Users/dc/dev/considr/demoflow/server/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:287:13)
    at WebhookClient.add (/Users/dc/dev/considr/demoflow/server/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:254:12)
    at /Users/dc/dev/considr/demoflow/server/src/stories/CoachReply.ts:31:13
    at WebhookClient.handleRequest (/Users/dc/dev/considr/demoflow/server/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:313:44)
    at Object.<anonymous> (/Users/dc/dev/considr/demoflow/server/src/stories/CoachReply.ts:34:11)
    at step (/Users/dc/dev/considr/demoflow/server/src/stories/CoachReply.ts:33:23)
    at Object.next (/Users/dc/dev/considr/demoflow/server/src/stories/CoachReply.ts:14:53)
    at fulfilled (/Users/dc/dev/considr/demoflow/server/src/stories/CoachReply.ts:5:58)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:11008) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:11008) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

seems this is the doc: https://dialogflow.com/docs/reference/fulfillment-library/webhook-client#new_webhookclientoptions

dcsan commented 4 years ago

this seems to work (in case helps someone else)

const intentHandler = (agent) => {
      let output = `${HEADER} \n ${reply.text}`
      agent.add(output)
    }
    agent.handleRequest(intentHandler)
dcsan commented 4 years ago

https://blog.dialogflow.com/post/fulfillment-library-beta/

FlorianDr commented 4 years ago

this seems to work (in case helps someone else)

const intentHandler = (agent) => {
      let output = `${HEADER} \n ${reply.text}`
      agent.add(output)
    }
    agent.handleRequest(intentHandler)

This only works for a single intent, bc of this line: https://github.com/dialogflow/dialogflow-fulfillment-nodejs/blob/master/src/dialogflow-fulfillment.js#L296

If you would like to use multiple intents, you need to either go with the Map, if you want to follow the flow.