【问题标题】:Send the message to personal chat instead of group chat using ms teams chatbot使用 ms 团队聊天机器人将消息发送到个人聊天而不是群聊
【发布时间】:2021-10-26 05:08:57
【问题描述】:

我想在个人聊天而不是群聊中发布消息,而从群聊中发布消息如何实现? 有什么方法可以解决这个问题吗?

【问题讨论】:

  • 个人聊天是可能的,有一个概念称为主动消息传递更好,请看一下。
  • @Siva - 您的问题是否已解决或仍在寻求帮助?

标签: node.js botframework chatbot microsoft-teams adaptive-cards


【解决方案1】:

@Siva - 您可以向用户发布主动的个人消息: 请在下面找到示例代码:

另外,在运行此代码之前,请确保用户已在个人范围内安装了机器人应用程序,或者是安装了机器人的团队的成员。

参考文档: https://docs.microsoft.com/en-us/java/api/com.microsoft.cognitiveservices.speech.transcription.conversation.createconversationasync?view=azure-java-stable

using Microsoft.Bot.Builder;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Schema.Teams;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Teams.Bot.Conversations
{
    class Program
    {
        public static async Task Main(string[] args)
        {
            //Teams internal id
            string teamInternalId = "19:96391bb270744e218c04dc8f571d3d8b@thread.skype";

            //The Bot Service Url needs to be dynamically stored and fetched from the Team. Recommendation is to store the serviceUrl from the bot Payload and later re-use it to send proactive messages.
            string serviceUrl = "https://smba.trafficmanager.net/emea/";

            //the upn of the user who should recieve the personal message
            string mentionUserPrincipalName = "user@tenant.onmicrosoft.com";

            //Office 365/Azure AD tenant id for the 
            string tenantId = "<tenant-GUID>";

            //From the Bot Channel Registration
            string botClientID = "<client-id>";
            string botClientSecret = "<client-secret>";

            var connectorClient = new ConnectorClient(new Uri(serviceUrl), new MicrosoftAppCredentials(botClientID, botClientSecret));

            var user = await ((Microsoft.Bot.Connector.Conversations)connectorClient.Conversations).GetConversationMemberAsync(mentionUserPrincipalName, teamInternalId, default);


            var personalMessageActivity = MessageFactory.Text($"Personal message from the Bot!");

            var conversationParameters = new ConversationParameters()
            {
                ChannelData = new TeamsChannelData
                {
                    Tenant = new TenantInfo
                    {
                        Id = tenantId,
                    }
                },
                Members = new List<ChannelAccount>() { user }
            };

            var response = await connectorClient.Conversations.CreateConversationAsync(conversationParameters);

            await connectorClient.Conversations.SendToConversationAsync(response.Id, personalMessageActivity);
        }
    }
}

【讨论】:

    【解决方案2】:

    正如其他人在此线程中所指出的,您需要使用一种称为“主动消息传递”的东西。不过,我看到您使用的是 node.js,所以这里有一个比 C# 或 Java 人员已经发布的更好的示例:https://github.com/pnp/teams-dev-samples/tree/main/samples/bot-proactive-messaging。我在示例中同时放置了 dotnet 和 node.js 版本,页面底部有一些链接可以阅读有关该主题的更多信息。这里还有一个视频会议的链接,我在其中更多地讨论了这个概念:https://www.youtube.com/watch?v=mM7-fYdcJhw&t=1398s

    重要的是要知道,只有当您已经与用户建立了“上下文”时,主动消息传递才会起作用,这基本上意味着他们必须已经安装了您的应用程序。不过,可以代表他们预先安装它。您需要使用 Graph 来执行此操作,您可以在此处阅读更多信息:https://docs.microsoft.com/en-us/graph/api/userteamwork-post-installedapps?view=graph-rest-1.0&tabs=http。

    【讨论】:

      猜你喜欢
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      相关资源
      最近更新 更多