0

I'm trying to update the functions in my messenger/wit.ai chat bot from using callbacks to promises.

This original format executes fine:

['buildScenario'](sessionId, context, cb) {

    var trendChoice = scenarioCombos['trends']
    var disruptionChoice = scenarioCombos['disruptions']
    context.trend = trendChoice[Math.floor(Math.random() * trendChoice.length)]
    context.disruption = disruptionChoice[Math.floor(Math.random() * disruptionChoice.length)]

    cb(context)
},

But when I update to Promises as below, it doesn't make it through:

['buildScenario']({sessionId, context, entities}) {
    return new Promise(function(resolve, reject) {
        var trendChoice = scenarioCombos['trends']
        var disruptionChoice = scenarioCombos['disruptions']
        context.trend = trendChoice[Math.floor(Math.random() * trendChoice.length)]
        context.disruption = disruptionChoice[Math.floor(Math.random() * disruptionChoice.length)]
        return resolve(context)
    })
},

I've tried debugging by adding console logs throughout the function like this:

enter image description here

When the function is triggered, it stops halfway through and fails to resolve the promise:

enter image description here

When I try console.log(context) within the function I get 'undefined'.

What am I missing?

EDIT: When I remove the curly brackets around my function parameters like so:

['buildScenario'](sessionId, context, entities) {
    console.log('BS POINT 1')
    return new Promise(function(resolve, reject) {
        console.log('BS POINT 2')
        var trendChoice = scenarioCombos['trends']
        console.log(trendChoice)
        console.log('BS POINT 3')
        var disruptionChoice = scenarioCombos['disruptions']
        console.log(disruptionChoice)
        console.log('BS POINT 4')
        console.log(context)
        context.trend = trendChoice[Math.floor(Math.random() * trendChoice.length)]
        console.log(context)
        console.log('BS POINT 5')
        context.disruption = disruptionChoice[Math.floor(Math.random() * disruptionChoice.length)]
        console.log(context)
        console.log('BS POINT 6')
        return resolve(context)
    })
},

I'm able to log my context but still can't resolve the Promise:

enter image description here

wanstan
  • 15
  • 5

2 Answers2

0

Your buildSenario should look like this, but its fine if you use it in your current way. You can ignore it coz its a warning message.

buildScenario({{sessionId, context, text, entities}}) {});

It seems that your some callback or Promises not geting resolved within 10 second.

I have only worked on wit.ai with fb bot integartion, in messenger bot node should send 200 status on receiving text message. Have a look at this code.

wit.ai example for fb messenger integration

Ashes Vats
  • 136
  • 1
  • 4
0

Turns out my package dependencies were limiting my node-wit API version to 3.3.2 and not allowing it to update any later (the API became Promise-based rather than using callbacks in v4.0.0). Once I edited my package.json file to enable the latest version of node-wit I got it working.

wanstan
  • 15
  • 5