LoginSignup
4
2

More than 1 year has passed since last update.

Microsoft Graph APIを使ってTeamsの会話のログを取得する

Last updated at Posted at 2022-02-10

ある日の弊社
人事さん「退職予定の人とTeamsのプライベートチャットでやり取りしてたんだけど」
  ぼく「はぁ」
人事さん「退職するとアカウント消えるじゃない?だからログ取りたいの」
  ぼく「そうですか」
人事さん「で、コピペするのが面倒なんだけど、サラッと取得できない?」
  ぼく「(そんなん知らんな・・・)まぁ取り敢えずやってみます」

そんなぼくでも安心。そう、Microsoft Graph Explorerならね。

結論から言うと
https://graph.microsoft.com/v1.0/chats/{chatID}/messages/

で取れました。Graph Explorerの左のpaneからログインして、アクセス許可を適切に与えれば取れます。

Q.プライベートチャットのIDが分からん!

Teamsのプライベートチャットにブラウザでアクセスして出るURLのconversations/の後ろがIDです。

ちなみに

Azureが特に制限無く使える人はAzureでアプリケーション連携するとかの方法もあるみたいですよ?知らんけど。弊社は無理なのでこうしました。
あとGraph Explorerでログイン中に出ているアクセストークンを使えばcurlとかでも情報を取得できます。

curl -H "Authorization: Bearer {tokenの値}" https://graph.microsoft.com/v1.0/me
後日

人事さん「教えてもらった方法やったら途中で切れてたんだけど」
  ぼく「!?」

どうも普通にやると$topが20程度に設定される様です。
https://docs.microsoft.com/ja-jp/graph/api/chat-list-messages?view=graph-rest-1.0&tabs=http

その場合、@odatanextLinkパラメータにURLが設定された状態でjsonが返ってくるようです。
めんどくさいのでjQuery使ってnextLinkが有る場合に再帰で全てのメッセージを取得する様なJS書きました。

function getChatMessage(apiUrl, accessToken){
	$.ajax({
		url: apiUrl,
		headers:{'authorization':'Bearer '+ accessToken},
		type:'GET',
		dataType:'json',
		success: function (results) {
			if(results["@odata.nextLink"] !== undefined){
				getChatMessage(results["@odata.nextLink"],accessToken);
			}
			resultRenderer(results.value);
		},
		error: function (error) {
			console.log("Error in getting data: " + error);
		}
	});
}
function resultRenderer(values){
	//Do Something for Rendering Messages
}

ちなみに

chats/{chatID}/getAllMessages

とかは2022/4/11現在できませんでした。

以上

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2