![](/img/trans.png)
[英]Sending pro-active messages to Microsoft Teams using Graph API (not Bot Framework)
[英]Sending messages to specific users in Teams using Graph API
任何通过 Graph API 向 Teams 中的特定用户发送消息的端点?
(为了清晰起见进行了编辑并添加了自定义请求)
您可以通过 Graph API 向私人用户发送消息,但存在一个问题,即您无法通过 Graph API 在两个用户之间创建新聊天。 这意味着如果您想从用户向用户发送消息,聊天必须已经存在。 (必须首先通过 MSTeams 客户端交换消息才能存在聊天)
所以确保你有一个开放的聊天!
如果是这样,请查看此 MSDoc(此文档说明了如何列出用户的聊天记录): https ://docs.microsoft.com/en-us/graph/api/chat-list?view=graph-rest -beta&tabs=http
列出所有聊天记录后,您可以查看此 MSDoc(此文档说明了如何向用户发送消息): https ://docs.microsoft.com/en-us/graph/api/chat -post-messages?view=graph-rest-beta&tabs=http
注意权限! 对于发送消息和列出聊天记录,到目前为止只有委派权限,并且这些调用仅在BETA 中可用,因此请小心使用。
我只能为您提供 Java 代码作为示例。
(对于我所做的一切,我使用 ScribeJava 来获取 Auth-Token)对于委托权限,您需要有一个“User-Auth-Token”。 这意味着您必须像这样使用 Password-Credential-Grant:
private void _initOAuth2Service()
{
oAuth2Service = new ServiceBuilder(clientId)
.apiSecret(clientSecret)
.defaultScope(GRAPH_SCOPE)
.build(MicrosoftAzureActiveDirectory20Api.custom(tenantId));
//PASSWORD CREDENTIALS FLOW
try
{
oAuth2Token = oAuth2Service.getAccessTokenPasswordGrant(username, password);
}
catch (IOException e) { e.printStackTrace(); }
catch (InterruptedException e) { e.printStackTrace(); }
catch (ExecutionException e) { e.printStackTrace(); }
}
用户名和密码是您要发送消息的用户(发件人)的凭据。
初始情况
这是我的 TeamsClient:
ScribeJava
获取所有打开的聊天记录
(URL 中的“我”是来自上面的用户(发件人)。)
private Response _executeGetRequest()
{
final OAuthRequest request = new OAuthRequest(Verb.GET, "https://graph.microsoft.com/beta/me/chats");
oAuth2Service.signRequest(oAuth2Token, request);
return oAuth2Service.execute(request);
}
我从这个请求中得到的响应是这样的:
{"@odata.context":"https://graph.microsoft.com/beta/$metadata#chats","value":[{"id":"{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces","topic":null,"createdDateTime":"2020-04-25T09:22:19.86Z","lastUpdatedDateTime":"2020-04-25T09:22:20.46Z"},{"id":"{secondUserChatID}@unq.gbl.spaces","topic":null,"createdDateTime":"2020-03-27T08:19:29.257Z","lastUpdatedDateTime":"2020-03-27T08:19:30.255Z"}]}
您可以看到我有两个打开的聊天记录,并从请求中获取了两个条目。
获取正确的会话 ID
您必须知道 id 可以分为三个部分。 {JustAPartOfTheId}_{userId}@{EndOfTheId}。 {userId} 是您的聊天伙伴(收件人)的 ID。
这是一个 GraphExplorer 响应,它为我提供了所有用户和有关他们的所有信息。
现在可以看到第一个ID:"id":"{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces"
匹配“_”后的用户 ID。
您可以在“_”过滤器处拆分 ID 并找到您需要的 ID。
向用户发送消息
现在您知道正确的 Id 并且可以像这样发送消息的新请求:
final OAuthRequest request = new OAuthRequest(Verb.POST, "https://graph.microsoft.com/beta/chats/{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces/messages");
oAuth2Service.signRequest(oAuth2Token, request);
request.addHeader("Accept", "application/json, text/plain, */*");
request.addHeader("Content-Type", "application/json");
request.setPayload("{\"body\":{\"content\":\" " + "Hi Hi Daniel Adu-Djan" + "\"}}");
oAuth2Service.execute(request);
GraphAPI-Custom-Requests
在 Graph-SDK 中,除了 Custom-Requests 之外没有机会使用 beta 端点。 (对于这些请求,我也使用 ScribeJava 来获取 Auth-Token)
设置 BETA 端点
当您想使用 BETA-Endpoint 时,您必须像这样使用 setEndpoint() 函数:
IGraphServiceClient graphUserClient = _initGraphServiceUserClient();
//Set Beta-Endpoint
graphUserClient.setServiceRoot("https://graph.microsoft.com/beta");
获取所有聊天记录
try
{
JsonObject allChats = graphUserClient.customRequest("/me/chats").buildRequest().get();
}
catch(ClientException ex) { ex.printStacktrace(); }
和上面一样的回复
与 userId 相同的情况 => 拆分和过滤
发信息
IGraphServiceClient graphUserClient = _initGraphServiceUserClient();
//Set Beta-Endpoint again
graphUserClient.setServiceRoot("https://graph.microsoft.com/beta");
try
{
JsonObject allChats = graphUserClient.customRequest("/chats/{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces/messages").buildRequest().post({yourMessageAsJsonObject});
}
catch(ClientException ex) { ex.printStacktrace();
}
这是一个小 GIF,您可以在其中看到我没有输入任何内容。 我刚刚启动了我的小应用程序,它会自动发送消息。
我希望这可以帮助你。 有什么不明白的可以评论留言哦! :)
此致!
截至目前,我们没有任何端点可以通过 Graph API 向特定用户发送消息。
您可以在 UserVoice 中提交/投票功能请求,也可以等待产品团队的更新。
您可以为以下已创建的功能请求投票。 您所要做的就是输入您的电子邮件 ID 并投票。
更新:
请在下面找到另一个在 Microsoft Graph 用户语音中为相同内容创建的用户语音并投票。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.