我有一个Bot Framework V3机器人代码库,可以在大约六个不同的客户团队租户中使用,并且在我们内部的团队租户中也可以正常工作。
在一个特定的客户租户中,当我调用ConnectorClient.Conversations.CreateConversationAsync()时,尝试向Teams Channel创建主动消息的尝试失败,并显示ConversationNotFound 404错误。
我用于创建对话并在频道中发布活动的代码如下:
var teamsChannelId = "19:deadbeef1234@thread.skype"; // insert the real channel ID obtained from lookups against Graph API...
var botCredentials = new MicrosoftAppCredentials(/* Bot ID & password */);
MicrosoftAppCredentials.TrustServiceUrl("https://smba.trafficmanager.net/amer/", DateTime.MaxValue);
using (var connectorClient = new ConnectorClient(new Uri("https://smba.trafficmanager.net/amer/"), botCredentials)) {
var botId = new ChannelAccount("28:" + botCredentials.MicrosoftAppId);
var msg = Activity.CreateMessageActivity();
msg.From = botId;
var card = MakeCard(); // builds an AdaptiveCard...
msg.Attachments.Add(new Attachment(AdaptiveCard.ContentType, content: card));
var parameters = new ConversationParameters() {
Bot = botId,
ChannelData = new TeamsChannelData() {
Channel = new ChannelInfo(teamsChannelId)
},
Activity = (Activity)msg
};
// This throws an Microsoft.Bot.Connector.ErrorResponseException with the code "ConversationNotFound"
ConversationResourceResponse convoResponse = await connectorClient .Conversations.CreateConversationAsync(parameters);
}
正如我最初提到的那样,此代码可能并不完美,但它可在许多不同的团队和Azure环境中工作,但在此特定环境中会失败。 Bot Framework的HTTP响应如下所示:
"Response": {
"StatusCode": 404,
"ReasonPhrase": "Not Found",
"Content": "{\"error\":{\"code\":\"ConversationNotFound\",\"message\":\"Conversation not found.\"}}",
"Headers": {
"Date": [
"Wed, 04 Sep 2019 14:43:24 GMT"
],
"Server": [
"Microsoft-HTTPAPI/2.0"
],
"Content-Length": [
"77"
],
"Content-Type": [
"application/json; charset=utf-8"
]
}
堆栈跟踪:
Microsoft.Bot.Connector.ErrorResponseException: Operation returned an invalid status code 'NotFound'
at Microsoft.Bot.Connector.Conversations.<CreateConversationWithHttpMessagesAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Bot.Connector.ConversationsExtensions.<CreateConversationAsync>d__3.MoveNext()
任何建议都将受到欢迎。
答案 0 :(得分:1)
我觉得您的parameters
缺少Tenant
。这可以解释为什么某些租户而不是其他租户失败。尝试这样的事情:
var parameters = new ConversationParameters
{
Members = new[] { new ChannelAccount(userId) },
ChannelData = new TeamsChannelData
{
Tenant = new TenantInfo(activity.Conversation.TenantId),
},
};
@ Trinetra-MSFT也正确。您不应该对服务URL进行硬编码。您的某些用户可能不在/amer
之外。
尽管在某种程度上可行,但不应将“主动消息传递”视为“与未与机器人对话的用户的消息传递”,而应视为"messaging a user about something not related to a previous conversation"。一般而言,主动式消息传递需要由机器人与之进行过对话的用户saving a conversation reference完成。 Bot框架就是这样专门定义主动消息的。
对于团队,按Proactive Messaging for Bots:
只要您的机器人在个人,groupChat或团队范围内具有通过先前添加获得的用户信息,机器人就可以与单个Microsoft Teams用户创建新的对话。该信息使您的机器人可以主动通知他们。例如,如果您的机器人被添加到团队中,它可以查询团队花名册并在个人聊天中向用户发送个人消息,或者用户可以@提及其他用户以触发该机器人向该用户发送直接消息。
请参见this SO answer for additional help。 注意:它是为V4机器人编写的,因此您可能需要进行一些调整。
如果您遇到问题,请告诉我,我会相应地调整答案。