blob: b00109e8303475c240d3d4d7f42418ef54b106b5 [file] [log] [blame]
Marc Kupietz34c5e412025-12-03 17:52:29 +01001#!/bin/bash
2
3# Default values
4NC_URL="${NC_TALK_URL:-https://cloud.ids-mannheim.de}"
5CONVERSATION="${NC_TALK_CONVERSATION:-o6toyqx7}"
6SECRET="${NC_TALK_SECRET}"
7MESSAGE=""
8DEBUG=false
9SILENT=false
10
11# Function to display help
12show_help() {
13 cat << EOF
14Usage: $(basename "$0") [OPTIONS] [message]
15
16Sends <message> to a Nextcloud Talk room. Use - for reading message from stdin
17
18Options:
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
26Environment variables:
27 NC_TALK_URL Nextcloud URL
28 NC_TALK_CONVERSATION Conversation token
29 NC_TALK_SECRET Bot secret
30
31EOF
32 exit 0
33}
34
35# Parse command line arguments
36while [[ $# -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
70done
71
72# Check if we should read from stdin (before consuming stdin)
73if [[ -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```'
79fi
80
81# Check required parameters
82if [[ -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
85fi
86
87if [[ -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
90fi
91
92if [[ -z "$MESSAGE" ]]; then
93 echo "Error: Message is required." >&2
94 exit 1
95fi
96
97if [[ "$DEBUG" == true ]]; then
98 echo "URL: $NC_URL" >&2
99 echo "Conversation: $CONVERSATION" >&2
100 echo "Message: $MESSAGE" >&2
101 echo "Silent: $SILENT" >&2
102fi
103
104# Generate a random header and signature
105RANDOM_HEADER=$(openssl rand -hex 32)
106MESSAGE_TO_SIGN="${RANDOM_HEADER}${MESSAGE}"
107SIGNATURE=$(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)
110MESSAGE_ESCAPED=$(echo -n "$MESSAGE" | jq -Rs .)
111
112# Send the message
113curl -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