DeMaking's picture
Update bot.py
641846a verified
raw
history blame
5.69 kB
import os
import logging
import asyncio
import nest_asyncio
import httpx
from telegram import Update, Bot
from telegram.ext import (
Application,
CommandHandler,
MessageHandler,
filters,
CallbackContext
)
# -------------------------
# Configure logging
# -------------------------
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
# -------------------------
# Environment variables
# -------------------------
# Get the Telegram Bot Token
TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
if not TOKEN:
raise ValueError("Missing Telegram Bot Token. Please set TELEGRAM_BOT_TOKEN environment variable.")
# Get the domain for webhook (publicly accessible, e.g., your-space.hf.space)
WEBHOOK_DOMAIN = os.getenv("WEBHOOK_DOMAIN")
if not WEBHOOK_DOMAIN:
raise ValueError("Missing Webhook Domain. Please set WEBHOOK_DOMAIN environment variable.")
# -------------------------
# Webhook configuration
# -------------------------
# Define a unique webhook path using the bot token
# WEBHOOK_PATH = f"/{TOKEN}"
# Construct the full webhook URL (must be HTTPS as required by Telegram)
WEBHOOK_URL = f"https://{WEBHOOK_DOMAIN}" # {WEBHOOK_PATH}"
TELEGRAM_WEBHOOK = f"https://api.telegram.org/bot{TOKEN}/setwebhook?url=https://{WEBHOOK_URL}"
# -------------------------
# API URL of the FastAPI server (running on Hugging Face)
# -------------------------
API_URL = "https://demaking-decision-helper-bot.hf.space/generate_response"
# -------------------------
# Function to fetch response from FastAPI (unchanged)
# -------------------------
async def fetch_response(user_text: str):
async with httpx.AsyncClient(timeout=45.0) as client:
try:
response = await client.post(API_URL, json={"text": user_text})
response.raise_for_status() # Raise exception for HTTP 4XX/5XX errors
return response.json()
except httpx.HTTPStatusError as e:
logger.error(f"HTTP Error: {e.response.status_code} - {e.response.text}")
return {"response": "Error: API returned an error."}
except httpx.RequestError as e:
logger.error(f"Request Error: {e}")
return {"response": "Error: Could not reach API."}
except Exception as e:
logger.error(f"Unexpected Error: {e}")
return {"response": "Error: Unexpected error occurred."}
# -------------------------
# Command handler for /start
# -------------------------
async def start(update: Update, context: CallbackContext):
# Respond to the /start command.
await update.message.reply_text("Hello! Tell me your decision-making issue, and I'll try to help.")
logger.info("Start command received.")
# -------------------------
# Message handler for incoming text messages
# -------------------------
async def handle_message(update: Update, context: CallbackContext):
user_text = update.message.text
logger.info(f"User message: {user_text}")
# Send the user text to the FastAPI server and get the response.
result = await fetch_response(user_text)
response_text = result.get("response", "Error generating response.")
logger.info(f"API Response: {response_text}")
await update.message.reply_text(response_text)
# def webhook():
# bot.remove_webhook()
# bot.set_webhook(url=WEBHOOL_URL + TOKEN) # ื”ื’ื“ืจ ืืช ื”-Webhook
# print("webhook set: ", WEBHOOL_URL + TOKEN)
# return "!", 200
# -------------------------
# Optional: Manually set webhook using bot.set_webhook()
# -------------------------
async def set_webhook():
bot = Bot(token=TOKEN)
bot.remove_webhook()
# This call will set the webhook to the given URL.
PATH = WEBHOOK_URL + TOKEN
try:
await bot.set_webhook(url=PATH)
logger.info(f"Webhook set successfully to: {WEBHOOK_URL}")
print("bot webhook success")
except Exception as e:
logger.error(f"Failed to set webhook manually. Error: {e}")
print(f"error setting bot webhook. Error: {e}")
# -------------------------
# Main function to run the bot using Webhook mode
# -------------------------
async def main():
await set_webhook()
# Build the Application with the Telegram Bot Token
application = Application.builder().token(TOKEN).build()
# Add command and message handlers
application.add_handler(CommandHandler("start", start))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
# Log: starting the bot in webhook mode.
logger.info("Starting bot in webhook mode...")
# Run the application using webhook mode.
# The bot will listen on all interfaces (0.0.0.0) at the specified port (5000 in this example).
# Make sure that the port you choose is allowed by your hosting environment.
await application.run_webhook(
listen="0.0.0.0", # Listen on all available interfaces
port=443, # Port to listen on
# url_path=TELEGRAM_WEBHOOK, # The webhook path; here, we use the bot token
#webhook_url=WEBHOOK_URL # The full webhook URL that Telegram will use to send updates
)
# -------------------------
# Run the main function
# -------------------------
if __name__ == "__main__":
# Apply nest_asyncio to support nested event loops if required.
nest_asyncio.apply()
# asyncio.run(main())
# Instead of asyncio.run(), which may try to close an already running loop,
# get the current loop and run main() until complete.
loop = asyncio.get_event_loop()
loop.run_until_complete(main())