| 23 Jul 2021 |
@grahamc:nixos.org | yeah | 11:45:13 |
CRTified | https://matrix.org/_matrix/media/r0/download/nixos.org/f99b43034eae3a82706e86aca15300fda8210b79 | 11:45:41 |
CRTified | You can just plug it in the end of that URL | 11:45:49 |
@grahamc:nixos.org | oh cool | 11:45:53 |
CRTified | In the end, its basically all HTTP | 11:46:16 |
@grahamc:nixos.org | yeah :) with some manipulation I suppose | 11:46:25 |
CRTified | You can also swap out the host and use the same URL on different homeservers, it will fetch (and cache) the download from the homeserver in the URL | 11:47:06 |
@grahamc:nixos.org | $ synadm -o json room details '!VjfUzaKsXokUdnQcvP:nixos.org' | jq .
{
"room_id": "!VjfUzaKsXokUdnQcvP:nixos.org",
"name": "Nix Python",
"canonical_alias": "#python:nixos.org",
"joined_members": 50,
"joined_local_members": 2,
"version": "6",
"creator": "@grahamc:nixos.org",
"encryption": null,
"federatable": true,
"public": true,
"join_rules": "public",
"guest_access": null,
"history_visibility": "world_readable",
"state_events": 62,
"avatar": null,
"topic": "Anything regarding using Python with Nix.",
"joined_local_devices": 12
}
so iterating over each room id, filtering out rooms with a null avatar, and then figuring out how to upload media and update the room id
| 11:47:54 |
kevincox | If you post an image Element will also let you view source and see the mxc URL. But that isn't too convenient for automation. | 11:47:54 |
@grahamc:nixos.org | yeah, I wonder what the API is like to upload the image automatically | 13:24:19 |
@grahamc:nixos.org | maybe sumner has ideas here | 13:24:25 |
kevincox | It looks like you could combine:
| 13:28:30 |
sumner | In reply to @grahamc:nixos.org yeah, I wonder what the API is like to upload the image automatically Just send it bytes I think: https://matrix.org/docs/spec/client_server/r0.4.0#post-matrix-media-r0-upload | 14:18:55 |
sumner | It gives you the MXC URI as a response. Normally clients will then turn around right away and send it in a message, but in this case you just need to add it to a m.room.avatar state event (https://matrix.org/docs/spec/client_server/r0.4.0#m-room-avatar) which can be created with this endpoint: https://matrix.org/docs/spec/client_server/r0.4.0#put-matrix-client-r0-rooms-roomid-state-eventtype | 14:22:37 |
@grahamc:nixos.org | cool | 14:59:06 |
@grahamc:nixos.org | thanks! | 14:59:07 |
@grahamc:nixos.org | I don't suppose anyone wants to make a little script which accepts a room id and local file path, to upload the image and set the avatar? 😃 | 14:59:36 |
andi- | One of the famous matrix bots has a command/plugin for converting an image to an MXC URI | 15:07:56 |
CRTified | Redacted or Malformed Event | 15:11:15 |
CRTified | In reply to @grahamc:nixos.org I don't suppose anyone wants to make a little script which accepts a room id and local file path, to upload the image and set the avatar? 😃 #!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl
# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here
# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"
# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: image/jpeg" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload?filename=image.png" | jq '.content_uri')
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $mxc }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"
| 15:29:53 |
@grahamc:nixos.org | 🥳 | 15:30:42 |
CRTified | Oh, Content-type might be wrong and needs a fix | 15:33:09 |
CRTified | But it works even if it's wrong | 15:33:19 |
CRTified | In reply to @grahamc:nixos.org I don't suppose anyone wants to make a little script which accepts a room id and local file path, to upload the image and set the avatar? 😃 * #!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl
# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here
# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"
# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: image/png" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload?filename=image.png" | jq '.content_uri')
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $mxc }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"
| 15:34:10 |
CRTified | * Oh, Content-type might be wrong and needs a fix (Edited it to image/png) | 15:34:37 |
CRTified | And note that there's no error handling 👀 | 15:35:25 |
sumner | Alternative is:
mimetype=$(file --mime-type $avatar_path | sed 's/.*: //g')
| 15:37:27 |
CRTified | That's significantly cleaner | 15:37:52 |
sumner | * Alternative is:
mimetype=$(file --mime-type $FILEPATH | sed 's/.*: //g')
| 15:38:14 |
CRTified | * #!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl
# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here
# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"
# Thanks to sumner
MIMETYPE=$(file --mime-type $FILEPATH | sed 's/.*: //g')
echo MIME is $MIMETYPE
# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: ${MIMETYPE}" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload?filename=image.png")
echo $mxc
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $(echo $mxc | jq '.content_uri') }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"
| 15:40:31 |