上篇文章介紹了如何獲取用戶的在線狀態,這篇文章我們記錄介紹如何統計用戶通話記錄。
首先,Teams為了安全,它要求 app 要有?CallRecords.Read.All
?權限。然后就可以通過這個api來獲取 call record。
GET /communications/callRecords/{id}
這個接口會返回類似如下的數據:
{"@odata.context": "https://graph.microsoft.com/beta/$metadata#communications/callRecords/$entity","version": 1,"type": "groupCall","modalities": ["audio"],"lastModifiedDateTime": "2020-12-25T19:00:24.582757Z","startDateTime": "2020-12-25T18:52:21.321Z","endDateTime": "2020-12-25T19:52:46.123Z","id": "e523d2ed-1111-4b6b-925b-754a88034cc5","organizer": {"user": {"id": "821809f5-0000-0000-0000-3b5136c0e777","displayName": "Abbie Wilkins","tenantId": "dc368399-474c-4d40-900c-6265431fd81f"}},"participants": [{"user": {"id": "821809f5-0000-0000-0000-3b5136c0e777","displayName": "Abbie Wilkins","tenantId": "dc368399-474c-4d40-900c-6265431fd81f"}},...]
}
可以看到,我們能通過?organizer
?和?participants
?來找到參與這個通過的參與人,我們可以記錄下用戶的 user id,同時我們還可以通過?startDateTime
?和?endDateTime
?來取得這次 call 的開始和結束時間。
實際上 Teams 里的 call record 比我們想象的復雜很多,一個call里可能有多個 session 組成,我們用一張官方的數據模型圖看一下。
?
我們可以給上面這個 api 加上參數來獲取 session。
GET https://graph.microsoft.com/beta/communications/callRecords/{id}?$expand=sessions
{"@odata.context": "https://graph.microsoft.com/beta/$metadata#communications/callRecords(sessions(segments()))/$entity","startDateTime": "2020-02-25T18:52:21.2169889Z","endDateTime": "2020-02-25T18:52:46.7640013Z","organizer": { ... },"participants": [ ... ],"sessions": [{"modalities": ["audio"],"startDateTime": "2020-02-25T18:52:21.2169889Z","endDateTime": "2020-02-25T18:52:46.7640013Z","id": "e523d2ed-2966-4b6b-925b-754a88034cc5","caller": {"@odata.type": "#microsoft.graph.callRecords.participantEndpoint","userAgent": {"@odata.type": "#microsoft.graph.callRecords.clientUserAgent","headerValue": "RTCC/7.0.0.0 UCWA/7.0.0.0 AndroidLync/6.25.0.27 (SM-G930U Android 8.0.0)","platform": "android","productFamily": "skypeForBusiness"},"identity": {"@odata.type": "#microsoft.graph.identitySet","user": {"id": "821809f5-0000-0000-0000-3b5136c0e777","displayName": "Abbie Wilkins","tenantId": "dc368399-474c-4d40-900c-6265431fd81f"}}},"callee": {"@odata.type": "#microsoft.graph.callRecords.participantEndpoint","userAgent": {"@odata.type": "#microsoft.graph.callRecords.clientUserAgent","headerValue": "UCCAPI/16.0.12527.20122 OC/16.0.12527.20194 (Skype for Business)","platform": "windows","productFamily": "skypeForBusiness"},"identity": {"user": {"id": "f69e2c00-0000-0000-0000-185e5f5f5d8a","displayName": "Owen Franklin","tenantId": "dc368399-474c-4d40-900c-6265431fd81f"}},"feedback": {"rating": "poor","tokens": {"NoSound": false,"OtherNoSound": false,"Echo": false,"Noisy": true,"LowVolume": false,"Stopped": false,"DistortedSound": false,"Interruptions": false}}}}]
}
從上面返回的json可以看到,session里有?startDateTime
?和?endDateTime
。還有?caller
?和?callee
,我們用這些數據來進行進一步的深入統計,而且可以看到里面還有每個用戶參與這個通話是使用的設備(userAgent.platform
),更有意思的是,這里面還有?feedback
,里面有這個用戶這次通話中音質是好還是差,有沒有噪音等等。有了這些信息,相信大家腦海里已經有了大量的可以統計的點了。
各位看到這里,可能會問,上面的接口是獲取某一次 call record 的,那我如何獲取所有的記錄呢?
這里就需要用到 graph api 的訂閱功能。
POST https://graph.microsoft.com/beta/subscriptions
{"changeType": "created","notificationUrl": "https://<<your api url>>","resource": "/communications/callRecords","expirationDateTime":"2016-11-20T18:23:45.9356913Z","clientState": "secretClientValue","latestSupportedTlsVersion": "v1_2"
}
上面的?resource
?是?/communications/callRecords
,表明如果有任何一個call發生,graph api會回調我們的接口。上面的?"notificationUrl": "https://<<your api url>>"
?是你的服務的回調 url,我們可以在這個接口里得到相應的 call record id,并且記錄下來,等一段時間call 結束后,就可以獲取整個call的信息了。
?