创建 Teams 频道对话失败并显示 ConversationNotFound 错误代码

Creating Teams Channel conversation failing with ConversationNotFound error code

我有一个 Bot Framework V3 机器人代码库,它可以在六个左右不同的客户 Teams 租户中运行,并且在我们的内部 Teams 租户中没有问题。

在一个特定的客户租户中,当我调用 ConnectorClient.Conversations.CreateConversationAsync() 时,尝试向 Teams 频道创建主动消息失败并出现 ConversationNotFound 404 错误。

我在频道中创建对话和 post 一个 activity 的代码如下所示:

    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()

非常欢迎任何建议。

我感觉你的 parameters 缺少 Tenant。这可以解释为什么它在某些租户上失败而不在其他租户上失败。尝试这样的事情:

var parameters = new ConversationParameters
{
    Members = new[] { new ChannelAccount(userId) },
    ChannelData = new TeamsChannelData
    {
        Tenant = new TenantInfo(activity.Conversation.TenantId),
    },
};

@Trinetra-MSFT 也是正确的。您不应该对服务进行硬编码 URL;您的一些用户可能不在 /amer.

虽然在某种程度上是可能的,但 "Proactive Messaging" 不应被认为是 "messaging users who have not spoken with the bot",而 "messaging a user about something not related to a previous conversation". Generally speaking, proactive messaging needs to be done by saving a conversation reference 来自机器人过去与之交谈过的用户。这就是 Bot Framework 具体定义主动消息传递的方式。

对于团队,根据 Proactive Messaging for Bots:

Bots can create new conversations with an individual Microsoft Teams user as long as your bot has user information obtained through previous addition in a personal, groupChat or team scope. This information enables your bot to proactively notify them. For instance, if your bot was added to a team, it could query the team roster and send users individual messages in personal chats, or a user could @mention another user to trigger the bot to send that user a direct message.

参见注意:它是为 V4 机器人编写的,因此您可能需要进行一些调整。

如果您 运行 遇到问题请告诉我,我会相应地调整我的答案。