0

I'm building my first FB Messenger chat bot using Wit as the NLP engine. All my services are connected and seem to be working on the surface, but when I look at my Heroku logs it seems that my bot's responses are being sent back to Wit to be parsed as well as user inputted messages. This is obviously causing issues further through my conversation flow when it comes time to trigger actions.

How do I make it so that my bot only parses user input, then responds appropriately according to my story in Wit?

Messenger window:

enter image description here

Relevant part of my Wit conversation flow:

enter image description here

My logs:

enter image description here

enter image description here

As far as I can tell, this is the important code:

var actions = {
say (sessionId, context, message, cb) {
    // Bot testing mode, run cb() and return
    if (require.main === module) {
        cb()
        return
    }

    console.log('WIT HAS A CONTEXT:', context)

    if (checkURL(message)) {
        FB.newMessage(context._fbid_, message, true)
    } else {
        FB.newMessage(context._fbid_, message)
    }

    cb()

    }, 

...

}

///

var read = function (sender, message, reply) {
console.log('READING LOG AAAAAAAAAAAAAAAAAAAAAA')
var sessionId = findOrCreateSession(sender)
console.log('READING LOG BBBBBBBBBBBBBBBBBBBBBB')
console.log(message)
    // Let's forward the message to the Wit.ai bot engine
    // This will run all actions until there are no more actions left to do
wit.runActions(
    sessionId, // the user's current session by id
    message,  // the user's message
    sessions[sessionId].context, // the user's session state
    function (error, context) { // callback
    console.log('READING LOG CCCCCCCCCCCCCC')
    if (error) {
        console.log('oops!', error)
    } else {
        // Wit.ai ran all the actions
        // Now it needs more messages
        console.log('READING LOG DDDDDDDDDDDDDDDD')
        console.log('Waiting for further messages')

        // Updating the user's current session state
        sessions[sessionId].context = context
        console.log('READING LOG EEEEEEEEEEEEEEEE')
    }
})
}

///

app.post('/webhooks', function (req, res) {
var entry = FB.getMessageEntry(req.body)
// IS THE ENTRY A VALID MESSAGE?
if (entry && entry.message) {
  if (entry.message.attachments) {
    // NOT SMART ENOUGH FOR ATTACHMENTS YET
    FB.newMessage(entry.sender.id, "That's interesting!")
  } else {
    // SEND TO BOT FOR PROCESSING
    console.log('SENDING TO BOT FOR PROCESSING XXXXX')
    Bot.read(entry.sender.id, entry.message.text, function (sender, reply) {
      FB.newMessage(sender, reply)
      return
    })
    console.log('SENDING TO BOT FOR PROCESSING YYYYY')
  }
}

res.sendStatus(200) 
})
wanstan
  • 15
  • 5
  • Facebook Messenger API will send your bot's messages to your bot's webhook. You need to compare sender user IDs in the webhook code and ignore messages that have the same sender ID as your bot's. – rgajrawala Sep 09 '16 at 01:17
  • Thanks for the tip - any hints on how to figure out my bot's sender ID? – wanstan Sep 09 '16 at 04:21

2 Answers2

0

I used the 'is_echo' : true to discern wits messages from others and it's been working.

if (event.message.is_echo) {
  console.log(`This sender is the wit bot.`);
  return;
}
0

When you create your Facebook messenger app, one of the webhooks events is message_echoes.

Make sure you you opt it out message_echoes for not receiving your own bot messages.

facebook messages echo webhook event

Guy Dubrovski
  • 1,542
  • 1
  • 20
  • 25