Files
Scripts/wetter-bot.sh
T
2026-07-12 13:39:33 +02:00

66 lines
1.6 KiB
Bash

#!/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&current=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