幾個月前,Teams 團隊又推出了新的 Graph API,讓 app 可以給用戶發送 Activity Feed。我們來看看如何做。
首先,我們的app需要使用較新的 manifest 1.7版本,當然如果使用最新的1.8版本就更好了。在manifest json中添加?webApplicationInfo
?和?activities
?配置塊
{"$schema": "https://developer.microsoft.com/json-schemas/teams/v1.8/MicrosoftTeams.schema.json","manifestVersion": "1.8","version": "1.0.0","id": "your app id","packageName": "com.example.myapp",..."webApplicationInfo": {"id": "AAD App ID","resource": "Resource URL for acquiring auth token for SSO","applicationPermissions": [ ... ],},"activities": {"activityTypes": [{"type":"taskCreated","description":"Task Created Activity","templateText":"{actor} created task {taskId} for you"},{"type":"teamMention","description":"Team Mention Activity","templateText":"{actor} mentioned team"}]}
}
在?webApplicationInfo
?中的 id 指的是 AzureAD app 的 ID (client ID),resource
?是 app 的 reply url (或者 redirect url)。
在?activities
?中,type 是指 activity 的類型,它在整個manifest文件里需要唯一。description 是這類 activity 的簡稱,templateText 是 activity feed 推送的文本內容,它是一個模版,{actor}
?在這里是一個特殊的類型,teams系統會自動填寫,如果發送activity feed使用的是 delegated 驗證的話,actor就顯示delegated的用戶的名字,如果是 application 驗證的話,它顯示的是 app 的名字。{taskId}
是一個自定義的參數,你可以在發送 activity feed 的時候指定值。
我們準備好了manifest文件后,可以安裝到teams 里,并且我們還需要確保一點:接受 activity feed 的用戶也必須安裝了我們的app,我之前有一篇文章講的是如何主動給用戶安裝 app,有興趣的讀者可以參考那篇文章,這樣就可以把你的app主動推給所有的用戶進行安裝。(當然,你的app首先需要取得相應的權限)。
準備工作做好后,我們就可以發送一個 Graph API的請求。如下:
POST https://graph.microsoft.com/beta/chats/{chatId}/sendActivityNotification
Content-Type: application/json{"topic": {"source": "entityUrl","value": "https://graph.microsoft.com/beta/chats/{your-chat-id}"},"activityType": "taskCreated","previewText": {"content": "A new task was created"},"recipient": {"@odata.type": "microsoft.graph.aadUserNotificationRecipient","userId": "569363e2-1111-2222-3333-16f245c5d66a"},"templateParameters": [{"name": "taskId","value": "123445"}]
}
大家可以看到在?templateParameters
?中,我們指定了taskId 參數,這個參數就對應到了之前 manifest 文件里?activities
?節點下的?{taskId}
。
{"type":"taskCreated","description":"Task Created Activity","templateText":"{actor} created task {taskId} for you"},
所以最終推送出去的文字內容就是?SomeName created task 123445 for you
。
?