4

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.

SmiffyKmc
  • 801
  • 1
  • 16
  • 34
  • Can you update your question with a screen shot of your `skillRatio` Intent screen in Dialogflow? I also see you're testing for `agent.FACEBOOK` specifically. What happens if you uncomment the `intentMap.set()` for skill-ratio and remove this if block? – Prisoner Jan 10 '19 at 21:56
  • Prisoner, I sure can, bare with me. Do you just want to see the Phrases? Thanks a million for the fast reply also! I tried to test for agent.FACEBOOK as I felt it was looking specifically for a Platform. But I could see in AWS CloudWatch that Facebook was already set. Nothing happens when I uncomment the skill-ratio mapping. I get the error in my logs when trying to call it. – SmiffyKmc Jan 10 '19 at 22:06
  • Seeing all sections of the entire Intent can help pick up if the problem was with something besides the phrases - so please include everything if you can. – Prisoner Jan 10 '19 at 22:10
  • Perfect will add the rest. There wasn't anything else really to the intent other than the switch for Fulfilment. I'll add the other photos also. – SmiffyKmc Jan 10 '19 at 22:11
  • 1
    I'm actually really annoyed with myself. It turns out that the default payload on the Dialogflow image was causing the issue. Twice I thought I could remove it but thought it was too small to be the issue. The code works fine now after I removed the Custom Payload. Sorry about the confusion but maybe this might help others in the future. Thanks for your help! – SmiffyKmc Jan 10 '19 at 22:20
  • I was gonna ask about that - good catch! Go ahead and write that as an answer so you can accept it. – Prisoner Jan 10 '19 at 23:16
  • 2
    (But that's why I asked to post everything. {: ) – Prisoner Jan 10 '19 at 23:16
  • Hahah that's true xD. Thanks a million though, the fast response helped me to keep at it :) – SmiffyKmc Jan 11 '19 at 08:40
  • (And I'm serious - write it up as an answer rather than a comment. It will help others with the same problem.) – Prisoner Jan 11 '19 at 11:31

2 Answers2

3

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

SmiffyKmc
  • 801
  • 1
  • 16
  • 34
1

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;
});