[英]Sending messages to specific users in Teams using Graph API
任何通过 Graph API 向 Teams 中的特定用户发送消息的端点?
(Edited because of clarity and added Custom-Requests) (为了清晰起见进行了编辑并添加了自定义请求)
You can send messages via Graph API to private users BUT there is a problem that you can't create a new chat between two users via the Graph API.您可以通过 Graph API 向私人用户发送消息,但存在一个问题,即您无法通过 Graph API 在两个用户之间创建新聊天。 This means that if you want to send a message from a user to a user, the chat must already exist.
这意味着如果您想从用户向用户发送消息,聊天必须已经存在。 (Messages must first have been exchanged via the MSTeams client for a chat to exist)
(必须首先通过 MSTeams 客户端交换消息才能存在聊天)
So make sure that you have a open chat!所以确保你有一个开放的聊天!
If so, have a look at this MSDoc (This document explains how you can list chats from a user): 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-list?view=graph-rest -beta&tabs=http
After you have all your chats listed, you can have a look at this MSDoc (This document explains how you can send a message to a user): https://docs.microsoft.com/en-us/graph/api/chat-post-messages?view=graph-rest-beta&tabs=http列出所有聊天记录后,您可以查看此 MSDoc(此文档说明了如何向用户发送消息): https ://docs.microsoft.com/en-us/graph/api/chat -post-messages?view=graph-rest-beta&tabs=http
Pay attention to the permissions!注意权限! For sending messages and listing chats there are only delegated permissions so far AND these calls are only available in BETA , so be carefull with it.
对于发送消息和列出聊天记录,到目前为止只有委派权限,并且这些调用仅在BETA 中可用,因此请小心使用。
I can only provide you Java code for an example.我只能为您提供 Java 代码作为示例。
(For everything I do I use ScribeJava to get an Auth-Token) For delegated permissions you need to have a "User-Auth-Token". (对于我所做的一切,我使用 ScribeJava 来获取 Auth-Token)对于委托权限,您需要有一个“User-Auth-Token”。 That means you have to use a Password-Credential-Grant like this:
这意味着您必须像这样使用 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(); }
}
username and password are the credentials from the user you want to send a message (sender).用户名和密码是您要发送消息的用户(发件人)的凭据。
Initial situation初始情况
This is my TeamsClient:这是我的 TeamsClient:
ScribeJava ScribeJava
Get all open chats获取所有打开的聊天记录
("me" in the URL is the user from above (sender).) (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);
}
The response I get from this request looks like this:我从这个请求中得到的响应是这样的:
{"@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"}]}
You can see that I have two open chats and get two entries back from the request.您可以看到我有两个打开的聊天记录,并从请求中获取了两个条目。
Get the right conversatonID获取正确的会话 ID
You have to know that the id can be split in three sections.您必须知道 id 可以分为三个部分。 {JustAPartOfTheId}_{userId}@{EndOfTheId}.
{JustAPartOfTheId}_{userId}@{EndOfTheId}。 The {userId} is the id from your chatpartner (recipient).
{userId} 是您的聊天伙伴(收件人)的 ID。
This is a GraphExplorer response which gives me all users and all informations about them.这是一个 GraphExplorer 响应,它为我提供了所有用户和有关他们的所有信息。
Now you can see that the first ID: "id":"{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces"现在可以看到第一个ID:"id":"{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces"
matches the UserID after the "_".匹配“_”后的用户 ID。
You can split the ID at the "_" filter and find the ID you need.您可以在“_”过滤器处拆分 ID 并找到您需要的 ID。
Send Message to user向用户发送消息
Now you know the right Id and can send a new request for the message like this:现在您知道正确的 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 GraphAPI-Custom-Requests
In the Graph-SDK is no opportunity to use the beta endpoint except for Custom-Requests.在 Graph-SDK 中,除了 Custom-Requests 之外没有机会使用 beta 端点。 (For these requests I also use ScribeJava to get an Auth-Token)
(对于这些请求,我也使用 ScribeJava 来获取 Auth-Token)
Set the BETA-Endpoint设置 BETA 端点
When you want to use the BETA-Endpoint you have to use the setEndpoint() function like this:当您想使用 BETA-Endpoint 时,您必须像这样使用 setEndpoint() 函数:
IGraphServiceClient graphUserClient = _initGraphServiceUserClient();
//Set Beta-Endpoint
graphUserClient.setServiceRoot("https://graph.microsoft.com/beta");
Get all chats获取所有聊天记录
try
{
JsonObject allChats = graphUserClient.customRequest("/me/chats").buildRequest().get();
}
catch(ClientException ex) { ex.printStacktrace(); }
Same response like above和上面一样的回复
Same situation with the userId => split and filter与 userId 相同的情况 => 拆分和过滤
Send message发信息
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();
}
Here is a little GIF where you can see that I didn't type anything.这是一个小 GIF,您可以在其中看到我没有输入任何内容。 I just started my little application and it sends messages automatically.
我刚刚启动了我的小应用程序,它会自动发送消息。
I hope this helps you.我希望这可以帮助你。 Feel free to comment if you don't understand something!
有什么不明白的可以评论留言哦! :)
:)
Best regards!此致!
As of now, We do not have any endpoint to send messages to specific users via Graph API.截至目前,我们没有任何端点可以通过 Graph API 向特定用户发送消息。
You may submit/vote a feature request in the UserVoice or just wait for the update from the Product Team.您可以在 UserVoice 中提交/投票功能请求,也可以等待产品团队的更新。
You can vote for a below feature requests which are already created.您可以为以下已创建的功能请求投票。 All you have to do is enter your email ID and vote.
您所要做的就是输入您的电子邮件 ID 并投票。
https://microsoftteams.uservoice.com/forums/555103-public/suggestions/40642198-create-new-1-1-chat-using-graph-api https://microsoftteams.uservoice.com/forums/555103-public/suggestions/40642198-create-new-1-1-chat-using-graph-api
https://microsoftteams.uservoice.com/forums/555103-public/suggestions/39139705-is-there-any-way-to-generate-chat-id-by-using-grap https://microsoftteams.uservoice.com/forums/555103-public/suggestions/39139705-is-there-any-way-to-generate-chat-id-by-using-grap
Update :更新:
Please find below one more user voice created for the same in Microsoft Graph user voices and vote for it.请在下面找到另一个在 Microsoft Graph 用户语音中为相同内容创建的用户语音并投票。
https://microsoftgraph.uservoice.com/forums/920506-microsoft-graph-feature-requests/suggestions/37802836-add-support-for-creating-chat-messages-on-the-user https://microsoftgraph.uservoice.com/forums/920506-microsoft-graph-feature-requests/suggestions/37802836-add-support-for-creating-chat-messages-on-the-user
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.