playing with matrix via curl
This commit is contained in:
113
testdata/matrix-sandbox/build.sh
vendored
Normal file
113
testdata/matrix-sandbox/build.sh
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
#! /bin/bash
|
||||
|
||||
if ! ps aux | grep -v grep | grep homeserver; then
|
||||
homeserver -c config.yaml &
|
||||
fi
|
||||
|
||||
# pick a username
|
||||
username="$(cat /proc/sys/kernel/random/uuid)"
|
||||
|
||||
# register
|
||||
out="$(
|
||||
curl -sS http://192.168.0.86:39487/_matrix/client/r0/register \
|
||||
-X POST \
|
||||
-H 'content-type: application/json' \
|
||||
-d "$(
|
||||
printf '{
|
||||
"username": "%s",
|
||||
"password": "%s",
|
||||
"auth": {
|
||||
"type": "m.login.dummy"
|
||||
}
|
||||
}' "$username"
|
||||
)"
|
||||
)"
|
||||
printf "%s\n" "$out"
|
||||
access_token="$(echo "$out" | jq -r .access_token)"
|
||||
user_id="$(echo "$out" | jq -r .user_id)"
|
||||
home_server="$(echo "$out" | jq -r .home_server)"
|
||||
|
||||
# create a room
|
||||
room="$(cat /proc/sys/kernel/random/uuid)"
|
||||
|
||||
out="$(
|
||||
curl -sS http://192.168.0.86:39487/_matrix/client/r0/createRoom\
|
||||
-X POST \
|
||||
-H 'content-type: application/json' \
|
||||
-H 'Authorization: Bearer '"$access_token" \
|
||||
-d "$(
|
||||
printf '{
|
||||
"room_alias_name": "%s"
|
||||
}' "$room"
|
||||
)"
|
||||
)"
|
||||
printf "%s\n" "$out"
|
||||
room_id="$(echo "$out" | jq -r .room_id)"
|
||||
|
||||
# join a room by name
|
||||
# curl -XPOST -d '{}' "https://localhost:8448/_matrix/client/r0/join/%21asfLdzLnOdGRkdPZWu:localhost?access_token=YOUR_ACCESS_TOKEN"
|
||||
curl -sS "http://192.168.0.86:39487/_matrix/client/r0/join/$room_id" \
|
||||
-X POST \
|
||||
-H 'Authorization: Bearer '"$access_token" \
|
||||
-d '{}'
|
||||
|
||||
# send message
|
||||
# curl -XPOST -d '{"msgtype":"m.text", "body":"hello"}' "https://localhost:8448/_matrix/client/r0/rooms/%21asfLdzLnOdGRkdPZWu:localhost/send/m.room.message?access_token=YOUR_ACCESS_TOKEN"
|
||||
#PUT /_matrix/client/r0/rooms/%21636q39766251%3Aexample.com/send/m.room.message/35 HTTP/1.1 Content-Type: application/json {"msgtype": "m.text","body": "hello"}
|
||||
txid="$(cat /proc/sys/kernel/random/uuid)"
|
||||
message="hello wurl"
|
||||
|
||||
out="$(
|
||||
curl -sS "http://192.168.0.86:39487/_matrix/client/r0/rooms/$room_id/send/m.room.message/$txid" \
|
||||
-X PUT \
|
||||
-H 'content-type: application/json' \
|
||||
-H 'Authorization: Bearer '"$access_token" \
|
||||
-d "$(
|
||||
printf '{
|
||||
"msgtype": "m.text",
|
||||
"body": "%s"
|
||||
}' "$message"
|
||||
)"
|
||||
)"
|
||||
printf "%s\n" "$out"
|
||||
|
||||
# get recent messages
|
||||
# curl --globoff -XGET 'https://localhost:8448/_matrix/client/r0/sync?filter={"room":{"timeline":{"limit":1}}}&access_token=YOUR_ACCESS_TOKEN'
|
||||
out="$(
|
||||
curl -sS "http://192.168.0.86:39487/_matrix/client/r0/sync" \
|
||||
-X GET \
|
||||
-H 'content-type: application/json' \
|
||||
-H 'Authorization: Bearer '"$access_token" \
|
||||
| jq '{"timeline": .rooms.join[].timeline.events[], "next": .next_batch}' \
|
||||
| jq '{"content": .timeline.content.body, "sender": .timeline.sender, "next": .next}'
|
||||
)"
|
||||
printf "%s\n" "$out"
|
||||
since="$(echo "$out" | jq -r .next | tail -n 1)"
|
||||
|
||||
# send new message
|
||||
txid="$(cat /proc/sys/kernel/random/uuid)"
|
||||
message="hello wurl2"
|
||||
|
||||
out="$(
|
||||
curl -sS "http://192.168.0.86:39487/_matrix/client/r0/rooms/$room_id/send/m.room.message/$txid" \
|
||||
-X PUT \
|
||||
-H 'content-type: application/json' \
|
||||
-H 'Authorization: Bearer '"$access_token" \
|
||||
-d "$(
|
||||
printf '{
|
||||
"msgtype": "m.text",
|
||||
"body": "%s"
|
||||
}' "$message"
|
||||
)"
|
||||
)"
|
||||
printf "%s\n" "$out"
|
||||
|
||||
# get second recent messages
|
||||
# curl --globoff -XGET 'https://localhost:8448/_matrix/client/r0/sync?filter={"room":{"timeline":{"limit":1}}}&access_token=YOUR_ACCESS_TOKEN'
|
||||
curl -sS "http://192.168.0.86:39487/_matrix/client/r0/sync?since=$since" \
|
||||
-X GET \
|
||||
-H 'content-type: application/json' \
|
||||
-H 'Authorization: Bearer '"$access_token" \
|
||||
| jq '{"timeline": .rooms.join[].timeline.events[], "next": .next_batch}' \
|
||||
| jq '{"content": .timeline.content.body, "sender": .timeline.sender, "next": .next}'
|
||||
|
||||
Reference in New Issue
Block a user