diff --git a/Advanced_Projects/Telegram_Bot/README.md b/Advanced_Projects/Telegram_Bot/README.md new file mode 100644 index 0000000000..e30ec8e136 --- /dev/null +++ b/Advanced_Projects/Telegram_Bot/README.md @@ -0,0 +1,72 @@ +# Advanced Telegram Bot Project + +This project is a feature-rich Telegram bot built using the `python-telegram-bot` library. The bot provides a variety of interactive commands and functionalities, such as weather updates, motivational quotes, and more. + +## Features + +- **Basic Commands**: Includes `/start`, `/help`, and `/info` commands to guide users. +- **Weather Updates**: Provides real-time weather information for any city, using an external API. +- **Motivational Quotes**: Offers daily motivational quotes with an option to get more via inline buttons. +- **Interactive Elements**: Inline keyboards for quick interactions, making the bot more engaging. +- **Error Handling**: Comprehensive logging and error handling for smooth operation. + +## Setup and Installation + +### 1. Install Dependencies + +Ensure you have Python 3 installed. Then, install the required libraries: + +```bash +pip install -r requirements.txt +``` + +### 2. Get API Keys + +- **Telegram Bot API Key**: Create a bot through [BotFather on Telegram](https://core.telegram.org/bots#botfather) and get the token. +- **Weather API Key**: Sign up on [WeatherAPI](https://www.weatherapi.com/) or [OpenWeatherMap](https://openweathermap.org/) to obtain an API key. + +### 3. Configure API Keys + +Replace placeholders in `app.py` with your actual API keys: + +```python +WEATHER_API_KEY = 'your_weather_api_key' +TELEGRAM_TOKEN = 'your_telegram_bot_token' +``` + +### 4. Run the Bot + +Run the bot using: + +```bash +python app.py +``` + +## Usage + +Once the bot is running, you can interact with it on Telegram: + +- **/start** - Starts the bot and displays a welcome message. +- **/help** - Lists available commands. +- **/weather ``** - Retrieves the current weather for a specified city. +- **/motivate** - Sends a motivational quote with an inline option to get more quotes. + +## Example Interactions + +- `/start`: "Welcome! Use /help to see available commands." +- `/weather London`: "Weather in London: 18°C, Partly Cloudy" +- `/motivate`: "Stay positive, work hard, make it happen!" with a button for more quotes. + +## Project Structure + +``` +├── app.py # Main bot code +├── README.md # Project documentation +└── requirements.txt # Dependencies +``` + +## Troubleshooting + +- **TypeError**: If you encounter issues with the `Updater` class, ensure you're using `Application` instead (for `python-telegram-bot` v20+). +- **Network/API Errors**: Ensure API keys are correctly configured and active. + diff --git a/Advanced_Projects/Telegram_Bot/app.py b/Advanced_Projects/Telegram_Bot/app.py new file mode 100644 index 0000000000..b324291df6 --- /dev/null +++ b/Advanced_Projects/Telegram_Bot/app.py @@ -0,0 +1,64 @@ +import logging +import requests +from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup +from telegram.ext import Application, CommandHandler, CallbackQueryHandler, ContextTypes + +# Enable logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +# Constants +WEATHER_API_KEY = 'your_weather_api_key' +TELEGRAM_TOKEN = 'your_telegram_bot_token' + +# Start command +async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + await update.message.reply_text("Welcome! Use /help to see available commands.") + +# Help command +async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + help_text = "/start - Start the bot\n/help - Get help\n/weather - Get current weather\n/motivate - Get daily motivation" + await update.message.reply_text(help_text) + +# Weather command with API integration +async def weather(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + city = ' '.join(context.args) if context.args else 'London' + response = requests.get(f"http://api.weatherapi.com/v1/current.json?key={WEATHER_API_KEY}&q={city}") + if response.status_code == 200: + data = response.json() + weather_info = f"Weather in {data['location']['name']}: {data['current']['temp_c']}°C, {data['current']['condition']['text']}" + await update.message.reply_text(weather_info) + else: + await update.message.reply_text("Could not retrieve weather data.") + +# Motivation command with inline keyboard +async def motivate(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + quote = "Stay positive, work hard, make it happen!" + keyboard = [[InlineKeyboardButton("Get Another", callback_data='new_motivation')]] + reply_markup = InlineKeyboardMarkup(keyboard) + await update.message.reply_text(quote, reply_markup=reply_markup) + +# Callback for inline button to get new quote +async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + query = update.callback_query + await query.answer() + new_quote = "Believe in yourself and all that you are!" + await query.edit_message_text(text=new_quote) + +# Main function to start the bot +def main() -> None: + # Create the Application instance + application = Application.builder().token(TELEGRAM_TOKEN).build() + + # Commands + application.add_handler(CommandHandler("start", start)) + application.add_handler(CommandHandler("help", help_command)) + application.add_handler(CommandHandler("weather", weather)) + application.add_handler(CommandHandler("motivate", motivate)) + application.add_handler(CallbackQueryHandler(button)) + + # Start the bot + application.run_polling() + +if __name__ == '__main__': + main() diff --git a/Advanced_Projects/Telegram_Bot/requirements.txt b/Advanced_Projects/Telegram_Bot/requirements.txt new file mode 100644 index 0000000000..c0d195ab71 --- /dev/null +++ b/Advanced_Projects/Telegram_Bot/requirements.txt @@ -0,0 +1,2 @@ +python-telegram-bot +requests \ No newline at end of file