Skip to content

Instantly share code, notes, and snippets.

@fbzhong
Last active March 15, 2025 16:36
Show Gist options
  • Save fbzhong/0ba6e521e657c2ec6da472cd5d16ccf5 to your computer and use it in GitHub Desktop.
Save fbzhong/0ba6e521e657c2ec6da472cd5d16ccf5 to your computer and use it in GitHub Desktop.
Discord Webhook Message Sender
#!/bin/bash
# This script sends a message to a Discord webhook or edits an existing message based on whether a local file containing the message ID exists.
# Requirements:
# - DISCORD_WEBHOOK_URL environment variable must be set to the Discord webhook URL.
# - jq must be installed for parsing JSON responses.
# Usage:
# export DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/xxx/yyy"
# ./discord_webhook_send.sh "Hello, world!" "my_key"
# ./discord_webhook_send.sh /path/to/content.txt "my_key"
# or
# export DISCORD_MESSAGE_KEY="my_key"
# ./discord_webhook_send.sh "Hello, world!"
# or
# ./discord_webhook_send.sh "Hello, world!"
# - First argument: content for the message or content file
# - Second argument (optional or ${DISCORD_MESSAGE_KEY}): Key for the message ID file. If provided, the script will edit the existing message if the file exists; otherwise, it will send a new message and save the message ID.
# If the second argument is omitted or empty, the script will always send a new message without saving the message ID.
# Check if the webhook URL environment variable is set
if [ -z "$DISCORD_WEBHOOK_URL" ]; then
echo "Error: DISCORD_WEBHOOK_URL is not set."
exit 1
fi
# Check if at least one argument is provided
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
echo "Usage: $0 <content> [key]"
exit 1
fi
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "Error: jq is not installed."
exit 1
fi
# Assign arguments to variables
content=$1
if [ -f "$content" ]; then
content=$(cat "$content")
fi
if [ $# -eq 2 ]; then
key=$2
else
key=${DISCORD_MESSAGE_KEY:-""}
fi
# Use jq to properly escape the content for JSON
# Handle large content by using a file to avoid argument list length issues
content_file=$(mktemp)
echo "$content" > "$content_file"
json_content=$(jq -n --rawfile content "$content_file" '{"content": $content}')
rm "$content_file"
# If key is provided, format it to make it safe for file names and set the file name
if [ -n "$key" ]; then
formatted_key=$(echo "$key" | sed 's/[^a-zA-Z0-9._]/_/g')
file="${TMPDIR}/.${formatted_key}.dws"
else
file=""
fi
# Function to send a new message and optionally save the message ID
send_new_message() {
local file=$1
local response=$(curl -s -X POST -H "Content-Type: application/json" -d "$json_content" "${DISCORD_WEBHOOK_URL}?wait=true")
if [ $? -eq 0 ]; then
local message_id=$(echo "$response" | jq -r '.id')
if [ "$message_id" != "null" ] && [ -n "$file" ]; then
echo "$message_id" > "$file"
fi
fi
}
# Main logic
if [ -n "$file" ] && [ -f "$file" ]; then
message_id=$(cat "$file")
if [ -n "$message_id" ]; then
# Edit existing message
edit_url="${DISCORD_WEBHOOK_URL}/messages/${message_id}"
curl -s -X PATCH -H "Content-Type: application/json" -d "$json_content" "$edit_url" > /dev/null 2>&1
else
# File exists but is empty; send new message and save message ID
send_new_message "$file"
fi
else
# File doesn't exist or key is empty; send new message and save message ID if key is provided
send_new_message "$file"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment