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

TypeError: this.sendToWatson is not a function #200

Closed febdev closed 5 years ago

febdev commented 5 years ago

I have moved from build 1.8.4 to 2.1.4 to allow new ICP4D token authentication. Now I am getting this error message below.

An error occurred in the receive middleware: TypeError: this.sendToWatson is not a function at Ware.<anonymous> (/Users/jinkim/Documents/deckard/deckard/node_modules/botkit-middleware-watson/src/index.ts:177:17) at Generator.next (<anonymous>) at /Users/jinkim/Documents/deckard/deckard/node_modules/botkit-middleware-watson/lib/index.js:23:71 at new Promise (<anonymous>) at __awaiter (/Users/jinkim/Documents/deckard/deckard/node_modules/botkit-middleware-watson/lib/index.js:19:12) at Ware.receive (/Users/jinkim/Documents/deckard/deckard/node_modules/botkit-middleware-watson/lib/index.js:126:16) at Ware.<anonymous> (/Users/jinkim/Documents/deckard/deckard/node_modules/wrap-fn/index.js:75:16) at Ware.<anonymous> (/Users/jinkim/Documents/deckard/deckard/node_modules/wrap-fn/index.js:57:27) at next (/Users/jinkim/Documents/deckard/deckard/node_modules/ware/lib/index.js:85:20) at /Users/jinkim/Documents/deckard/deckard/node_modules/wrap-fn/index.js:121:18

Nothing has changed on my end except using import { WatsonMiddleware } from 'botkit-middleware-watson'; and changing from const watsonMiddleware = require('botkit-middleware-watson')... to const watsonMiddleware = new WatsonMiddleware(... and adding babel preset to allow importing class.

Any idea how to resolve this?

Naktibalda commented 5 years ago

You must bind receive method like this now:

controller.middleware.receive.use(
  watsonMiddleware.receive.bind(watsonMiddleware),
);
febdev commented 5 years ago

Thanks heaps!

developer-bz commented 4 years ago

Hi @Naktibalda , What if i want to use message filtering with this new method? Can you give one sample with the new method? Thanks

Naktibalda commented 4 years ago

It is exactly the same.

developer-bz commented 4 years ago

Do you mean it is the same as below,

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

slackController.middleware.receive.use(receiveMiddleware);

We don't need to bind receive method? Also, if i use the same method as above, it doesn't work.

Naktibalda commented 4 years ago

Bind is only necessary if method is passed as parameter in order to allow it to access other fields of middleware.

developer-bz commented 4 years ago

It doesn't work as it is. Can you please provide with a sample?

Thanks

Naktibalda commented 4 years ago

No, I can't give you another sample.

It doesn't work as it is. Please be more specific. Do you get an error or does it do nothing?

The first guess is that a value of message.type is not what you think it is, so do some debugging with console.log.