Search code examples
botframeworkazure-bot-servicedirect-line-botframework

Directline/Web chat channel error: HTTP status code Forbidden


I am facing issue while sending message in my Chatbot. I had taken the below steps to create the Chatbot.

  • Created the bot using Bot framework SDK v-4 and published to Azure
  • Created the Bot Channels Registrations and registered the App ID
  • Included the AppID and password for the bot channel in my Bot service API's app settings in Azure
  • Created a web client using botchat-es5.js (es-5 version)

When I type and send a message (using the web client) I get below error (with code error 502) on my browser console.

POST https://directline.botframework.com/v3/directline/conversations/7IqYgcAzBp4EObQvFzK2fF/activities 502 (Bad Gateway)

The Directline and Web chat channel logs shows the error message as

There was an error sending this message to your bot: HTTP status code Forbidden

Below is the request header details.

enter image description here

Below is the Bot config (.bot) file details. Note, the App ID and Password is blank, if that's the issue?

{
  "name": "Chatbot",
  "services": [
    {
      "type": "endpoint",
      "name": "development",
      "endpoint": "http://localhost:3978/api/messages",
      "appId": "",
      "appPassword": "",
      "id": "1"
    },
    {
      "type": "endpoint",
      "name": "production",
      "endpoint": "https://<my_app_name>.azurewebsites.net/api/messages",
      "appId": "<YOUR APP ID>",
      "appPassword": "<YOUR APP PASSWORD>",
      "id": "2"
    }
  ],
  "padlock": "",
  "version": "2.0"
}

I tried re-creating all of these on a different subscription but I get same error. I have another bot which is up and running and if I use the Directline channel key of that bot in the same web client, it works fine.

I looked for some online references but those didn't help. Can someone please help me if I am missing something? Let me know if I can provide more details.


Solution

  • If you're bot is using the integration library, the startup code using a .bot file generally has this (among other things):

    var secretKey = Configuration.GetSection("botFileSecret")?.Value;
    var botFilePath = Configuration.GetSection("botFilePath")?.Value;
    
    BotConfiguration botConfig = BotConfiguration.Load(botFilePath, secretKey);
    
    var environment = _isProduction ? "production" : "development";
    var service = botConfig.Services.FirstOrDefault(s => s.Type == "endpoint" && s.Name == environment);
    
    services.AddBot<BasicBot>(options =>
    {
        options.CredentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);
    
        //other code
    }
    

    Notice the credential provider created from bot file AppId and AppPassword. This appid and apppassword can instead be retrieved from App Settings with something like:

    options.CredentialProvider =  new SimpleCredentialProvider(Configuration[MicrosoftAppCredentials.MicrosoftAppIdKey],
                                                          Configuration[MicrosoftAppCredentials.MicrosoftAppPasswordKey]);
    

    Note: you're endpoint should also be correct:

    "endpoint": "https://<my_app_name>.azurewebsites.net/api/messages",