Skip to content

Instantly share code, notes, and snippets.

@apfzvd
Last active June 18, 2024 08:19
Show Gist options
  • Save apfzvd/300346dae55190e022ee49a1001d26af to your computer and use it in GitHub Desktop.
Save apfzvd/300346dae55190e022ee49a1001d26af to your computer and use it in GitHub Desktop.
how to send a message to discord channel using the webhook via shell script
#!/bin/bash
##
# Discord Webhook
# Change the 'your_discord_webhook_name' with your actual Discord Webhook
##
##
# add to linux cron:
# sudo crontab -e
# https://crontab.guru/
##
discord_url="your_discord_webhook_name"
generate_post_data() {
cat <<EOF
{
"content": "Hello! World!",
"embeds": [{
"title": "Embeded Title",
"description": "Description",
"color": "45973"
}]
}
EOF
}
# POST request to Discord Webhook
curl -H "Content-Type: application/json" -X POST -d "$(generate_post_data)" $discord_url
@frederikstroem
Copy link

Thank you, OP. I drew inspiration and adopted a generalized approach:

#!/bin/bash

# Generalized Discord Notification Script

discord_url="your_discord_webhook_url"

# Define a function to send a message
send_discord_notification() {
  local message=$1
  
  # Construct payload
  local payload=$(cat <<EOF
{
  "content": "$message"
}
EOF
)

  # Send POST request to Discord Webhook
  curl -H "Content-Type: application/json" -X POST -d "$payload" $discord_url
}

# Use the function
send_discord_notification "Hello, World!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment