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.

82 lines
2.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/bin/bash
  2. # Send message to a matrix room. API call looks like this:
  3. # 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"
  4. set -e
  5. cd "${1}"
  6. exec 3>&1
  7. exec 1>&2
  8. # for jq
  9. PATH=/usr/local/bin:$PATH
  10. payload=$(mktemp /tmp/resource-in.XXXXXX)
  11. cat > "${payload}" <&0
  12. matrix_server_url="$(jq -r '.source.matrix_server_url' < "${payload}")"
  13. token="$(jq -r '.source.token' < "${payload}")"
  14. room_id="$(jq -r '.source.room_id' < "${payload}")"
  15. ts="$(date +%s)"
  16. matrix_endpoint="$matrix_server_url/_matrix/client/r0/rooms/$room_id/send/m.room.message/$ts?access_token=$token"
  17. text_file="$(jq -r '.params.text_file // ""' < "${payload}")"
  18. from="$(jq -r '.params.from // ""' < "${payload}")"
  19. text="$(jq '(.params.text // "${TEXT_FILE_CONTENT}")' < "${payload}")"
  20. msgtype="$(jq '(.params.msgtype // "m.notice")' < "${payload}")"
  21. always_notify="$(jq -r '.params.always_notify // "true"' < "${payload}")"
  22. debug="$(jq -r '.params.debug // "false"' < "${payload}")"
  23. silent="$(jq -r '.params.silent // "false"' < "${payload}")"
  24. TEXT_FILE_CONTENT=""
  25. [[ -n "${text_file}" && -f "${text_file}" ]] && TEXT_FILE_CONTENT="$(cat "${text_file}")"
  26. if [[ "$always_notify" == "true" || -n "$TEXT_FILE_CONTENT" || -z "$text_file" ]]
  27. then
  28. TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(no notification provided)_}"
  29. text="$(eval printf ${text} )"
  30. [[ -z "${text}" ]] && text="_(missing notification text)_"
  31. text="$(echo "${text}" | jq -R -s .)"
  32. [[ "${token}" != "null" ]] && username="$(eval "printf ${token}" | jq -R -s .)"
  33. [[ "${room_id}" != "null" ]] && room_id="$(eval "printf ${room_id}" | jq -R -s .)"
  34. body="$(cat <<EOF
  35. {
  36. "msgtype": ${msgtype},
  37. "body": ${text}
  38. }
  39. EOF
  40. )"
  41. compact_body="$(echo "${body}" | jq -c '.')"
  42. if [[ "$debug" == "true" ]]
  43. then
  44. json="$(cat <<EOF
  45. {
  46. "matrix_endpoint": "${matrix_endpoint}",
  47. "body": ${body}
  48. }
  49. EOF
  50. )"
  51. echo "$json" | jq -c '.'
  52. exit 0
  53. elif [[ "$silent" == "true" ]]
  54. then
  55. echo "Using silent output"
  56. curl -s -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}"
  57. else
  58. echo curl -v -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}"
  59. curl -v -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}"
  60. fi
  61. fi
  62. jq -n "{version:{timestamp:\"$(date +%s)\"}}" >&3