You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.9 KiB
90 lines
2.9 KiB
#!/bin/bash
|
|
|
|
# Send message to a matrix room. API call looks like this:
|
|
# curl -XPOST -d '{"msgtype":"m.text", "body":"hello"}' "http://matrix.org/_matrix/client/api/v1/rooms/%21CvcvRuDYDzTOzfKKgh%3Alocalhost/send/m.room.message?access_token=YOUR_ACCESS_TOKEN"
|
|
|
|
|
|
set -e
|
|
|
|
cd "${1}"
|
|
|
|
exec 3>&1
|
|
exec 1>&2
|
|
|
|
# for jq
|
|
PATH=/usr/local/bin:$PATH
|
|
|
|
payload=$(mktemp /tmp/resource-in.XXXXXX)
|
|
|
|
cat > "${payload}" <&0
|
|
|
|
matrix_server_url="$(jq -r '.source.matrix_server_url' < "${payload}")"
|
|
token="$(jq -r '.source.token' < "${payload}")"
|
|
room_id="$(jq -r '.source.room_id' < "${payload}")"
|
|
|
|
ts="$(date +%s)"
|
|
|
|
matrix_endpoint="$matrix_server_url/_matrix/client/r0/rooms/$room_id/send/m.room.message/$ts?access_token=$token"
|
|
|
|
text_file="$(jq -r '.params.text_file // ""' < "${payload}")"
|
|
data_file="$(jq -r '.params.data_file // ""' < "${payload}")"
|
|
from="$(jq -r '.params.from // ""' < "${payload}")"
|
|
text="$(jq '(.params.text // "${TEXT_FILE_CONTENT}")' < "${payload}")"
|
|
msgtype="$(jq '(.params.msgtype // "m.notice")' < "${payload}")"
|
|
|
|
always_notify="$(jq -r '.params.always_notify // "false"' < "${payload}")"
|
|
debug="$(jq -r '.params.debug // "false"' < "${payload}")"
|
|
silent="$(jq -r '.params.silent // "false"' < "${payload}")"
|
|
|
|
TEXT_FILE_CONTENT=""
|
|
[[ -n "${text_file}" && -f "${text_file}" ]] && TEXT_FILE_CONTENT="$(cat "${text_file}")"
|
|
DATA_FILE_CONTENT=""
|
|
[[ -n "${data_file}" && -f "${data_file}" ]] && DATA_FILE_CONTENT="$(cat "${data_file}")"
|
|
|
|
if [[ "$always_notify" == "true" || -n "$TEXT_FILE_CONTENT" || -z "$text_file" || -n "$DATA_FILE_CONTENT" ]]
|
|
then
|
|
TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(no notification provided)_}"
|
|
|
|
text="$(eval printf ${text} )"
|
|
[[ -z "${text}" ]] && text="_(missing notification text)_"
|
|
text="$(echo "${text}" | jq -R -s .)"
|
|
|
|
[[ "${token}" != "null" ]] && username="$(eval "printf ${token}" | jq -R -s .)"
|
|
[[ "${room_id}" != "null" ]] && room_id="$(eval "printf ${room_id}" | jq -R -s .)"
|
|
body="$(cat <<EOF
|
|
{
|
|
"msgtype": ${msgtype},
|
|
"body": ${text}
|
|
}
|
|
EOF
|
|
)"
|
|
if [ "$msgtype" != "m.notice" ]; then
|
|
# we can attach custom data...
|
|
builddata="$(cat <<EOF
|
|
{
|
|
"build_job_name": "${BUILD_JOB_NAME}",
|
|
"build_name": "${BUILD_NAME}",
|
|
"build_pipeline_name": "${BUILD_PIPELINE_NAME}",
|
|
"build_id": "${BUILD_ID}",
|
|
"atc_external_url": "${ATC_EXTERNAL_URL}"
|
|
}
|
|
EOF
|
|
)"
|
|
body=$(echo $body | jq -c ".build=$builddata")
|
|
if [ -n "$DATA_FILE_CONTENT" ]; then
|
|
body=$(echo $body | jq -c ".data=$DATA_FILE_CONTENT")
|
|
fi
|
|
fi
|
|
|
|
compact_body="$(echo "${body}" | jq -c '.')"
|
|
|
|
if [[ "$silent" == "true" ]]
|
|
then
|
|
echo "Using silent output"
|
|
curl -s -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}"
|
|
else
|
|
curl -v -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}"
|
|
fi
|
|
fi
|
|
|
|
jq -n "{version:{timestamp:\"$(date +%s)\"}}" >&3
|