rocket.chat HTTP API示例

rocket.chat HTTP API示例

阅读原文

建议阅读原文,始终查看最新文档版本,获得最佳阅读体验:《rocket.chat HTTP API示例》

rocket.chat官方开发者文档

网址:Rocket.Chat API

rocket.chat的api是非常丰富和完善的,方便开发者将rocket.chat集成至其它应用中。

image.png

rocket.chat教程

《rocket.chat–开源的跨平台即时通讯软件》

社区wrapper

官方文档:https://developer.rocket.chat/apidocs#languagespecific-wrappers

还有几个社区开发的针对常用编程语言的wrapper,对于api的使用会更加方便,但是官方并不维护这些wrapper

可以看到,有python专用的wrapper,我发现第二个python wrapper已经有几年没有更新了,所以如果要用python这个wrapper,请用第一个。

image.png

创建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,

image.pngimage.png

如此便得到了token和user id。接下来的api请求,都需要用到这两个

image.png

获取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来指定每次输出的数量

image.png

发送消息

查询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 .

输出

image.png

确保拥有相应权限

需要拥有下图所示的权限,才能通过api发送消息,实验环境我用的是管理员。

image.png

发送消息

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 .

输出

image.png

关于作者和DreamAI

https://docs.dingtalk.com/i/nodes/Amq4vjg890AlRbA6Td9ZvlpDJ3kdP0wQ?iframeQuery=utm_source=portal&utm_medium=portal_recent

关注微信公众号“AI发烧友”,获取更多IT开发运维实用工具与技巧,还有很多AI技术文档!

梦幻智能logo-01(无水印).png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值