.net - 在一对一聊天中向租户中的用户发送通知
问题描述
我正在使用 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";
}
从现在开始,我对分散的信息量越来越感到困惑。我看到的是,通常情况应该是:
- 在机器人回调 OnConversationUpdateActivityAsync 上,应保存 ConversationReference,因为它是在安装机器人时触发的。但是这个对象存储在 ConcurrentDictionary 中,并且它的生命周期仅限于运行时。在我的情况下,我应该坚持它,但我找不到要存储的属性以及如何重新创建示例中始终按原样存储的 ConversationReference 对象,但没有持久性示例。
- 鉴于上述问题,我无法在机器人适配器上使用 ContinueConversationAsync 方法。
- 在任何情况下,如果我们查看文档,即使它明确表示要检索此 ChatId,它也会通过使用缓存的 ConversationReference 对象来执行操作。
我应该怎么做这样一个“简单”的事情,比如向一个人发送一条简单的消息,而它看起来很混乱?
解决方案
要保留对话参考属性,请检查Bot State Docs中的存储层概念
您可以继续尝试将此代码示例放在 startup.cs 文件中:
services.AddSingleton<IStorage>(new AzureBlobStorage(Configuration["_connectionstring"], Configuration["BlobName"]));
services.AddSingleton<UserState>();
services.AddSingleton<ConversationState>();
services.AddSingleton<BotStateService>();
另外,请查看主动消息示例代码。
推荐阅读
- cpu-architecture - 基本处理器架构
- selenium-webdriver - 如何在 selenium Web 驱动程序中处理具有相同名称和 id 的表中的多个图像编辑图标
- angularjs - Angular JS 中的路由错误
- javascript - ES6函数解构赋值为对象
- fortran - 错误:f77 程序中有两个主要程序
- ios - 使用 twitter 回调 url 错误登录
- linux - 将第 n+1 行的列合并到第 n 行
- php - html中电话号码的正则表达式
- python - 最高效的 3sum 算法使用 python 进行 leetcode 挑战
- reactjs - React Redux this.props.getClasses 不是函数