I’m currently developing a bot that uses Azure’s Custom Question Answering service. I’ve verified all credentials for creating the adapter and configuring the service, but I’m encountering an issue when testing in Web Chat and Direct Line. The error I receive is:
There was an error sending this message to your bot: HTTP status code Unauthorized
Here is the relevant portion of my code:
const { onRequest } = require("firebase-functions/v2/https");
const logger = require("firebase-functions/logger");
const { BotFrameworkAdapter, MemoryStorage, ConversationState, UserState } = require('botbuilder');
const axios = require('axios');
const functions = require('firebase-functions');
// Create adapter.
const adapter = new BotFrameworkAdapter({
appId: '6c546f61-...',
appPassword: 'EXK8Q~....'
});
// Configuration for Custom Question Answering
const projectName = 'mr-...';
const deploymentName = 'production';
const endpointKey = 'a27ee....';
const host = 'https://mr-...';
// Create storage.
const memoryStorage = new MemoryStorage();
const conversationState = new ConversationState(memoryStorage);
const userState = new UserState(memoryStorage);
// Create bot logic.
const botLogic = async (context) => {
if (context.activity.type === 'message') {
const question = context.activity.text;
try {
const response = await axios.post(
`${host}/language/:query-knowledgebases?projectName=${projectName}&api-version=2021-10-01&deploymentName=${deploymentName}`,
{
top: 1,
question: question
},
{
headers: {
'Ocp-Apim-Subscription-Key': endpointKey,
'Content-Type': 'application/json'
}
}
);
const answers = response.data.answers;
if (answers.length > 0) {
await context.sendActivity(answers[0].answer);
} else {
await context.sendActivity('Sorry, I do not have an answer for that.');
}
} catch (error) {
console.error('Error querying the knowledge base:', error);
await context.sendActivity('There was an error querying the knowledge base.');
}
}
};
const cors = require('cors')({ origin: true });
// Listen for incoming requests and send them to the bot.
exports.bot = functions.https.onRequest((req, res) => {
console.error('Just a test message');
adapter.processActivity(req, res, async (context) => {
await botLogic(context);
});
});
I’ve ensured that the appId, appPassword, projectName, deploymentName, endpointKey, and host are correct, and the app password is not expired. I’m unsure if I’m missing something in my code or elsewhere in the configuration. Any guidance on resolving this issue would be greatly appreciated.
Note: I deployed this function to Firebase function, and I didn’t get any warning or error in Firebase logs and also during deployment.
2
Answers
I did everything, but none of them solved my problem. I deleted the functions from Firebase several times, but my problem was not solved. In the end, I only deleted the functions of Firebase through the Windows terminal once, and then I redeployed, and then everything was fixed. The most absurd solution possible
Generate a new App ID and Password by clicking Create New Microsoft App ID. Copy both and save them securely. You’ll use these in your bot code.
Here AppId, AppPassword means Client ID, Client secret you can check below.
client ID:
Client secret (Copy the value):
Check the below code it worked successfully.
Code:
If you are testing with the Bot Framework Emulator copy the Direct Line Secret check below.
Enable authentication for access to the trusted user ID’s.