docs(bots): add bot message send example

Signed-off-by: Michael Bell <mb@igloos.one>
This commit is contained in:
Michael Bell 2025-11-24 12:25:52 +10:00
parent 201d0afc81
commit f10d188c81

View file

@ -330,6 +330,54 @@ Bots can also send message. On the sending process the same signature/verificati
+ `413 Payload Too Large` When the message was longer than the allowed limit of 32000 characters (or 1000 until Nextcloud 16.0.1, check the `spreed => config => chat => max-length` capability for the limit)
+ `429 Too Many Requests` When `401 Unauthenticated` was triggered too often
#### Sample bash script:
```bash
#!/bin/bash
# Required environment variables:
# NC_URL: The URL of the Nextcloud instance (e.g., "https://nextcloud.example.com")
# TOKEN: The token of the conversation
# SECRET: Shared secret that is specified when installing a bot
# MESSAGE: The message to be sent
# Check if required variables are set
if [ -z "$NC_URL" ]; then
echo "Error: NC_URL is not set."
exit 1
fi
if [ -z "$TOKEN" ]; then
echo "Error: TOKEN is not set."
exit 1
fi
if [ -z "$SECRET" ]; then
echo "Error: SECRET is not set."
exit 1
fi
if [ -z "$MESSAGE" ]; then
echo "Error: MESSAGE is not set."
exit 1
fi
# Generate a random header and signature
RANDOM_HEADER=$(openssl rand -hex 32)
MESSAGE_TO_SIGN="${RANDOM_HEADER}${MESSAGE}"
SIGNATURE=$(echo -n "${MESSAGE_TO_SIGN}" | openssl dgst -sha256 -hmac "${SECRET}" | cut -d' ' -f2)
# Send the message
curl -X POST "${NC_URL}/ocs/v2.php/apps/spreed/api/v1/bot/${TOKEN}/message" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "OCS-APIRequest: true" \
-H "X-Nextcloud-Talk-Bot-Random: ${RANDOM_HEADER}" \
-H "X-Nextcloud-Talk-Bot-Signature: ${SIGNATURE}" \
-d '{"message":"'"${MESSAGE}"'"}'
```
## Reacting to a chat message
Bots can also react to a message. The same signature/verification method is applied.