Skip to content

MusaIslamFahad/telegram-weather-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

30 Commits
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒค๏ธ Telegram Weather Bot

A Python Telegram bot that fetches real-time weather data from OpenWeatherMap and automatically delivers hourly reports to your Telegram chat using APScheduler.


๐Ÿ“– Overview

Telegram Weather Bot is a Python-based bot that connects the Telegram Bot API with the OpenWeatherMap API to deliver real-time weather updates for Dhaka, Bangladesh (configurable to any city). Once started, the bot automatically pushes a full weather report to your Telegram chat every hour using a background scheduler - no manual trigger needed. It also responds to on-demand commands for instant updates and control.


โœจ Features

  • ๐Ÿ“ก Real-time weather data: temperature, humidity, wind speed, pressure, visibility, and weather condition
  • โฑ๏ธ Hourly auto-reports: APScheduler pushes updates every hour without any user action
  • ๐Ÿค– Bot commands: full control via /start, /stop, and /test
  • ๐Ÿ“ City-configurable: defaults to Dhaka, Bangladesh; change to any city in one line
  • ๐Ÿ”’ Secure API keys: credentials loaded from environment variables, never hardcoded
  • ๐Ÿชถ Lightweight: no database, no web server; just Python running in the background

๐Ÿค– Bot Commands

Command Action
/start Begin receiving hourly weather reports
/stop Stop the hourly weather schedule
/test Get an immediate weather report and confirm the bot is alive

๐ŸŒก๏ธ Weather Report Fields

Each report includes the following live data from OpenWeatherMap:

Field Description
๐ŸŒก๏ธ Temperature Current temp in ยฐC
๐ŸŒก๏ธ Feels Like Perceived temperature
๐Ÿ’ง Humidity Relative humidity (%)
๐Ÿ’จ Wind Speed Speed in m/s
๐ŸŒฌ๏ธ Pressure Atmospheric pressure (hPa)
๐Ÿ‘๏ธ Visibility Visibility in metres
โ˜๏ธ Condition Sky condition (e.g., Clear, Cloudy, Rain)

๐Ÿ”ง How It Works

User sends /start on Telegram
         โ”‚
         โ–ผ
   pyTelegramBotAPI receives the command
         โ”‚
         โ–ผ
   APScheduler starts a background job
         โ”‚  (runs every 60 minutes)
         โ–ผ
   OpenWeatherMap API called
         โ”‚  โ†’ Returns live JSON weather data
         โ–ผ
   Bot formats the weather message
         โ”‚
         โ–ผ
   Message sent to user's Telegram chat โœ…

/stop cancels the scheduled job. /test triggers a single immediate fetch outside the schedule.


๐Ÿ› ๏ธ Tech Stack

Tool Purpose
Python 3 Core language
pyTelegramBotAPI Telegram bot API wrapper
APScheduler Background hourly job scheduler
OpenWeatherMap API Real-time weather data source
requests HTTP calls to the weather API

โš™๏ธ Requirements

  • Python 3.7+
  • pyTelegramBotAPI
  • APScheduler
  • requests

๐Ÿš€ Getting Started

Step 1: Get Your API Keys

Telegram Bot Token

  1. Open Telegram and message @BotFather
  2. Send /newbot and follow the prompts
  3. Copy your bot token

OpenWeatherMap API Key

  1. Sign up at openweathermap.org
  2. Go to API Keys in your account dashboard
  3. Copy your free API key

Step 2: Clone the Repository

git clone https://github.com/MusaIslamFahad/telegram-weather-bot.git
cd telegram-weather-bot

Step 3: Install Dependencies

pip install pyTelegramBotAPI APScheduler requests

Step 4: Set Environment Variables

Option A: Export directly (Linux / macOS)

export BOT_TOKEN="your_telegram_bot_token"
export WEATHER_API_KEY="your_openweathermap_api_key"

Option B: Set in terminal (Windows CMD)

set BOT_TOKEN=your_telegram_bot_token
set WEATHER_API_KEY=your_openweathermap_api_key

Option C: Use a .env file (recommended)

Create a .env file in the project root:

BOT_TOKEN=your_telegram_bot_token
WEATHER_API_KEY=your_openweathermap_api_key

Then install python-dotenv and load it at the top of the script:

from dotenv import load_dotenv
load_dotenv()

โš ๏ธ Security: Always add .env to your .gitignore โ€” never commit API keys to GitHub.


Step 5: Run the Bot

python "Telegram_bot/Telegram Weather Bot.py"

Then open Telegram, find your bot, and send /start.


๐Ÿ”ง Customization

Change the City

In the bot script, find the API call and update the city name:

city = "Dhaka"   # Change to any city, e.g. "London", "Tokyo", "New York"

Change the Update Interval

In the APScheduler configuration, update the interval (in minutes):

scheduler.add_job(send_weather, 'interval', minutes=60)
# Change 60 to any value, e.g. 30 for every 30 minutes

๐Ÿ“‚ Project Structure

telegram-weather-bot/
โ”‚
โ”œโ”€โ”€ Telegram_bot/
โ”‚   โ””โ”€โ”€ Telegram Weather Bot.py   # Main bot script
โ”‚
โ”œโ”€โ”€ .env                          # API keys โ€” DO NOT commit (add to .gitignore)
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ screenshots/                  # README screenshots
โ””โ”€โ”€ README.md

Recommended .gitignore:

.env
__pycache__/
*.pyc

๐Ÿ”ฎ Future Enhancements

  • ๐ŸŒ† Multi-city support: let users request weather for any city via command
  • โฐ Custom intervals: allow users to set their own update frequency from Telegram
  • ๐Ÿ“… Forecast reports: add 3-day or 7-day weather forecasts
  • ๐ŸŒ Inline queries: quick city lookups without switching to the bot chat
  • ๐Ÿณ Docker support: containerize for easy deployment on any server
  • โ˜๏ธ Cloud deployment: host on AWS / Heroku / Railway so the bot runs 24/7 without keeping your PC on

๐Ÿค Contributing

Contributions and suggestions are welcome!

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/your-feature)
  3. Commit your changes (git commit -m 'Add your feature')
  4. Push to the branch (git push origin feature/your-feature)
  5. Open a Pull Request

๐Ÿ™ Acknowledgments


๐Ÿ‘จโ€๐Ÿ’ป Author

Musa Islam Fahad


โญ If you found this useful or built something cool on top of it, drop a star. It helps a lot!

About

A Python Telegram bot that fetches real-time weather data from OpenWeatherMap and automatically delivers hourly reports to your Telegram chat using APScheduler.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages