| Marc Kupietz | 34c5e41 | 2025-12-03 17:52:29 +0100 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Default values |
| 4 | NC_URL="${NC_TALK_URL:-https://cloud.ids-mannheim.de}" |
| 5 | CONVERSATION="${NC_TALK_CONVERSATION:-o6toyqx7}" |
| 6 | SECRET="${NC_TALK_SECRET}" |
| 7 | MESSAGE="" |
| 8 | DEBUG=false |
| 9 | SILENT=false |
| 10 | |
| 11 | # Function to display help |
| 12 | show_help() { |
| 13 | cat << EOF |
| 14 | Usage: $(basename "$0") [OPTIONS] [message] |
| 15 | |
| 16 | Sends <message> to a Nextcloud Talk room. Use - for reading message from stdin |
| 17 | |
| 18 | Options: |
| 19 | -h, --help Show this help message |
| 20 | -d, --debug Output extra debugging |
| 21 | -c, --conversation <token> Send message to conversation (default: o6toyqx7) |
| 22 | -s, --secret <secret> Bot secret |
| 23 | -u, --url <url> Nextcloud URL (default: https://cloud.ids-mannheim.de) |
| 24 | --silent Send message silently (no notification) |
| 25 | |
| 26 | Environment variables: |
| 27 | NC_TALK_URL Nextcloud URL |
| 28 | NC_TALK_CONVERSATION Conversation token |
| 29 | NC_TALK_SECRET Bot secret |
| 30 | |
| 31 | EOF |
| 32 | exit 0 |
| 33 | } |
| 34 | |
| 35 | # Parse command line arguments |
| 36 | while [[ $# -gt 0 ]]; do |
| 37 | case $1 in |
| 38 | -h|--help) |
| 39 | show_help |
| 40 | ;; |
| 41 | -d|--debug) |
| 42 | DEBUG=true |
| 43 | shift |
| 44 | ;; |
| 45 | -c|--conversation) |
| 46 | CONVERSATION="$2" |
| 47 | shift 2 |
| 48 | ;; |
| 49 | -s|--secret) |
| 50 | SECRET="$2" |
| 51 | shift 2 |
| 52 | ;; |
| 53 | -u|--url) |
| 54 | NC_URL="$2" |
| 55 | shift 2 |
| 56 | ;; |
| 57 | --silent) |
| 58 | SILENT=true |
| 59 | shift |
| 60 | ;; |
| 61 | -*) |
| 62 | echo "Unknown option: $1" >&2 |
| 63 | show_help |
| 64 | ;; |
| 65 | *) |
| 66 | MESSAGE="$1" |
| 67 | shift |
| 68 | ;; |
| 69 | esac |
| 70 | done |
| 71 | |
| 72 | # Check if we should read from stdin (before consuming stdin) |
| 73 | if [[ -z "$MESSAGE" ]] || [[ "$MESSAGE" == "-" ]]; then |
| 74 | if [[ "$DEBUG" == true ]]; then |
| 75 | echo "Reading from stdin" >&2 |
| 76 | fi |
| 77 | # Read from stdin and wrap in code blocks like slack.js |
| 78 | MESSAGE=$'```\n'"$(cat)"$'\n```' |
| 79 | fi |
| 80 | |
| 81 | # Check required parameters |
| 82 | if [[ -z "$CONVERSATION" ]]; then |
| 83 | echo "Error: Conversation token is required. Use -c/--conversation option or set NC_TALK_CONVERSATION environment variable." >&2 |
| 84 | exit 1 |
| 85 | fi |
| 86 | |
| 87 | if [[ -z "$SECRET" ]]; then |
| 88 | echo "Error: Bot secret is required. Use -s/--secret option or set NC_TALK_SECRET environment variable." >&2 |
| 89 | exit 1 |
| 90 | fi |
| 91 | |
| 92 | if [[ -z "$MESSAGE" ]]; then |
| 93 | echo "Error: Message is required." >&2 |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
| 97 | if [[ "$DEBUG" == true ]]; then |
| 98 | echo "URL: $NC_URL" >&2 |
| 99 | echo "Conversation: $CONVERSATION" >&2 |
| 100 | echo "Message: $MESSAGE" >&2 |
| 101 | echo "Silent: $SILENT" >&2 |
| 102 | fi |
| 103 | |
| 104 | # Generate a random header and signature |
| 105 | RANDOM_HEADER=$(openssl rand -hex 32) |
| 106 | MESSAGE_TO_SIGN="${RANDOM_HEADER}${MESSAGE}" |
| 107 | SIGNATURE=$(echo -n "${MESSAGE_TO_SIGN}" | openssl dgst -sha256 -hmac "${SECRET}" | cut -d' ' -f2) |
| 108 | |
| 109 | # Escape the message for JSON (replace backslashes, quotes, and newlines) |
| 110 | MESSAGE_ESCAPED=$(echo -n "$MESSAGE" | jq -Rs .) |
| 111 | |
| 112 | # Send the message |
| 113 | curl -X POST \ |
| 114 | "${NC_URL}/ocs/v2.php/apps/spreed/api/v1/bot/${CONVERSATION}/message" \ |
| 115 | -H "Content-Type: application/json" \ |
| 116 | -H "Accept: application/json" \ |
| 117 | -H "OCS-APIRequest: true" \ |
| 118 | -H "X-Nextcloud-Talk-Bot-Random: ${RANDOM_HEADER}" \ |
| 119 | -H "X-Nextcloud-Talk-Bot-Signature: ${SIGNATURE}" \ |
| 120 | -d '{"message":'"${MESSAGE_ESCAPED}"',"silent":'"$SILENT"'}' |
| 121 | |