这篇继续介绍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