【发布时间】:2020-01-22 21:16:27
【问题描述】:
当链接到 Microsoft Teams 的机器人更改某些内容时,我将发送 通知 主动消息。该演示仅使用 (为简单起见) bot 网站上的 API 端点。
开始对话后,我将对话中的内容存储到名为MemoryVariables.ConversationReferences 的属性中(ConcurrentDictionary<string, ConversationReference> 的类型)。
当我浏览到 API 端点时,我看到没有消息但是我得到了 200 响应。在寻找错误时,我在标记的行上添加了一个断点。
public class AdapterWithErrorHandler : BotFrameworkHttpAdapter
{
public AdapterWithErrorHandler(IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger : base(configuration, logger)
{
OnTurnError = async (turnContext, exception) =>
{
logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}"); // <-- breakpoint added here
};
}
}
我看到我有下一个例外:
System.ArgumentNullException: 值不能为空。
参数名称:clientSecret
这是堆栈跟踪:
at Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential..ctor(String clientId, String clientSecret)
at Microsoft.Bot.Connector.Authentication.MicrosoftAppCredentials.<BuildAuthenticator>b__14_0()
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
--- End of stack trace from previous location where exception was thrown ---
at System.Lazy`1.CreateValue()
at Microsoft.Bot.Connector.Authentication.AppCredentials.GetTokenAsync(Boolean forceRefresh)
at Microsoft.Bot.Connector.Authentication.AppCredentials.ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Bot.Connector.Conversations.ReplyToActivityWithHttpMessagesAsync(String conversationId, String activityId, Activity activity, Dictionary`2 customHeaders, CancellationToken cancellationToken)
at Microsoft.Bot.Connector.ConversationsExtensions.ReplyToActivityAsync(IConversations operations, String conversationId, String activityId, Activity activity, CancellationToken cancellationToken)
at Microsoft.Bot.Builder.BotFrameworkAdapter.SendActivitiesAsync(ITurnContext turnContext, Activity[] activities, CancellationToken cancellationToken)
at Microsoft.Bot.Builder.TurnContext.<>c__DisplayClass22_0.<<SendActivitiesAsync>g__SendActivitiesThroughAdapter|1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Bot.Builder.TurnContext.SendActivityAsync(IActivity activity, CancellationToken cancellationToken)
at MyDemoBot.Bot.Controllers.NotifyController.BotCallback(ITurnContext turnContext, CancellationToken cancellationToken) in C:\MyDemoBot\Savaco.KBESAVAC.NotificationBot.Bot\Controllers\NotifyController.cs:line 59
at Microsoft.Bot.Builder.BotFrameworkAdapter.TenantIdWorkaroundForTeamsMiddleware.OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken)
at Microsoft.Bot.Builder.MiddlewareSet.ReceiveActivityWithStatusAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken)
at Microsoft.Bot.Builder.BotAdapter.RunPipelineAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken)
下面是我的NotifyController,在标记行上抛出异常。
[Route("api/notify")]
[ApiController]
public class NotifyController : ControllerBase
{
public async Task<IActionResult> Get()
{
MemoryVariables memoryVariables = MemoryVariables.GetInstance();
List<Task> taskList = new List<Task>();
ICollection<ConversationReference> conversationReferences = memoryVariables.ConversationReferences.Values;
foreach (ConversationReference conversationReference in conversationReferences)
{
taskList.Add(memoryVariables.BotAdapter.ContinueConversationAsync(
conversationReference.Bot.Id,
conversationReference,
BotCallback,
new CancellationToken()
));
}
await Task.WhenAll(taskList); // <-- exceptions throws on this line code
return GetContentResult(new { messagesSend = conversationReferences.Count }, HttpStatusCode.OK);
}
private ContentResult GetContentResult(object content, HttpStatusCode httpStatusCode)
{
return new ContentResult()
{
Content = JsonConvert.SerializeObject(content),
ContentType = "application/json",
StatusCode = (int)httpStatusCode
};
}
private async Task BotCallback(ITurnContext turnContext, CancellationToken cancellationToken)
{
await turnContext.SendActivityAsync(
MessageFactory.SuggestedActions(
new string[] { "Create A", "Create B" },
"Hi, you've received a notification. Choose an action from below to continue."
),
cancellationToken
);
}
}
我是否遗漏了代码中的任何内容,或者我是否遗漏了任何针对 Microsoft Teams 的特定内容?使用 localhost 和 Bot Framework Emulator,我没有问题。
【问题讨论】:
标签: c# botframework microsoft-teams