node.jsazurebotframeworkchatbotbotkit

Botkit unauthorized when connecting through emulator (Microsoft Bot Framework)


I would like to set up botkit to use the Microsoft Bot Framework.

botkit refers to the yeoman generator on their "Get started" page: https://botkit.ai/getstarted.html

This generator asks an App ID and App Password.

I went to my created channel in azure -> Settings to get my App id. Copied the value from "Microsoft App ID" and hovering over the information icon tells me to click "Manage" to get a password. Clicked "Manage" which leads me to a "Not found" page. (According to old articles this should have worked, i guess this is because of the migration? https://learn.microsoft.com/en-us/azure/bot-service/bot-service-quickstart-registration?view=azure-bot-service-3.0)

afterwards i followed a microsoft blog post about retrieving app passwords here: https://blog.botframework.com/2018/07/03/find-your-azure-bots-appid-and-appsecret/

After entering this in the emulator as explained in the steps. it says: "Unauthorized".

As a messaging endpoint i put my ngrok forwarded url. (retrieved using ./ngrok http 3000)

Is there anything I'm missing? It should be pretty straight forward to get this working normally no?


Solution

  • For Botkit to work with the BotFramework, you only need a few items to be configured. Compare what you have with the below, note any differences, and make the appropriate changes. I have this simple setup working.

    First, this is how you can locate your BotFramework appId & appPassword, if you don't know it.

    Navigate to your Azure bot's resource group. On the left menu pane, select Deployments. enter image description here

    Then select the bot deployment and select Inputs in the menu pane. This will then list your appId and appPassword.

    enter image description here

    Place this code in your index.js file. Be sure to do the necessary npm installs.

    const { MemoryStorage } = require('botbuilder');
    const { Botkit, BotkitBotFrameworkAdapter } = require('botkit');
    const path = require('path');
    
    // Note: Ensure you have a .env file.
    const ENV_FILE = path.join(__dirname, '.env');
    require('dotenv').config({ path: ENV_FILE });
    
    const storage = new MemoryStorage();
    
    const controller = new Botkit({
      adapter: BotkitBotFrameworkAdapter,
      adapterConfig: {
        appId: process.env.MicrosoftAppId,
        appPassword: process.env.MicrosoftAppPassword
      },
      storage: storage,
      webhook_uri: '/api/messages',
    });
    
    controller.hears('hello', 'message', async function(bot, message) {
      await bot.reply(message, 'Hello yourself');
    });
    

    Include your appId and appPassword in a .env file with the following set up.

    MicrosoftAppId=<appId>
    MicrosoftAppPassword=<appPassword>
    

    Run the bot from the project's root directory with node index.js.

    Lastly, in Emulator, set the messaging endpoint to http://localhost:3000/api/messages and include the same 'Microsoft App Id' and 'Microsoft App password' credentials.

    enter image description here

    Once Emulator is setup, test by typing a message. The bot is expecting 'hello' and will only respond to that user input.

    You should be good to go, at this point.

    Hope of help!