Search code examples
botframework

Bot Framework Composer Slack adapter: unhandled error : Unable to get an instance of ConversationState from turnContext


I created a bot with Bot Framework composer with an ejected runtime. I added the slack adapter following this: https://learn.microsoft.com/en-us/azure/bot-service/bot-service-channel-connect-slack?view=azure-bot-service-4.0&tabs=adapter

I was able to make the connection between the slack app and my bot running in my local machine. The /api/messages endpoint still runs just fine, but when talking to the bot from the slack app the following error appears

Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
      [OnTurnError] unhandled error : Unable to get an instance of ConversationState from turnContext.
System.InvalidOperationException: Unable to get an instance of ConversationState from turnContext.
   at Microsoft.Bot.Builder.Dialogs.DialogManager.OnTurnAsync(ITurnContext context, CancellationToken cancellationToken)
   at Microsoft.BotFramework.Composer.Core.ComposerBot.OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken) in pathToBot/runtime/core/ComposerBot.cs:line 88
   at Microsoft.Bot.Builder.MiddlewareSet.ReceiveActivityWithStatusAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken)
   at Microsoft.Bot.Builder.BotAdapter.RunPipelineAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken)

Then it enters a loop in which it keeps throwing the error and a lot of error messages to the slack chat:

The bot encountered an error or bug.

To continue to run this bot, please fix the bot source code.

Then it hits a 429 because of the amount of requests to slack.

The only way I deviated from the instructions is that the latest version of the adapter seems to need a cancelation token in the Process call.

This is what the instructions call for:

await _adapter.ProcessAsync(Request, Response, _bot);

This is what I did:

await _adapter.ProcessAsync(Request, Response, _bot, default(CancellationToken));

Solution

  • I haven't worked with the slack adapter but I had the same issue with the Twilio adapter. The cause was that the adapter was missing setting up the conversation state used by the DialogManager in the adapter.

    You can check this article https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-adaptive-dialog-setup?view=azure-bot-service-4.0 and check the section were the adapter is prepared to work with DialogManager.

    UPDATE

    The key point is adding the storage and state to the adapter, you should have something like this

        public class AdapterWithErrorHandler : BotFrameworkHttpAdapter
        {
            public AdapterWithErrorHandler(
                ICredentialProvider credentialProvider,
                ILogger<BotFrameworkHttpAdapter> logger,
                IStorage storage,
                UserState userState,
                ConversationState conversationState,
                IConfiguration configuration)
                : base(credentialProvider)
            {
                // These three lines are key for DialogManager to work
                this.UseStorage(storage);
                this.UseBotState(userState);
                this.UseBotState(conversationState);
    
                OnTurnError = async (turnContext, exception) =>
                {
                    // Log any leaked exception from the application.
                    logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");
    
                    // Send a message to the user
                    await turnContext.SendActivityAsync("The bot encountered an error or bug.");
                    await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");
    
                    // Send a trace activity, which will be displayed in the Bot Framework Emulator
                    await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
                };
            }
        }
    
    

    You can check the details here: https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-adaptive-dialog-setup?view=azure-bot-service-4.0#add-state