Skip to content

afreisinger/weather-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌀 Weather Bot

Tests Build GitHub release GHCR Docker Pulls Docker Image Version License

A Telegram bot that provides real-time weather information powered by the OpenWeather OneCall 3.0 API. It supports current conditions, multi-day forecasts, hourly forecasts, and weather alerts β€” all accessible via Telegram commands or a CLI tool.

The project is built with Python 3.12, aiogram 3.x, and can be easily integrated with OpenClaw for agent automation or LLM pipelines


πŸ“‹ Table of Contents


✨ Features

  • 🌑 Current weather β€” temperature, humidity, wind speed, and conditions.
  • πŸ“… Multi-day forecast β€” up to 8 days ahead.
  • ⏱ Hourly forecast β€” up to 48 hours ahead.
  • 🚨 Weather alerts β€” active alerts for any city.
  • ⚑ In-memory caching β€” 5-minute TTL to reduce API calls.
  • πŸ”„ Auto-retry β€” exponential backoff on transient network errors.
  • 🐳 Docker-ready β€” multi-stage Dockerfile with a non-root user.
  • πŸ–₯ CLI interface β€” query weather directly from the terminal.

πŸ“ Project Structure

weather-bot/
β”œβ”€β”€ cli/
β”‚   └── weather_cli.py          # CLI entrypoint
β”œβ”€β”€ config/
β”‚   └── config.yaml             # App configuration (city, units, defaults)
β”œβ”€β”€ logs/                       # Log output directory
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ test_weather.py         # Unit + integration tests (fully mocked)
β”‚   └── helpers/
β”‚       └── telegram_sender.py  # Test helpers
β”œβ”€β”€ weather/
β”‚   β”œβ”€β”€ bot/
β”‚   β”‚   β”œβ”€β”€ main.py             # Bot entrypoint (aiogram)
β”‚   β”‚   └── handlers.py         # Telegram command handlers
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ config.py           # Settings loader (YAML + env vars)
β”‚   β”‚   └── logging.py          # Logger setup
β”‚   └── skills/
β”‚       └── weather/
β”‚           β”œβ”€β”€ client.py       # Async OpenWeather API client
β”‚           β”œβ”€β”€ formatters.py   # Response formatters
β”‚           β”œβ”€β”€ schema.py       # Tool-call schema handler
β”‚           └── skill.py        # WeatherSkill core logic
β”œβ”€β”€ .env.sample                 # Environment variable template
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ Dockerfile
└── requirements.txt

βœ… Requirements


πŸš€ Installation

# 1. Clone the repository
git clone https://github.com/afreisinger/weather-bot.git
cd weather-bot

# 2. Create and activate a virtual environment
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate

# 3. Install dependencies
pip install -r requirements.txt

βš™οΈ Configuration

Environment Variables

Copy the sample file and fill in your credentials:

cp .env.sample .env
Variable Description
TELEGRAM_TOKEN Your Telegram Bot token
OPENWEATHER_API_KEY Your OpenWeather API key
CONFIG_PATH (Optional) Custom config file path

config/config.yaml

weather:
  default_city: "Buenos Aires"
  units: "metric"         # metric | imperial | standard
  forecast_days: 3        # Default days for /forecast (1–8)
  forecast_hours: 12      # Default hours for /forecast_hourly (1–48)

▢️ Running the Bot

Locally

export TELEGRAM_TOKEN=your_token
export OPENWEATHER_API_KEY=your_key

python -m weather.bot.main

With Docker

# Build
docker build -t weather-bot .

# Run
docker run --env-file .env -v $(pwd)/config:/app/config weather-bot

With Docker Compose

docker compose up --build

The docker-compose.yml mounts ./config and ./logs as volumes and automatically restarts the container on failure.


πŸ–₯ CLI Usage

Query weather directly from your terminal without running the bot:

# Current weather
python -m cli.weather_cli current "London"

# 5-day forecast
python -m cli.weather_cli forecast "CΓ³rdoba" --days 5

# Enable debug logging
python -m cli.weather_cli -v current "Tokyo"

πŸ’¬ Telegram Commands

Command Description
/weather Current weather for the default city
/weather <city> Current weather for a specific city
/forecast 3-day forecast for the default city
/forecast <city> 3-day forecast for a specific city
/forecast <city> <days> Custom-day forecast (1–8 days)
/forecast_hourly 12-hour forecast for the default city
/forecast_hourly <city> 12-hour forecast for a specific city
/forecast_hourly <city> <hours> Custom hourly forecast (1–48 hours)
/alerts Active weather alerts for the default city
/alerts <city> Active weather alerts for a specific city
/help Show all available commands

πŸ§ͺ Running Tests

Tests are fully mocked β€” no real network calls or API keys are required.

# Install dev dependencies
pip install -r requirements-dev.txt

# Run all tests
pytest tests/ -v

# Run with coverage report
pytest tests/ -v --cov=weather --cov-report=term-missing

πŸ— Architecture Overview

Telegram User
     β”‚
     β–Ό
[aiogram Handlers]  ←── weather/bot/handlers.py
     β”‚
     β–Ό
[WeatherSkill]      ←── weather/skills/weather/skill.py
     β”‚
     β”œβ”€β”€β–Ί [Geocoding API]    ─┐
     └──► [OneCall 3.0 API]  ──── weather/skills/weather/client.py
                              β”‚   (retry, cache, validation)
                              β–Ό
                       [Formatters]  ←── weather/skills/weather/formatters.py
                              β”‚
                              β–Ό
                      Formatted string response
  • WeatherSkill is transport-agnostic β€” it can be used by the Telegram bot, the CLI, or any other interface.
  • client.py handles all HTTP communication with in-memory caching and exponential backoff retries.
  • config.py merges config.yaml defaults with environment variable overrides.

πŸ“„ License

MIT License. See LICENSE for details.

About

A Telegram bot that provides real-time weather. integrated with OpenClaw

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors