首页 > 解决方案 > 在一对一聊天中向租户中的用户发送通知

问题描述

我正在使用 ASP.NET Core、Graph SDK(来自 Microsoft.Identity.Web.MicrosoftGraphBeta)在 Microsoft Teams 中为我的组织开发通知机器人。我很难为租户中的特定用户开发一个简单的通知系统。这些用户由电子邮件地址标识。我已经通过以下方式为用户和团队实现了应用程序的主动安装:

接收消息以在 MS Teams 中发送给特定用户的控制器

[HttpPost]
[Route("notify-approver")]
public async Task<List<string>> NotifyApprover([FromBody] ApproverMessage approverMessage)
{
            var approvers = await _graphServiceClient.Users.Request().Filter($"mail eq '{approverMessage.Email}'").GetAsync();

            var targetApprover = approvers.CurrentPage.ToList().FirstOrDefault();

            var appInstallation= await _graphServiceClient.InstallIfNotAlreadyForUser(targetApprover.Id, _configuration.GetTeamsAppId());


            // PER IL MOMENTO SEMBRA BUG
            var chatId = await _graphServiceClient.GetChatThreadId(targetApprover.Id, appInstallation.Id)

        }

安装工作正常并返回 UserScopeTeamsAppInstallation。这是我为主动安装而构建的功能:

public static async Task<UserScopeTeamsAppInstallation> InstallIfNotAlreadyForUser(this IGraphServiceClient graphServiceClient, string userId, string teamsAppId)
        {
            var appsCollectionPage = await graphServiceClient.Users[userId].Teamwork.InstalledApps.Request().Expand("teamsAppDefinition")
                .GetAsync();
            var appInstallation = appsCollectionPage.CurrentPage.ToList()
                .Find(a => a.TeamsAppDefinition.TeamsAppId.Equals(teamsAppId));

            if (appInstallation != null) return appInstallation;
            var userScopeTeamsAppInstallation = new UserScopeTeamsAppInstallation()
            {
                AdditionalData = new Dictionary<string, object>
                {
                    {"teamsApp@odata.bind", $"https://graph.microsoft.com/beta/appCatalogs/teamsApps/{teamsAppId}"}
                }
            };
            var freshInstallation = await graphServiceClient.Users[userId].Teamwork.InstalledApps.Request().AddAsync(userScopeTeamsAppInstallation);
            return freshInstallation;

        }

到现在还可以,但不好的是在试用中主动发送消息;正如文档中所建议的那样,我继续获取应该有用的这个 chatId。有趣的是,如果我使用 Graph Client v1.0 在检索下面的聊天对象时出现内部服务器错误,然后我切换到 Beta 客户端,它......有点工作。

public static async Task<string> GetChatThreadId(this IGraphServiceClient graphServiceClient, string userId, string teamsAppIdInstallation)
        {
            // forse ci vuole AppInstallationId anziché il semplice teamsAppId della app

            //var chat = await graphServiceClient.Users[userId].Teamwork.InstalledApps[teamsAppIdInstallation].Chat.Request().GetAsync();

            /*
             * Perché dá internal server error?? Possibile bug della API di graph
             * perché viceversa da graph explorer funziona e restituisce l'id della chat
             *
             * Per adesso sono costretto a salvarmi in un database tutte le conversation references.
             */

            var chat =  graphServiceClient.Users[userId].Teamwork.InstalledApps[teamsAppIdInstallation].Chat.Request().GetAsync();


            return chat.Id.ToString();
            //return "CIAONE";
        }

GetChatThreadId 函数中获取聊天对象 从现在开始,我对分散的信息量越来越感到困惑。我看到的是,通常情况应该是:

我应该怎么做这样一个“简单”的事情,比如向一个人发送一条简单的消息,而它看起来很混乱?

标签: .netbotframeworkmicrosoft-teamsmicrosoft-graph-sdksmicrosoft-graph-teams

解决方案


要保留对话参考属性,请检查Bot State Docs中的存储层概念

您可以继续尝试将此代码示例放在 startup.cs 文件中:

services.AddSingleton<IStorage>(new AzureBlobStorage(Configuration["_connectionstring"], Configuration["BlobName"]));
services.AddSingleton<UserState>();
services.AddSingleton<ConversationState>();
services.AddSingleton<BotStateService>(); 

另外,请查看主动消息示例代码。


推荐阅读