是否可以从外部渠道向Microsoft团队发送消息

时间:2019-12-09 04:21:19

标签: c# botframework microsoft-teams microsoft-graph-teams

我有一个MS小组机器人。现在,我需要向用户介绍这一点。因此,该机器人应该能够将消息发送给在Microsoft Teams中自我介绍的一组特定用户。 我有数据库中的人员列表。现在,可以向团队中的所有人发送消息了。如果是,怎么办?

1 个答案:

答案 0 :(得分:0)

我还没有尝试过,但是大概管理员可以为用户安装该应用程序。此时,机器人应该收到conversationUpdate,并且机器人应该能够在事件处理程序中向用户发送消息。

正如@ Abhijit-MSFT在评论中提到的,以下是使用Graph为用户添加应用的示例:https://github.com/microsoftgraph/contoso-airlines-teams-sample/blob/283523d45f5ce416111dfc34b8e49728b5012739/project/Models/GraphService.cs#L176

        public async Task InstallAppToAllUsers()
        {
            string appid = "f46ad259-0fe5-4f12-872d-c737b174bcb4"; // Adobe

            var graph = GetAuthenticatedClient();
            var users = await graph.Users.Request().Select("id,displayName").GetAsync();
            foreach (User user in users)
            {
                if (user.DisplayName.StartsWith("Megan"))
                {
                    // Check if the app is already installed for that user.
                    // Use $expand to populate the teamsAppDefinition property,
                    // and $filter to search for the one app we care about
                    TeamsAppInstallation[] installs = await HttpGetList<TeamsAppInstallation>(
                        $"/users/{user.Id}/teamwork/installedApps?$expand=teamsAppDefinition&$filter=teamsAppDefinition/teamsAppId eq '{appid}'", 
                        endpoint: graphBetaEndpoint);
                    if (installs.Length == 0)
                    {
                        // install app
                        await HttpPost($"/users/{user.Id}/teamwork/installedApps",
                                new TeamsAppInstallation()
                                {
                                    AdditionalData = new Dictionary<string, object>() { ["teamsApp@odata.bind"] = $"{graphBetaEndpoint}/appCatalogs/teamsApps/{appid}" }
                                },
                                endpoint: graphBetaEndpoint);
                        // Bot will get a notification about the new user and the chat thread ID for that user
                    }

                    // If you've forgotten the chat thread ID for that user, you can find it again:
                    var chats = await HttpGetList<Chat>($"/users/{user.Id}/chats?$filter=installedApps/any(a:a/teamsApp/id eq '{appid}')", endpoint: graphBetaEndpoint);
                    string threadId = chats[0].Id;

                    // Wait a little before installing the next app to avoid throttling
                    Thread.Sleep(1000); 
                }
            }
        }