Skip to content

SecureAuditX/weather-telegram-bot

Repository files navigation

🌦️ Telegram Weather Bot

Python PostgreSQL Telegram Bot License: MIT

A smart Telegram bot that provides real-time weather updates using the OpenWeatherMap API and stores weather queries in a PostgreSQL database for future insights and analytics.

bot photo

📌 Features

  • ✅ Get real-time weather for any city by simply messaging the bot
  • ✅ Returns temperature and weather condition in human-readable format
  • ✅ Saves each query into a PostgreSQL database with timestamp
  • ✅ Clean modular Python architecture
  • ✅ Easily deployable on cloud platforms like Railway or Fly.io
  • ✅ Error-handling for invalid cities and API issues

🧱 Tech Stack

Layer Technology
Programming Python 3.10+
Messaging API Telegram Bot API
Weather API OpenWeatherMap API
Database PostgreSQL
Bot Framework python-telegram-bot v20+
Hosting Railway / Fly.io

🗂️ Project Structure

weather_bot/
├── telegram_bot.py       # Main Telegram bot logic
├── weather_api.py        # Weather API call logic
├── db.py                 # PostgreSQL DB connection and insertion
├── config.py             # Stores API keys and DB credentials ***But Disabled for security purposes***
├── requirements.txt      # Python dependencies
└── README.md             # Project documentation

⚙️ Setup Instructions

📌 Prerequisites

🔧 1. Clone the repository

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

🔧 2. Create a virtual environment

python -m venv venv
source venv/bin/activate    # On Windows: venv\Scripts\activate

🔧 3. Install dependencies

pip install -r requirements.txt

🔧 4. Configure your config.py

# config.py

TELEGRAM_BOT_TOKEN = "your_telegram_token"
WEATHER_API_KEY = "your_openweathermap_api_key"

DB_NAME = "your_db_name"
DB_USER = "your_db_user"
DB_PASSWORD = "your_db_password"
DB_HOST = "localhost"
DB_PORT = "5432"

🔧 5. Initialize your PostgreSQL database

Run this SQL manually or inside init_db():

CREATE TABLE IF NOT EXISTS weather (
    id SERIAL PRIMARY KEY,
    city TEXT,
    temperature REAL,
    condition TEXT,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

🚀 Run the bot

python telegram_bot.py

🌐 Deploy to Railway (Optional)

  1. Push your project to GitHub
  2. Go to Railway.app
  3. Connect your GitHub repo
  4. Add environment variables:
    • TELEGRAM_BOT_TOKEN
    • WEATHER_API_KEY
    • PostgreSQL credentials
  5. Set start command to:
python telegram_bot.py

🧪 Sample Output

Telegram:

User: Paris
Bot: 📍 Weather in Paris:
🌡️ 23.4°C
🌤️ scattered clouds

Terminal:

🤖 Bot is running...
✅ Received message: Paris

Database Record:

id city temperature condition timestamp
1 Paris 23.4 scattered clouds 2025-07-12 10:42:00

🙌 Contributing

Contributions are welcome!

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

📄 License

This project is licensed under the MIT License.


💡 Ideas for Expansion

  • Add weather history retrieval
  • User-specific logs
  • Weather alerts for extreme conditions
  • Multilingual support
  • Deploy with webhook instead of polling
  • Frontend dashboard (Streamlit or Flask)

✨ Author

Made with ❤️ by SecureAuditX

About

Telegram Weather Bot using Python, PostgreSQL, and OpenWeatherMap API.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages