前兩篇文章介紹了如何對用戶的在線狀態和通話記錄進行數據統計。這篇文章我們來看看如何統計用戶的聊天消息。
在介紹具體 api 如何調用前,我們可以先看一下 Teams 里對于 Message 的層級結構,在 Teams 里,message有兩種,一種是 Team 的 Channel 的 message,一種是對一對聊天或者群聊里的 message。他們的層級設計是不同的。
Channel 里的消息
它的層次結構如下:
Team -> Channel -> Message -> Reply 一個 tenant 里有多個 Team,每個 Team 里可以有多個 Channel,每個 Channel 里有多個 Message,每個 Message 可以有 0 到 n 個 reply消息。
所以我們使用 graph api 來獲取信息的時候,我們先獲取 teams 列表,因為 graph api目前沒有一個簡單的獲取 team 列表的接口,所以必須要用下面這個獲取 group 列表的接口,再加上 filter。這是一個已知的問題,希望以后能被fix掉。
GET /groups?$filter=resourceProvisioningOptions/Any(x:x eq 'Team')
Response:
{"@odata.context": "https://graph.microsoft.com/beta/$metadata#groups","value": [{"id": "02bd9fd6-8f93-4758-87c3-1fb73740a315","displayName": "HR Taskforce","visibility": "Private"...},...]
}
我們有了 team 列表后就可以開始獲取每個 team 的 channel。使用如下接口:
GET https://graph.microsoft.com/beta/teams/{team-id}/channels
Response:
{"value": [{"description": "description-value","displayName": "display-name-value","id": "02bd9fd6-1111-4758-87c3-1fb73740a315",....},...]
}
有了 channel 后,我們就可以獲取 channel 里的 message 列表。
GET https://graph.microsoft.com/beta/teams/{team-id}/channels/{channel-id}/messages
Response:
{"value": [{"id": "1555375673184","messageType": "message","createdDateTime": "2019-04-16T00:47:53.184Z","lastModifiedDateTime": "2019-05-04T19:58:15.511Z","lastEditedDateTime": null,"deletedDateTime": null,"subject": "","summary": null,"importance": "normal","locale": "en-us","from": {"user": {"id": "bb8775a4-4d8c-42cf-a1d4-4d58c2bb668f","displayName": "Adele Vance","userIdentityType": "aadUser"}},"body": {"contentType": "html","content": "<div><div>Nice to join this team. <at id=\"0\">Megan Bowen</at>, have we got the March report ready please?</div>\n</div>"},"attachments": [],"mentions": [{"mentionText": "Megan Bowen","mentioned": {"user": {"id": "5d8d505c-864f-4804-88c7-4583c966cde8","displayName": "Megan Bowen","userIdentityType": "aadUser"}}}],"reactions": []},...]
}
大家可以發現上面有用的信息非常多,比如:
createdDateTime
,?lastModifiedDateTime
,?lastEditedDateTime
?和?deletedDateTime
,各種時間from
:message是誰發的mentions
:消息里有沒有 @ 其他人reactions
:消息有沒有被點贊,誰在什么時候點了贊或者點了什么其他表情
由于一個channel里的 message 會很多,所以 graph api 還有一個分批獲取 message 的接口,如果對這個接口感興趣,可以參考這個文檔:https://docs.microsoft.com/en-us/graph/api/chatmessage-delta?view=graph-rest-beta&tabs=http
GET /teams/{team-id}/channels/{channel-id}/messages/delta
有了 message 后就可以獲取每個 message 的replies 了。
GET /teams/{team-id}/channels/{channel-id}/messages/{message-id}/replies
對一對聊天或者群聊里的 message
它的層次結構比較簡單,如下:
User -> Chat -> Message
我們先需要枚舉當前 tenant 下的所有的 user,然后對每一個 user 調用下面的接口來獲取這個用戶的聊天。
GET https://graph.microsoft.com/beta/users/{user-id}/chats
Response:
{"value": [{"id": "19:meeting_MjdhNjM4YzUtYzExZi00OTFkLTkzZTAtNTVlNmZmMDhkNGU2@thread.v2","topic": "Meeting chat sample","createdDateTime": "2020-12-08T23:53:05.801Z","lastUpdatedDateTime": "2020-12-08T23:58:32.511Z","chatType": "meeting"},{"id": "19:561082c0f3f847a58069deb8eb300807@thread.v2","topic": "Group chat sample","createdDateTime": "2020-12-03T19:41:07.054Z","lastUpdatedDateTime": "2020-12-08T23:53:11.012Z","chatType": "group"},{"id": "19:d74fc2ed-cb0e-4288-a219-b5c71abaf2aa_8c0a1a67-50ce-4114-bb6c-da9c5dbcf6ca@unq.gbl.spaces","topic": null,"createdDateTime": "2020-12-04T23:10:28.51Z","lastUpdatedDateTime": "2020-12-04T23:10:36.925Z","chatType": "oneOnOne"}]
}
可以看到,上面的接口返回了各種對話類型 (chatType):一對一聊天?oneOnOne
?,群聊?group
?,和會議里的聊天?meeting
。有了chat列表后,我們就能對每一個chat來獲取 message。
GET https://graph.microsoft.com/beta/users/{user-id}/chats/{chat-id}/messages
這個接口返回的內容和channel message返回的內容類似。
看到這里想必大家已經發現了 teams 的強大,和 graph api 的開放性,只要 app 有對應的權限,基本就能拿到任何數據,有了數據后,我們的統計報表就簡單了。