govardhan
govardhan

Reputation: 31

Trying to connect Microsoft QnA maker service with Bot Framework but not getting any reply from my bot

Bot Framework Emulator

[18:48:31] -> POST 202 [conversationUpdate] 
[18:48:31] -> POST 202 [conversationUpdate] 
[18:48:36] -> POST 202 [message] hello 
[18:48:37] Warning: The Bot Framework State API is not recommended for production environments, and may be deprecated in a 
future release. Learn how to implement your own storage adapter. 
[18:48:37] <- GET 200 getPrivateConversationData 
[18:48:37] <- GET 200 getUserData 
[18:48:37] <- GET 200 getConversationData 
[18:48:37] <- POST 200 setPrivateConversationData 
[18:48:37] <- POST 200 Reply[event] Debug Event 

I'm new to Microsoft bot framework, trying to build basic bot using QnA maker

But I got stuck in connecting QnA maker service with app.js.

Not getting the response from QnA maker.

$ nodemon app.js
[nodemon] 1.17.5
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js`
restify listening to http://[::]:3978
WARN: ChatConnector: receive - emulator running without security enabled.
ChatConnector: message received.
WARN: ChatConnector: receive - emulator running without security enabled.
ChatConnector: message received.
WARN: ChatConnector: receive - emulator running without security enabled.
ChatConnector: message received.
The Bot State API is deprecated.  Please refer to https://aka.ms/I6swrh for details on how to replace with your own storage.
UniversalBot("*") routing "hello" from "emulator"
Session.beginDialog(/)
/ - Session.sendBatch() sending 0 message(s)
The Bot State API is deprecated.  Please refer to https://aka.ms/I6swrh for details on how to replace with your own storage.

app.js

const restify = require('restify');
const builder = require('botbuilder');
const cognitiveServices = require('botbuilder-cognitiveservices');
//connecting to server

const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978,
    function () {
        console.log('%s listening to %s',server.name,server.url);
    }
);

const connector = new builder.ChatConnector({
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword
});
//listening post from server
server.post('/api/messages', connector.listen());

var bot = new builder.UniversalBot(connector);

const recognizer = new cognitiveServices.QnAMakerRecognizer({
    knowledgeBaseId: "ffek8d39-dldc-48df-a9db-d902efc18cda",
    subscriptionKey: "881jc9eb-1a5b-4a10-bi89-233afh83ce98",
});

const qnaMakerDialog = new cognitiveServices.QnAMakerDialog({
    recognizers: [recognizer],
    defaultMessage: "Sorry I don't understand the question",
    qnaThreshold: 0.4,
});

bot.dialog('/', qnaMakerDialog);

Upvotes: 3

Views: 2710

Answers (2)

Ali Heikal
Ali Heikal

Reputation: 4113

Just make sure you're using a valid knowledgeBaseId and authKey and add endpointHostName as stated in the GA announcement as follows using the sample provided here

var restify = require('restify');
var builder = require('botbuilder');
var cognitiveservices = require('../../../lib/botbuilder-cognitiveservices');

//=========================================================
// Bot Setup
//=========================================================

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
    console.log('%s listening to %s', server.name, server.url);
});

// Create chat bot
var connector = new builder.ChatConnector({
    appId: process.env.MICROSOFT_APP_ID,
    appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var bot = new builder.UniversalBot(connector);
bot.set('storage', new builder.MemoryBotStorage());         // Register in-memory state storage
server.post('/api/messages', connector.listen());

//=========================================================
// Bots Dialogs
//=========================================================

var recognizer = new cognitiveservices.QnAMakerRecognizer({
    knowledgeBaseId: 'set your kbid here',
    authKey: 'set your authorization key here',
    endpointHostName: 'set your endpoint host name'});

var basicQnAMakerDialog = new cognitiveservices.QnAMakerDialog({
    recognizers: [recognizer],
    defaultMessage: 'No match! Try changing the query terms!',
    qnaThreshold: 0.3
});

bot.dialog('/', basicQnAMakerDialog);

Upvotes: 0

JJ_Wailes
JJ_Wailes

Reputation: 2227

The QnAMaker is currently in GA version, and is no longer in Preview. This doesn't seem like much, but it does mean the difference of a single recognizer variable: endpointHostName.

You currently have:

const recognizer = new cognitiveServices.QnAMakerRecognizer({
    knowledgeBaseId: "kbid",
    subscriptionKey: "subKey"
});

INSTEAD, it should read as follows:

const recognizer = new cognitiveServices.QnAMakerRecognizer({
    knowledgeBaseId: "kbid",
    authKey: "subKey",
    endpointHostName: "https://NAMEOFMYQNABOTHERE.azurewebsites.net/qnamaker"
});

The endpoint is listed as 'HOST' on the code snippet from QnAMaker.ai.

As for the reason you're not getting any replies, it's because you don't have the code necessary for your bot to tell you it has no idea which QnAMaker Knowledgebase you're talking about. A minor alteration to your final bot.dialog portion will help this.

bot.dialog('qnaMakerDialog', qnaMakerDialog);    

bot.dialog('/', 
    [
        function (session) {
            var qnaKnowledgebaseId = "kbid";
            var qnaAuthKey = "subKey";
            var endpointHostName = "https://NAMEOFMYQNABOTHERE.azurewebsites.net/qnamaker";

            // QnA Subscription Key and KnowledgeBase Id null verification
            if ((qnaAuthKey == null || qnaAuthKey == '') || (qnaKnowledgebaseId == null || qnaKnowledgebaseId == ''))
                session.send('Please set QnAKnowledgebaseId, QnAAuthKey and QnAEndpointHostName (if applicable) in App Settings. Learn how to get them at https://aka.ms/qnaabssetup.');
            else {
                    session.replaceDialog('qnaMakerDialog');
            }
        }
    ]);

That way, if any of them are missing or incorrect, your bot will return the canned message shown above to let you know something's missing.

Happy coding!

Upvotes: 4

Related Questions