【问题标题】:Initiate Proactive Conversation with Bot Framework for Microsoft Teams without Teams-Context possible?可以在没有 Teams-Context 的情况下使用 Microsoft Teams 的 Bot Framework 发起主动对话?
【发布时间】:2019-06-17 20:48:43
【问题描述】:

我基本上是在尝试与 Microsoft Bot Framework 进行主动对话。我使用以下代码成功为 Skype for BUsiness 这样做:

string serviceUrl = "https://api.skypeforbusiness.com/platformservice/botframework";
MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);

var connector = new ConnectorClient(new Uri(serviceUrl), "{app-id}", "{app-password}");

var user = new ChannelAccount("any-user-upn@domain.com", "Name of User");
var bot = new ChannelAccount("bot-upn@campana-schott.com", "Name of bot");

ConversationParameters cpMessage = new ConversationParameters(true, bot, new List<ChannelAccount> { user }, "Topic of the conversation");

ConversationResourceResponse response = await connector.Conversations.CreateConversationAsync(cpMessage);

启动对话后,我可以向用户发送消息,效果很好。但对于 MS Teams,我有两个问题: 1. 哪个 serviceURL 是正确的?我发现了一些提示,包括以“https://smba.trafficmanager.net ...”开头的 url,但它们都不起作用,我总是收到异常“Bad Request”。

第二个问题:我是否需要 Teams-Context(Teams ID)来实际发布消息,或者是否可以(如我使用 Skype 的示例)只与用户发起聊天?

编辑:我尝试添加 channelData (teamsChannelId) 并使用服务 URL“https://smba.trafficmanager.net/emea-client-ss.msg” - 然后我得到响应代码“禁止”。 这让我想到了一个问题:实际上是否可以在没有任何 Team-Context(只是聊天)的情况下发起与团队的对话?

提前致谢, 马丁

【问题讨论】:

  • 确定一下:bot v3 还是 v4?
  • 我不在乎哪个版本,我尝试了 v3 和 v4 - 现在看我的下一个答案,我想我找到了为什么我的代码不起作用的解决方案以及为什么基本用例永远不会的答案工作。

标签: azure botframework


【解决方案1】:

@kolbi 理想的解决方案是使用 Botframework V4,这是一种类似于您尝试实现但使用 Botframework V4 版本编写的方法。 Here is a detailed blog post about accomplishing this(发起对话,主动回复特定对话)

            var channelData = turnContext.Activity.GetChannelData<TeamsChannelData>();

            var conversationParameter = new ConversationParameters
            {
                Bot = turnContext.Activity.Recipient,
                IsGroup = true,
                ChannelData = channelData,
                TenantId = channelData.Tenant.Id,
                Activity = MessageFactory.Text(message)
            };
            var response = await _client.Conversations.CreateConversationAsync(conversationParameter);

【讨论】:

  • 谢谢您,但这并不能解决我的问题,因为该解决方案仅针对 Channell-Conversations,而不是直接与用户聊天。与此同时,我与 Microsoft Teams 开发人员取得了联系,他们确认目前这是故意不可能的,但他们正在为未来的版本努力。
  • 即使在仅将自己限制在频道之后,只有当用户在机器人启动后至少发起一次针对机器人的对话时,我们才会收到 referenceturnContext。所以我们不能指望机器人在重启后启动频道消息。
【解决方案2】:

所以,我想我自己找到了答案:

以下代码适用于 MS Teams 主动编写(机器人服务 v3):

string serviceUrl = "https://smba.trafficmanager.net/emea/";
MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);

var connector = new ConnectorClient(new Uri(serviceUrl), botAppId, appSecret);
var teamsBotAccount = new ChannelAccount("28:<BOT-APP-ID>", "Smart Office Bot");
var teamsUserAccount = new ChannelAccount("29:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_x", "Martin");

ConversationResourceResponse response = connector.Conversations.CreateOrGetDirectConversation(teamsBotAccount, teamsUserAccount, "9e73e135-fe7a-447f-baba-b0312d3aa55d");

关键点是要向其发送消息的用户的 ChannelAccount ID。我的第一个误解是,MS Teams 的频道帐户 ID 也适用于电子邮件 - 事实并非如此。 MS 团队的 ChannelAccounts 是神秘的,而且是特定于频道的。

这意味着:在 ChannelAccount 中用于消息接收者的用户 ID 仅存在于 Channel Context 中。对于直接 1:1 聊天,我的用户似乎在每次聊天中都有不同的 ID。

我的结论:问题不在于 Bot Framework,而在于 MS Teams,它根本不支持从自动化组件发起聊天。正如我通过其他类似线程得出的结论,故意不支持防止垃圾邮件(不幸的是,它非常不一致 - 这显然不适用于 Skype for Business)。

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 2023-03-11
    • 2017-09-26
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2019-02-04
    相关资源
    最近更新 更多