matrix_thing/doc/upload_bot_image.sh
2019-03-26 18:44:46 -07:00

38 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
#A script to easily upload an image to a matrix homeserver for use as a profile picture of the bot.
#Will print out the URI of the uploaded image if it succeeds. If it fails, I have no guess as to what might happen tbh
image_file="$1"
homeserver="$2"
username="$3"
password="$4"
if [ -z "$image_file" ];then
echo "need image file to upload"
exit 1
fi
if [ -z "$homeserver" ];then
echo "need homeserver to upload to"
exit 2
fi
if [ -z "$username" ];then
echo "need username"
exit 3
fi
if [ -z "$password" ];then
stty -echo
printf "Password: "
read password
stty echo
echo
fi
image_type="$(file "$image_file" | grep -o "[^ \t]* image data" | tr '[:upper:]' '[:lower:]' | cut -d' ' -f1)"
access_token="$(curl -X POST -d '{"type":"m.login.password", "user":"'"$username"'", "password":"'"${password}"'"}' "https://${homeserver}/_matrix/client/r0/login" | grep "access_token" | sed -e 's/^[ \t]*//' -e 's/[\"\,]//g' | cut -d' ' -f2)"
media_id="$(curl -X POST -H "Content-Type: image/${image_type}" --data-binary @"$image_file" "https://${homeserver}/_matrix/media/r0/upload?access_token=${access_token}&filename=$(basename $image_file)" | cut -d':' -f2- | sed -e 's/[\"\}]//g')"
echo "$media_id"