Skip to content

Commit 025314a

Browse files
chore: merge changes from master
2 parents 00cbb05 + 027a795 commit 025314a

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,36 @@ var receiveMiddleware = function (bot, message, next) {
152152
slackController.middleware.receive.use(receiveMiddleware);
153153
```
154154

155+
### Minimum Confidence
156+
157+
To use the setup parameter `minimum_confidence`, you have multiple options:
158+
159+
#### Use it manually in your self-defined controller.hears() function(s)
160+
161+
For example:
162+
```js
163+
controller.hears(['.*'], ['direct_message', 'direct_mention', 'mention', 'message_received'], function(bot, message) {
164+
if (message.watsonError) {
165+
bot.reply(message, "Sorry, there are technical problems."); // deal with watson error
166+
} else {
167+
if (message.watsonData.intents.length == 0) {
168+
bot.reply(message, "Sorry, I could not understand the message."); // was any intent recognized?
169+
} else if (message.watsonData.intents[0].confidence < watsonMiddleware.minimum_confidence) {
170+
bot.reply(message, "Sorry, I am not sure what you have said."); // is the confidence high enough?
171+
} else {
172+
bot.reply(message, message.watsonData.output.text.join('\n')); // reply with Watson response
173+
}
174+
}
175+
});
176+
```
177+
178+
#### Use the middleware's hear() function
179+
You can find the default implementation of this function [here](https://github.com/watson-developer-cloud/botkit-middleware/blob/e29b002f2a004f6df57ddf240a3fdf8cb28f95d0/lib/middleware/index.js#L40). If you want, you can redefine this function in the same way that watsonMiddleware.before and watsonMiddleware.after can be redefined. Refer to the [Botkit Middleware documentation](https://github.com/howdyai/botkit/blob/master/docs/middleware.md#hear-middleware) for an example. Then, to use this function instead of Botkit's default pattern matcher (that does not use minimum_confidence), plug it in using:
180+
```js
181+
controller.changeEars(watsonMiddleware.hear)
182+
```
183+
184+
Note: if you want your own `hear()` function to implement pattern matching like Botkit's default one, you will likely need to implement that yourself. Botkit's default set of 'ears' is the `hears_regexp` function which is implemented [here](https://github.com/howdyai/botkit/blob/77b7d7f80c46d5c8194453667d22118b7850e252/lib/CoreBot.js#L1180).
155185

156186
### Implementing app actions
157187

examples/multi-bot/bot-facebook.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@ var controller = Botkit.facebookbot({
2323

2424
var bot = controller.spawn();
2525
controller.hears('(.*)', 'message_received', function(bot, message) {
26-
bot.reply(message, message.watsonData.output.text.join('\n'));
26+
if (message.watsonError) {
27+
console.log(message.watsonError);
28+
bot.reply(message, message.watsonError.description || message.watsonError.error);
29+
} else if (message.watsonData && 'output' in message.watsonData) {
30+
bot.reply(message, message.watsonData.output.text.join('\n'));
31+
} else {
32+
console.log('Error: received message in unknown format. (Is your connection with Watson Conversation up and running?)');
33+
bot.reply(message, 'I\'m sorry, but for technical reasons I can\'t respond to your message');
34+
}
2735
});
2836

2937
module.exports.controller = controller;

examples/multi-bot/bot-slack.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ var bot = controller.spawn({
2222
});
2323

2424
controller.hears(['.*'], ['direct_message', 'direct_mention', 'mention'], function(bot, message) {
25-
bot.reply(message, message.watsonData.output.text.join('\n'));
25+
if (message.watsonError) {
26+
console.log(message.watsonError);
27+
bot.reply(message, message.watsonError.description || message.watsonError.error);
28+
} else if (message.watsonData && 'output' in message.watsonData) {
29+
bot.reply(message, message.watsonData.output.text.join('\n'));
30+
} else {
31+
console.log('Error: received message in unknown format. (Is your connection with Watson Conversation up and running?)');
32+
bot.reply(message, 'I\'m sorry, but for technical reasons I can\'t respond to your message');
33+
}
2634
});
2735

2836
module.exports.controller = controller;

examples/multi-bot/bot-twilio.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ var bot = controller.spawn({
2727
autojoin: true
2828
});
2929
controller.hears(['.*'], 'message_received', function(bot, message) {
30-
bot.reply(message, message.watsonData.output.text.join('\n'));
30+
if (message.watsonError) {
31+
console.log(message.watsonError);
32+
bot.reply(message, message.watsonError.description || message.watsonError.error);
33+
} else if (message.watsonData && 'output' in message.watsonData) {
34+
bot.reply(message, message.watsonData.output.text.join('\n'));
35+
} else {
36+
console.log('Error: received message in unknown format. (Is your connection with Watson Conversation up and running?)');
37+
bot.reply(message, 'I\'m sorry, but for technical reasons I can\'t respond to your message');
38+
}
3139
});
3240

3341
module.exports.controller = controller;

examples/simple-bot/simple-bot-slack.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ slackController.hears(['.*'], ['direct_message', 'direct_mention', 'mention'], f
3636
middleware.interpret(bot, message, function() {
3737
if (message.watsonError) {
3838
console.log(message.watsonError);
39-
bot.reply(message, 'I\'m sorry, but for technical reasons I can\'t respond to your message');
40-
} else {
39+
bot.reply(message, message.watsonError.description || message.watsonError.error);
40+
} else if (message.watsonData && 'output' in message.watsonData) {
4141
bot.reply(message, message.watsonData.output.text.join('\n'));
42+
} else {
43+
console.log('Error: received message in unknown format. (Is your connection with Watson Conversation up and running?)');
44+
bot.reply(message, 'I\'m sorry, but for technical reasons I can\'t respond to your message');
4245
}
4346
});
4447
});

0 commit comments

Comments
 (0)