![](/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.