dialogflow / dialogflow-fulfillment-nodejs

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

How to send custom Payload from Dialogflow fulfilment #267

Open Shajeel-Afzal opened 4 years ago

Shajeel-Afzal commented 4 years ago

I am trying to send custom payload from the webhook (Firebase Cloud Function) but I am not able to achieve what I am trying. In response I wanna get something like:

"fulfillmentMessages": [
    {
      "text": {
        "text": [
          "First question."
        ]
      }
    },
    {
      "text": {
        "text": [
          "What is your name?"
        ]
      }
    },
    "payload": {
      "deplay": "13 seconds"
    }
  ]

But right now I don't know how to add Payload in the fulfillmentMessages Array. My current code is:

let response = ['First question.', 'What is your name?'];
agent.add(response);

I have tried this

agent.add(new Payload('PLATFORM_UNSPECIFIED', JSON.stringify(json)));

But then it does not send fulfillmentMessages array. Any solution?

gabriel-soga-movile commented 4 years ago

+1, I'm having the same problem.

But as I'm analyzing, my payload is being nested inside fullfilment.webhookPayload instead of fulfillment.fulfillmentMessages, even using the payload flags sendAsMessage or rawPayload

hspens commented 4 years ago

Check which version of dialogflow-fulfillment you are using. The inline editor for firebase use version 5.0.0 by default and I noticed a similar behaviour to what is described above with that version, so try changing the version of dialogflow-fulfillment:

In package.json: "dialogflow-fulfillment": "^0.5.0" ---> "dialogflow-fulfillment": "^0.6.1"

s22su commented 4 years ago

In package.json: "dialogflow-fulfillment": "^0.5.0" ---> "dialogflow-fulfillment": "^0.6.1"

Thanks @HSpens works as expected after update. Just for others who Google the same thing, the code to send custom payload is:

// const { Payload } = require("dialogflow-fulfillment");

const payload = {
  key: 'value',
  key2: 2
};

agent.add(
  new Payload(agent.UNSPECIFIED, payload, {rawPayload: true, sendAsMessage: true})
);
Shajeel-Afzal commented 4 years ago

@s22su, does voice also work in case of Custom payload?

s22su commented 4 years ago

@s22su, does voice also work in case of Custom payload?

I don't have an answer to that as I haven't built any bot who answer using voice.

ParticleDuality commented 4 years ago

In package.json: "dialogflow-fulfillment": "^0.5.0" ---> "dialogflow-fulfillment": "^0.6.1"

Thanks @HSpens works as expected after update. Just for others who Google the same thing, the code to send custom payload is:

// const { Payload } = require("dialogflow-fulfillment");

const payload = {
  key: 'value',
  key2: 2
};

agent.add(
  new Payload(agent.UNSPECIFIED, payload, {rawPayload: true, sendAsMessage: true})
);

God, man I was trying so hard to find a solution and this code resolved my issue. Thanks.

EugeniaCasFrag commented 4 years ago

In package.json: "dialogflow-fulfillment": "^0.5.0" ---> "dialogflow-fulfillment": "^0.6.1"

Thanks @HSpens works as expected after update. Just for others who Google the same thing, the code to send custom payload is:

// const { Payload } = require("dialogflow-fulfillment");

const payload = {
  key: 'value',
  key2: 2
};

agent.add(
  new Payload(agent.UNSPECIFIED, payload, {rawPayload: true, sendAsMessage: true})
);

Hi, sorry to mention this post as a long time has passed, but I am desperate. I have copied your code, I have upgraded versions and it is still not working, though there is no error message in logs, nothing is showing .......please help me... the JSON that I get as a response is the following:

{ "responseId": "56a0a335-5814-402e-8076-464b37ae6038-266f04e0", "queryResult": { "queryText": "list", "parameters": {}, "allRequiredParamsPresent": true, "fulfillmentMessages": [ { "payload": { "key": "value", "key2": 2 }, "platform": "TELEGRAM" } ], "intent": { "name": "projects/my-best-self-hywkfp/agent/intents/f8edc35d-02b9-41ce-b6a5-166d2655fd89", "displayName": "DisplayList" }, "intentDetectionConfidence": 1, "diagnosticInfo": { "webhook_latency_ms": 421 }, "languageCode": "en" }, "webhookStatus": { "message": "Webhook execution successful" } }

jmodiano commented 4 years ago

In package.json: "dialogflow-fulfillment": "^0.5.0" ---> "dialogflow-fulfillment": "^0.6.1"

Thanks @HSpens works as expected after update. Just for others who Google the same thing, the code to send custom payload is:

// const { Payload } = require("dialogflow-fulfillment");

const payload = {
  key: 'value',
  key2: 2
};

agent.add(
  new Payload(agent.UNSPECIFIED, payload, {rawPayload: true, sendAsMessage: true})
);

What exactly goes in payload? Like the original question I'm trying to send something like this:

"fulfillmentMessages": [
    {
      "text": {
        "text": [
          "First question."
        ]
      }
    },
    {
      "text": {
        "text": [
          "What is your name?"
        ]
      }
    },
    "payload": {
      "deplay": "13 seconds"
    }
  ]

But I can't seem to get it into that format in a way that works!

Kirigetov commented 3 years ago

In package.json: "dialogflow-fulfillment": "^0.5.0" ---> "dialogflow-fulfillment": "^0.6.1"

Thanks @HSpens works as expected after update. Just for others who Google the same thing, the code to send custom payload is:

// const { Payload } = require("dialogflow-fulfillment");

const payload = {
  key: 'value',
  key2: 2
};

agent.add(
  new Payload(agent.UNSPECIFIED, payload, {rawPayload: true, sendAsMessage: true})
);

Hi, sorry to mention this post as a long time has passed, but I am desperate. I have copied your code, I have upgraded versions and it is still not working, though there is no error message in logs, nothing is showing .......please help me... the JSON that I get as a response is the following:

{ "responseId": "56a0a335-5814-402e-8076-464b37ae6038-266f04e0", "queryResult": { "queryText": "list", "parameters": {}, "allRequiredParamsPresent": true, "fulfillmentMessages": [ { "payload": { "key": "value", "key2": 2 }, "platform": "TELEGRAM" } ], "intent": { "name": "projects/my-best-self-hywkfp/agent/intents/f8edc35d-02b9-41ce-b6a5-166d2655fd89", "displayName": "DisplayList" }, "intentDetectionConfidence": 1, "diagnosticInfo": { "webhook_latency_ms": 421 }, "languageCode": "en" }, "webhookStatus": { "message": "Webhook execution successful" } }

try send like this (set rawPayload to false): agent.add( new Payload(agent.TELEGRAM, payload, {rawPayload: false, sendAsMessage: true}) );

miguel191156 commented 2 years ago

Hello, how can I send a voice file using this method?

Exter-dg commented 2 years ago

try send like this (set rawPayload to false): agent.add( new Payload(agent.TELEGRAM, payload, {rawPayload: false, sendAsMessage: true}) );

@Kirigetov , How to set the rawPayload inside the json response which I am sending back to Dialogflow just like @jmodiano