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

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

我使用 Bot Framework composer 创建了一个带有弹出运行时的机器人。我在下面添加了松弛适配器:https://docs.microsoft.com/en-us/azure/bot-service/bot-service-channel-connect-slack?view=azure-bot-service-4.0&tabs=adapter

我能够在我的本地机器上建立 slack 应用程序和我的 bot 运行 之间的连接。 /api/messages 端点仍然运行良好,但是当从 slack 应用程序与机器人对话时,出现以下错误

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)

然后它进入一个循环,在这个循环中它不断地向 slack chat 抛出错误和大量错误消息:

The bot encountered an error or bug.

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

然后由于请求松弛的数量而达到 429。

我偏离说明的唯一方法是最新版本的适配器似乎需要在 Process 调用中使用取消标记。

这是说明要求的内容:

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

这是我所做的:

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

我没有使用过松弛适配器,但我在使用 Twilio 适配器时遇到了同样的问题。原因是适配器缺少设置适配器中 DialogManager 使用的对话状态。

您可以查看这篇文章 https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-adaptive-dialog-setup?view=azure-bot-service-4.0 并查看适配器准备与 DialogManager 一起工作的部分。

更新

关键是给适配器添加存储和状态,你应该有这样的东西

    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");
            };
        }
    }

您可以在这里查看详细信息:https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-adaptive-dialog-setup?view=azure-bot-service-4.0#add-state