Download telegram_bot.py from DeMaking/decision-helper-bot: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/spaces/DeMaking/decision-helper-bot/resolve/f83b63817d90dcafe111f72cea7e8c4e2fe71f2e/telegram_bot.py
- Command line
-
hf download hf://spaces/DeMaking/decision-helper-bot@f83b63817d90dcafe111f72cea7e8c4e2fe71f2e/telegram_bot.py
-
curl -L -o telegram_bot.py https://huggingface.co/spaces/DeMaking/decision-helper-bot/resolve/f83b63817d90dcafe111f72cea7e8c4e2fe71f2e/telegram_bot.py
1.78 kB
| import logging | |
| import os | |
| import requests | |
| from telegram import Update | |
| from telegram.ext import Application, CommandHandler, MessageHandler, filters | |
| # Set up logging | |
| logging.basicConfig(level=logging.INFO) | |
| # Get the Telegram Bot Token from environment variables | |
| TOKEN = os.getenv("TELEGRAM_BOT_TOKEN") | |
| if not TOKEN: | |
| raise ValueError("Missing Telegram Bot Token. Please set TELEGRAM_BOT_TOKEN in environment variables.") | |
| # Flask API URL | |
| API_URL = "https://demaking-decision-helper-bot.hf.space/ask" | |
| # Function to send user input to Flask API and return the response | |
| def get_response_from_api(user_text): | |
| try: | |
| response = requests.post(API_URL, json={"text": user_text}) | |
| if response.status_code == 200: | |
| return response.json().get("response", "Error: No response received") # Gets the response or error if response not provided) | |
| else: | |
| return "Error: API request failed" | |
| except Exception as e: | |
| logging.error(f"Error connecting to API: {e}") | |
| return "Error: Could not connect to the server" | |
| # Function to handle incoming messages from users | |
| def handle_message(update: Update): | |
| user_text = update.message.text | |
| response = get_response_from_api(user_text) | |
| update.message.reply_text(response) | |
| # Function to handle /start command | |
| def start(update: Update): | |
| update.message.reply_text("Hello! Tell me your Decision-Making issue in HE or EN, and I'll try to help you.") | |
| # Initialize Telegram bot | |
| def run_telegram_bot(): | |
| app = Application.builder().token(TOKEN).build() | |
| app.add_handler(CommandHandler("start", start)) | |
| app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) | |
| app.run_polling() | |
| if __name__ == "__main__": | |
| run_telegram_bot() | |