這篇繼續介紹BUILD大會里的內容:兩個新加入GraphAPI 1.0的關于Teams的API。
這兩個新增api是關于在頻道Channel里發送消息和回復消息的。實際上這兩個api在beta版本中早就已經加入,上個月build大會中公布的只是把這兩個api正式發布到1.0版本,這意味著微軟官方會對這兩個api作長期的支持,這也意味著大家在生產環境可以使用了。 :)
我們先來看一下如何在一個channel里發送消息,使用如下的rest api。
POST /teams/{team-id}/channels/{channel-id}/messages
其中?team-id
?就是channel所屬的team的id,?channel-id
?就新消息要發送到的channel的id。
大家要注意的是這個rest api目前只是支持Delegated permission,并不支持Application permission,也就是說你以當前登入用戶的身份來發送消息。需要申請的權限是:ChannelMessage.Send
,?Group.ReadWrite.All
。
發送的內容(HTTP body)可以是一個簡單的純文字內容,如下:
{"body": {"content": "Hello!"}
}
還可以發送一個 @ 某人的富文本消息,http body內容如下:
{"body": {"contentType": "html","content": "Hello <at id=\"0\">Tony</at>"},"mentions": [{"id": 0,"mentionText": "Tony","mentioned": {"user": {"displayName": "Tony","id": "a01a899c-3e5f-4f0e-85b9-d75e24166e1a","userIdentityType": "aadUser"}}}]
}
還可以發送更加復雜的互動卡片,具體的內容,大家可以參考官方api文檔。
回復一條消息的api和上面發送消息的api的權限一樣ChannelMessage.Send
,?Group.ReadWrite.All
,它目前也只是支持Delegated permission,并不支持Application permission。url是:
POST /teams/{team-id}/channels/{channel-id}/messages/{message-id}/replies
message-id
就是你需要回復的那條消息的id。
http body的內容和上面發送消息的body格式一樣。api返回的內容如下。它實際上是一個message結構,具體內容可以參考這里。
{"id": "message-id","messageType": "message","createdDateTime": "2020-01-01T00:11:22.333Z","lastModifiedDateTime": null,"subject": null,"summary": null,"importance": "normal","locale": "en-us","policyViolation": null,"from": {"application": null,"device": null,"conversation": null,"user": {"displayName": "Tony","userIdentityType": "aadUser"}},"body": {"contentType": "html","content": "Hello!"},"attachments": [],"mentions": [],"reactions": []
}
參考:
- Create chatMessage in a channel
- Reply to a message in a channel