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.
- ✅ 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
| 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 |
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- Python 3.10+ installed
- Telegram bot token from @BotFather
- OpenWeatherMap API key from https://openweathermap.org/api
- PostgreSQL database (local or cloud)
git clone https://github.com/SecureAuditX/weather-telegram-bot.git
cd weather-telegram-botpython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txt# 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"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
);python telegram_bot.py- Push your project to GitHub
- Go to Railway.app
- Connect your GitHub repo
- Add environment variables:
TELEGRAM_BOT_TOKENWEATHER_API_KEY- PostgreSQL credentials
- Set start command to:
python telegram_bot.pyTelegram:
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 |
Contributions are welcome!
- Fork this repo
- Create your feature branch (
git checkout -b feature/cool-feature) - Commit your changes (
git commit -m 'Add cool feature') - Push to the branch (
git push origin feature/cool-feature) - Open a Pull Request
This project is licensed under the MIT License.
- 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)
Made with ❤️ by SecureAuditX
