Profile picture

[Shell Script] 디스코드(discord)로 메시지 보내기 (discord webhook)

JaehyoJJAng2024년 01월 01일

▶ 방법 1

#!/usr/bin/bash

#==================
# Usage: sendDiscord.sh <content> <color>
#==================

if [[ $# -ne 2 ]]; then
  echo "Usage: $0 <content> <color>"
  exit 1
fi

DISCORD_WEBHOOK_URI="$(cat /home/$USER/Oracle/discord/.webhook | awk -F '=' '{print $2}')"
send_discord_notification() {
    payload=$(cat << EOF
    {
      "embeds": [{
          "title": "Alarm",
          "description": "$1",
          "color": "$2"
      }]
    }
EOF
    )
    curl -H "Content-Type: application/json" -X POST -d "$payload" "$DISCORD_WEBHOOK_URI"
}

send_discord_notification "$1" "$2"

▶ 방법 2

#!/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!"

Loading script...