watson-developer-cloud / botkit-middleware

A middleware to connect Watson Conversation Service to different chat channels using Botkit
https://www.npmjs.com/package/botkit-middleware-watson
Apache License 2.0
206 stars 254 forks source link

Watson Conversation is called with the response payload #18

Closed robertyates closed 7 years ago

robertyates commented 7 years ago

I got up and running with botkit and the watson middleware.

When I looked at the logs in the "improve" tab, I noticed that Watson Conversation was also getting called with the response payload.

In my example I would issue a "hello", Watson would respond with a "Hello Again" but then the code was sending this response back to the server as a user request so I would also see in the improve logs an utterance coming from the user of "Hello Again".

I debugged the code and it looks like receive also gets called when a message is sent back to the client (this was slack). I hacked the code below to look for bot_id and this seems to have fixed it, but it may well only work for slack.

      if (!middleware.conversation) {
        debug('Creating Conversation object with parameters: ' + JSON.stringify(config, 2, null));
        middleware.conversation = new ConversationV1(config);
      }

      if (!message.text || ignoreType.indexOf(message.type) !== -1 || message.reply_to || message.bot_id) {
        // Ignore messages initiated by Slack. Reply with dummy output object

        message.watsonData = {
          output: {
            text: []
          }
        };
        return message;
      }
stevenoh93 commented 7 years ago

I've tested this with bot Slack and Facebook Messenger, and it looks like this problem is unique to Slack. The change you proposed above does fix the problem, and it seems like the best solution to me. I'll submit a pull request and have others review it as well.