node.jsazurebots

Azure Bot: "HTTP status code Unauthorized" Error when Using Custom Question Answering


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.


Solution

  • 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