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
207 stars 257 forks source link

controller.hears doesn't trigger when use wrapper for receive handler #217

Open NxP4Code opened 3 years ago

NxP4Code commented 3 years ago

I am trying to filter messages passed to the Watson. I followed the step to use the wrapper function.

    const receiveMiddleware = (bot, message, next) => {
        if (message.text && message.type == 'direct_message') {
            watsonMiddleware.receive(bot, message, next);
        } else {
            next();
        }
    };

    controller.middleware.receive.use(receiveMiddleware);

Receive function gets registered and it gets executed. Problem is that the following hears function doesn't get triggered. I made sure the intent is matching.

    controller.hears(
        async (message) => {
            return message.watsonData && message.watsonData.intents.length > 0 && message.watsonData.intents[0].intent === 'General_Greetings'
        },
        'direct_message',
        async function (bot, message) {
            await bot.reply(message, message.watsonData.output.text.join('\n'));
        }
    );

Above mentioned hears function gets triggered fine with the following receive handler.

   controller.middleware.receive.use(
        watsonMiddleware.receive.bind(watsonMiddleware),
    );
Naktibalda commented 3 years ago

You have an easy workaround of calling next() after await watsonMiddleware.receive(bot, message);.

It is up to you to implement next parameter in receive method and raise a pull request.

NxP4Code commented 3 years ago

what is puzzling to me is, why does wrapper doesn't work and direct receive middleware registration works. My understanding is in both cases next is not implemented.

NxP4Code commented 3 years ago

The following does work, I am still confused why does it work when I do direct middleware registration which doesn't call next().


    const receiveMiddleware = async (bot, message, next) => {
        if (message.text && message.type == 'direct_message') {
            await watsonMiddleware.receive(bot, message);
            next();
        } else {
            next();
        }
    };

    controller.middleware.receive.use(receiveMiddleware);