出了点问题,请从 Bot Framework MessagingException 重试

Something went wrong, please try again from BotFramework MessagingExtesion

我一直致力于为连接到我们的应用程序的 MS Teams 实现一个机器人,使用 BotFramework v4 和 C#(Web API 库)。我已经在 azure 中注册了 bot,并成功地让 bot 的聊天部分正常工作。

但是,当我从消息传递扩展程序中搜索项目时,我得到了上述错误 Something went wrong. Please try again. 我已经逐步检查了代码,并且从服务器端代码看来我正在返回正确的响应,但是,当我检查来自 Teams Web 客户端的请求时,响应为空。

从代码的 Bot 端,我实现如下

protected override async Task<MessagingExtensionResponse> OnTeamsMessagingExtensionQueryAsync(ITurnContext<IInvokeActivity> turnContext, MessagingExtensionQuery query, CancellationToken cancellationToken)
{
     var text = query?.Parameters?[0]?.Value as string ?? string.Empty;
     if (text == "true" && query?.Parameters?[0]?.Name == "initialRun")
          return new MessagingExtensionResponse { ComposeExtension = new MessagingExtensionResult { Type = "message", Text = "Please enter your search term above" } };
     else 
          return new MessagingExtensionResponse { ComposeExtension = new MessagingExtensionResult { Type = "message", Text = "Not the initial run" } };
}

和控制器

public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
{
     _adapter = adapter;
     _bot = bot;
}

public async Task Messages()
{
     await _adapter.ProcessAsync(ActionContext.Request, ActionContext.Request.CreateResponse(), _bot);
}

谁能指出我哪里出错的正确方向?

对于遇到同样问题的其他人 运行,如果您正在处理来自 ASP.NET WebApi 的 BotFramework,您需要确保从消息端点正确返回响应。

即控制器中的以下例程

public async Task Messages()
{
     await _adapter.ProcessAsync(ActionContext.Request, ActionContext.Request.CreateResponse(), _bot);
}

需要转成

public async Task<HttpResponseMessage> Messages()
{
     var response = new HttpResponseMessage();
     await _adapter.ProcessAsync(ActionContext.Request, response, _bot);
     return response;
}