To follow the instructions, I will create a simple Python script that captures and logs the DynDNS update status using a webhook URL to prevent sending unnecessary messages.
Here is the code:
``
import requests
from datetime import datetime
# Set up webhooks for Telegram, Discord, and a personal DM channel
webhook_telegram = "https://example.org/telegram"
webhook_discord = "https://example.co/discord"
webhook dm = {"to": ["1234567890"]}
# Define the DynDNS status update URL
dynupdate_url = "https://example.net/dynupdate"
# Send an email notification with the current status (or a default message if not available)
def dnu_status(update):
# Check if the status is NO_CHANGE
if update == "NO CHANGE":
print("Sending NOTHING to telegram...")
# Check if the status indicates a successful update
elif update.startswith("OK:"):
for target in webhook_telegram + webhook_discord:
# Send an email notification with a relevant message
subject = f"DynDNS Update: {datetime.now().strftime('%Y%m%d %H:%M:%S')}"
body = "๐ DynDNS Updated successfully. Please refresh the page to check its status."
response = requests.post(target, data={"status": update}, headers={"Content-Type": "application/json"})
if not success(response.text):
print("Sending SUCCESSALERT message for telegram...")
# Send a default message (or nothing) to the personal DM channel
elif update.startswith("->"):
try:
user = requests.post(webhook dm["to"], json={"status": update}, headers={"Content-Type": "application/json"})
if not success(user.text):
print("Sending SUCCESSALERT message for the DM...")
except Exception as e:
print(f"Sorry, something went wrong: {e}")
# Define a function to check if an email message is sent successfully
def success(message):
# Check if the message subject and body contain anything interesting
if "NO CHANGE" not in message.decode().splitlines() and "OK:" not in message.decode().splitlines():
return True
return False
# Run the script with a scheduled cron job under /root/.hermes/skills/cf-dyndns.sh
os.system('/root/.hermes/skills/cf-dyndns.sh 2>&1')
`
Please replace ," target="_blank" rel="noopener">https://example.org/telegram, https://example.co/discord and " target="_blank" rel="noopener">https://example.net/dynupdate with your actual Telegram, Discord, and DynDNS update URLs. Make sure to adjust the email notification message templates as well.
This script will send an alarm for a NO_CHANGE status or a SUCCESSALERT message for each successful DynDNS update. The success messages are filtered by checking if they start with
-> (the HTTP status code)