skip to Main Content

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


  1. Chosen as BEST ANSWER

    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


  2. There was an error sending this message to your bot: HTTP status code Unauthorized.

    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:
    enter image description here

    Client secret (Copy the value):
    enter image description here

    Check the below code it worked successfully.

    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: 'YOUR_MICROSOFT_APP_ID',
        appPassword: 'YOUR_MICROSOFT_APP_PASSWORD'
    });
    
    // Configuration for Custom Question Answering
    const projectName = 'YOUR_PROJECT_NAME';
    const deploymentName = 'production';
    const endpointKey = 'YOUR_ENDPOINT_KEY';
    const host = 'YOUR_ENDPOINT_URL';
    
    // Create storage.
    const memoryStorage = new MemoryStorage();
    const conversationState = new ConversationState(memoryStorage);
    const userState = new UserState(memoryStorage);
    
    // Bot logic function
    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.');
            }
        }
    };
    
    // CORS Middleware
    const cors = require('cors')({ origin: true });
    
    // Firebase Function to handle bot messages
    exports.bot = functions.https.onRequest((req, res) => {
        adapter.processActivity(req, res, async (context) => {
            await botLogic(context);
        });
    });
    

    If you are testing with the Bot Framework Emulator copy the Direct Line Secret check below.

    enter image description here

    Enable authentication for access to the trusted user ID’s.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search