From 0f34fb8964524cff3ae6e6df928ee437de53eaeb Mon Sep 17 00:00:00 2001 From: dataprolet <1+dataprolet@noreply.localhost> Date: Sun, 12 Jul 2026 13:39:33 +0200 Subject: [PATCH] Add wetter-bot.sh --- wetter-bot.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 wetter-bot.sh diff --git a/wetter-bot.sh b/wetter-bot.sh new file mode 100644 index 0000000..11626db --- /dev/null +++ b/wetter-bot.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +set -x +set -eou pipefail + +logdir="$HOME/.logs/wetter" +mkdir -p $logdir +log="$logdir/wetter-$(date +%Y-%m-%d_%H:%M).log" +exec > >(tee -i ${log}) 2>&1 + +# Koordinaten von meinem Zuhause +lat="" +long="" + +# API-URLs +base_url="api.open-meteo.com" +api_url="https://${base_url}/v1/forecast/?latitude=${lat}&longitude=${long}&daily=temperature_2m_max¤t=temperature_2m&timezone=Europe%2FBerlin&forecast_days=1" + +# Telegram +telegram_token="" +CHAT_ID="" + +# Error-Handling +handle_error() { + MESSAGE="Fehler!" + curl -s -X POST https://api.telegram.org/bot${telegram_token}/sendMessage -d chat_id=${CHAT_ID} -d \ + text="${MESSAGE}" > /dev/null +} + +trap handle_error ERR + +# Teste Verbindung (10 Versuche) +count=0 +while true; do + if ping -c3 -W3 "${base_url}"; then + break + else + if [[ count -ge 10 ]]; then + exit 1 + fi + count=$((count + 1)) + sleep 5 + fi +done + +# Hole Temperaturen für heute (10 Versuche) +count=0 +while true; do + if curl --connect-timeout 3 "${api_url}" | jq . > /tmp/todays-forecast.json; then + break + else + if [[ count -ge 10 ]]; then + exit 1 + fi + count=$((count + 1)) + sleep 5 + fi +done + +max_temp=$(cat /tmp/todays-forecast.json | jq '.daily.temperature_2m_max.[]') +current_temp=$(cat /tmp/todays-forecast.json | jq '.current.temperature_2m') + +# Sende Nachricht (Linebreak = %0A) +MESSAGE="Heute werden es höchsten ${max_temp} °C, aktuell sind es ${current_temp} °C." +curl -s -X POST https://api.telegram.org/bot${telegram_token}/sendMessage -d chat_id=${CHAT_ID} -d \ + text="${MESSAGE}" > /dev/null \ No newline at end of file