rocket.chat HTTP API示例
阅读原文
建议阅读原文,始终查看最新文档版本,获得最佳阅读体验:《rocket.chat HTTP API示例》
rocket.chat官方开发者文档
rocket.chat的api是非常丰富和完善的,方便开发者将rocket.chat集成至其它应用中。
rocket.chat教程
社区wrapper
官方文档:https://developer.rocket.chat/apidocs#languagespecific-wrappers
还有几个社区开发的针对常用编程语言的wrapper,对于api的使用会更加方便,但是官方并不维护这些wrapper
可以看到,有python专用的wrapper,我发现第二个python wrapper已经有几年没有更新了,所以如果要用python这个wrapper,请用第一个。
创建personal access token
参考资料:https://developer.rocket.chat/apidocs/introduction-to-authentication
In Rocket.Chat, there are primarily two types of authentication tokens: authToken and personal access token.
authToken
authToken
is a temporary authentication token returned to users after a successful login through any login endpoint. Additionally, workspace administrators can create authtoken
for a user via the Create User Token endpoint.
personal access token
Personal access tokens are permanent authentication tokens that users can generate for themselves to access the API securely without exposing their primary credentials. See the Get Personal Access Tokens endpoint for more details.
本小节介绍如何创建personal access token,
如此便得到了token和user id。接下来的api请求,都需要用到这两个
获取users列表
参考资料:Get Users List
endpoint:/api/v1/users.list
详细的api说明请看上方的参考资料,官方文档有详细说明。
以下是一个示例:
curl --request GET \
--url http://10.65.37.237:3000/api/v1/users.list \
--header 'X-Auth-Token: <此处替换为你的token,可以是personal access token>' \
--header 'X-User-Id: <替换为你的user Id>' \
--header 'accept: application/json' | jq .
返回结果,默认显示50个用户信息,可以通过query parameter:count来指定每次输出的数量
发送消息
查询rooms的id
参考资料:Get Rooms
endpoint:api/v1/rooms.get
这个api,会输出所有rooms的基本信息
curl --request GET \
--url http://10.65.37.237:3000/api/v1/rooms.get \
--header 'X-Auth-Token: <此处替换为你的token,可以是personal access token>' \
--header 'X-User-Id: <替换为你的user Id>' \
--header 'accept: application/json' | jq .
输出
确保拥有相应权限
需要拥有下图所示的权限,才能通过api发送消息,实验环境我用的是管理员。
发送消息
curl --request POST \
--url http://10.65.37.237:3000/api/v1/chat.sendMessage \
--header 'X-Auth-Token: <此处替换为你的token,可以是personal access token>' \
--header 'X-User-Id: <替换为你的user Id>' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '{
"message": {
"rid": "GENERAL",
"msg": "Sample message",
"alias": "Gruggy",
"emoji": ":smirk:",
"avatar": "http://res.guggy.com/logo_128.png",
"attachments": [
{
"color": "#ff0000",
"text": "Yay for gruggy!",
"ts": "2016-12-09T16:53:06.761Z",
"thumb_url": "http://res.guggy.com/logo_128.png",
"message_link": "https://google.com",
"collapsed": false,
"author_name": "Bradley Hilton",
"author_link": "https://rocket.chat/",
"author_icon": "https://avatars.githubusercontent.com/u/850391?v=3",
"title": "Attachment Example",
"title_link": "https://youtube.com",
"title_link_download": true,
"image_url": "http://res.guggy.com/logo_128.png",
"audio_url": "http://www.w3schools.com/tags/horse.mp3",
"video_url": "http://www.w3schools.com/tags/movie.mp4",
"fields": [
{
"short": true,
"title": "Test",
"value": "Testing out something or other"
},
{
"short": true,
"title": "Another Test",
"value": "[Link](https://google.com/) something and this and that."
}
]
}
]
}
}' | jq .
输出