#!/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 # workaround for directory not existing before get in 3.0.0 mkdir -p "${1}" cd "${1}" exec 3>&1 exec 1>&2 # for jq PATH=/usr/local/bin:$PATH payload=$(mktemp /tmp/resource-in.XXXXXX) cat > "${payload}" <&0 version=$(cat version) echo "Matrix-Notification-Resource Version: ${version}" matrix_server_url="$(jq -r '.source.matrix_server_url' < "${payload}")" token="$(jq -r '.source.token' < "${payload}")" room_id="$(jq -r '.source.room_id' < "${payload}")" msgtype="$(jq -r '.source.msgtype // "m.notice"' < "${payload}")" data_file="$(jq -r '.source.data_file // ""' < "${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}")" thisdata_file="$(jq -r '.params.data_file // ""' < "${payload}")" [[ -n $thisdata_file && $thisthisdata_file != $data_file ]] && data_file="${thisdata_file}" from="$(jq -r '.params.from // ""' < "${payload}")" trigger="$(jq -r '.params.trigger // ""' < "${payload}")" text="$(jq -r '(.params.text // "${TEXT_FILE_CONTENT}")' < "${payload}")" thismsgtype="$(jq -r '.params.msgtype // ""' < "${payload}")" [[ -n $thismsgtype && $thismsgtype != $msgtype ]] && msgtype="${thismsgtype}" always_notify="$(jq -r '.params.always_notify // "false"' < "${payload}")" debug="$(jq -r '.params.debug // "false"' < "${payload}")" silent="$(jq -r '.params.silent // "true"' < "${payload}")" link="$(jq -r '.params.link // "false"' < "${payload}")" team="$(jq -r '.params.team // "main"' < "${payload}")" prefix="$(jq -r '.params.prefix // ""' < "${payload}")" is_html="$(jq -r '.params.is_html // "false"' < "${payload}")" TEXT_FILE_CONTENT="" [[ -n "${text_file}" && ! -f "${text_file}" ]] && TEXT_FILE_CONTENT="_(text_file not found)_" if [[ -n "${text_file}" && -f "${text_file}" ]]; then TEXT_FILE_CONTENT="$(cat "${text_file}")" TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(text_file empty)_}" fi 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 export TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(text_file not provided)_}" text=$(echo -en "${text}" | envsubst) [[ -z "${text}" ]] && text="_(missing notification text)_" [ "${link}" == "true" ] && formatted_body="${text}" if [ -n "$prefix" ]; then prefix=$(echo "$prefix" | envsubst) text="${prefix}: ${text}" [ -n "${formatted_body}" ] && formatted_body="${prefix}: ${formatted_body}" fi if [[ "${is_html}" == "true" && -n "${formatted_body}" ]]; then formatted_body="${text}" fi text=$(echo -n "${text}" | jq -R -s .) [ -n "${formatted_body}" ] && formatted_body=$(echo -n "${formatted_body}" | jq -R -s .) [[ "${token}" != "null" ]] && username=$(echo "$token" | envsubst | jq -R -s .) [[ "${room_id}" != "null" ]] && room_id=$(echo "$room_id" | envsubst | jq -R -s .) body="$(cat <&3