Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Platform can NOT be empty at new Payload in Dialogflow

I have a serverless app where I want to run my logic from the chatbot request coming from Facebook Messenger. When I run the intent function for test_handler I get the correct response back. But after I added another handler for skillRatio I seem to be getting the error in the title i.e

Error: Platform can NOT be empty at new Payload

. My code is as below.

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

const app = express();
app.use(bodyParser.json({ strict: false }));

const {WebhookClient, Payload, Image, Card, Suggestion} = require('dialogflow-fulfillment');
const request = require('request');

app.get('/', function (req, res) {
  res.send('Hello World !!!\n');
  console.log("Testing express lambda\n");
})

app.post('/', function (req, res) {
    const agent = new WebhookClient({request: req, response: res});

    function test_handler(agent) {
      agent.add(`Welcome to my agent on AWS Lambda!`);
      agent.add(new Image("https://image-charts.com/chart?chs=700x190&chd=t:60,40&cht=p3&chl=Hello%7CWorld&chf=ps0-0,lg,45,ffeb3b,0.2,f44336,1|ps0-1,lg,45,8bc34a,0.2,009688,1"))
    }

    function skillRatio(agent) {
      agent.add(`Let me just have a look and I'll gather the data. *Processing Chart Data....Mmmm Pie*. 
        Here we go! Here is the data on your $Skills.original request.`);
      //agent.add(`Feel free to save or share :)`);
      //agent.add(new Image("https://image-charts.com/chart?chs=700x190&chd=t:60,40&cht=p3&chl=Hello%7CWorld&chf=ps0-0,lg,45,ffeb3b,0.2,f44336,1|ps0-1,lg,45,8bc34a,0.2,009688,1"))
    }

    // Run the proper function handler based on the matched Dialogflow intent name
    let intentMap = new Map();
    intentMap.set('super-test', test_handler);
    //intentMap.set('skill-ratio', skillRatio);

    if (agent.requestSource === agent.FACEBOOK) {
      intentMap.set('super-test', test_handler);
      intentMap.set('skill-ratio', skillRatio);
    } else {

    }

    agent.handleRequest(intentMap);
})
module.exports.handler = serverless(app);

Dialogflow Images:

Training Phrases enter image description here enter image description here enter image description here

I am trying to run the code on Messenger. Any help would be hugely appreciated as I am so stuck trying to get my head around this.

like image 492
SmiffyKmc Avatar asked Sep 10 '25 09:09

SmiffyKmc


2 Answers

As it turns out, in the below image, a Custom Payload was causing the issue I was having. If you get the same error

Error: Platform can NOT be empty at new Payload.

Triple check your default responses across all the response types and remove anything that has an empty payload.

enter image description here

like image 75
SmiffyKmc Avatar answered Sep 13 '25 03:09

SmiffyKmc


Your resolution is a little intuitive and not completely correct. It is not specifically a problem with an empty payload, the problem persists with having a payload in general.

You can try to either set the platform manually like so => How to set a custom platform in Dialogflow NodeJS Client

or choose one of the methods described in here => https://github.com/dialogflow/dialogflow-fulfillment-nodejs/issues/153

Setting the platform befor initializing the WebHookClient

if (!request.body.queryResult.fulfillmentMessages)
    return;
request.body.queryResult.fulfillmentMessages = request.body.queryResult.fulfillmentMessages.map(m => {
    if (!m.platform)
        m.platform = 'PLATFORM_UNSPECIFIED';
    return m;
});
like image 35
angularNoob Avatar answered Sep 13 '25 02:09

angularNoob