Matrix / Synapse:通过客户端服务器API发送图像

问题描述 投票:0回答:2

Matrix 是一个用于安全、去中心化通信的开放网络。我在一台主机上运行 Matrix Homeserver 的 Synapse 实现。

我想使用客户端-服务器 API 将消息发送到特定的矩阵房间。我使用访问令牌和房间 ID 来执行 API 调用。我可以使用以下curl命令发送短信:

curl --header "Authorization: Bearer my_token" -X POST -d '{"msgtype":"m.text", "body":"my hello world messsage"}' "https://my_homeserver_url/_matrix/client/r0/rooms/"my_room_id"/send/m.room.message"

不幸的是,我还无法通过客户端-服务器 API 发送位于计算机本地的图像。根据文档,您必须选择 m.image 作为消息类型并相应地引用图像。

不幸的是,即使经过深入研究,我还没有找到一个可行的例子。有人能指出我正确的方向吗?

我尝试了各种curl命令,并尝试通过链接、路径或管道命令来引用该文件。

curl matrix matrix-synapse
2个回答
2
投票

您必须上传文件/图像,然后将文件的网址附加到新事件。这是一个例子:

1。上传图像 - 链接到规范

curl --data-binary @image.png 'https://matrix.server/_matrix/media/v3/upload?filename=image.png' \
  -X 'POST' \
  -H 'Authorization: Bearer my_access_token' \
  -H 'Content-Type: image/png' \
  --compressed

您会得到这样的回复

{
  "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw"
}

2。发送事件 - 链接到规范

curl 'https://matrix.server/_matrix/client/v3/rooms/!rVwmJkYsikYjnIeLNU:matrix.server/send/m.room.message/1212' \
  -X 'PUT' \
  -H 'Authorization: Bearer my_access_token' \
  -H 'accept: application/json' \
  -H 'content-type: application/json' \
  --data-raw '{"info":{"mimetype":"image/png","size":512,"w":512,"h":512},"msgtype":"m.image","body":"tta.webp","url":"mxc://example.com/AQwafuaFswefuhsfAFAgsw"}' \
  --compressed

请注意,交易 ID(上例中的 1212)对于您的访问令牌来说必须是唯一的。因此,使用 uuid(例如使用

uuidgen
生成)可能是一个好主意。


-1
投票

它对我不起作用...

WRN Didn't find attachment 8565DF8A-5892-4D05-AA87-D58BB500A1E6 in message module=iMessage/Mac
© www.soinside.com 2019 - 2024. All rights reserved.