Sign inSign up

afreisinger/weather-bot

By afreisinger

โ€ขUpdated 6 months ago

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

Image
Buildkit cache
API management
Machine learning & AI
Developer tools
1

1.5K

afreisinger/weather-bot repository overview

โ ๐ŸŒค Weather Bot

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
VariableDescription
TELEGRAM_TOKENYour Telegram Bot token
OPENWEATHER_API_KEYYour 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

CommandDescription
/weatherCurrent weather for the default city
/weather <city>Current weather for a specific city
/forecast3-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_hourly12-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)
/alertsActive weather alerts for the default city
/alerts <city>Active weather alerts for a specific city
/helpShow 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.

Tag summary

Content type

Image

Digest

sha256:0faa3e639โ€ฆ

Size

53.4 MB

Last updated

6 months ago

docker pull afreisinger/weather-bot