|
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 |
|
) |
|
|
|
|
|
|
|
|
|
logging.basicConfig( |
|
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO |
|
) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
TOKEN = os.getenv("TELEGRAM_BOT_TOKEN") |
|
if not TOKEN: |
|
raise ValueError("Missing Telegram Bot Token. Please set TELEGRAM_BOT_TOKEN environment variable.") |
|
|
|
|
|
WEBHOOK_DOMAIN = os.getenv("WEBHOOK_DOMAIN") |
|
if not WEBHOOK_DOMAIN: |
|
raise ValueError("Missing Webhook Domain. Please set WEBHOOK_DOMAIN environment variable.") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WEBHOOK_URL = f"https://{WEBHOOK_DOMAIN}" |
|
TELEGRAM_WEBHOOK = f"https://api.telegram.org/bot{TOKEN}/setwebhook?url=https://{WEBHOOK_URL}" |
|
|
|
|
|
|
|
|
|
|
|
API_URL = "https://demaking-decision-helper-bot.hf.space/generate_response" |
|
|
|
|
|
|
|
|
|
|
|
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() |
|
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."} |
|
|
|
|
|
|
|
|
|
|
|
async def start(update: Update, context: CallbackContext): |
|
|
|
await update.message.reply_text("Hello! Tell me your decision-making issue, and I'll try to help.") |
|
logger.info("Start command received.") |
|
|
|
|
|
|
|
|
|
|
|
async def handle_message(update: Update, context: CallbackContext): |
|
user_text = update.message.text |
|
logger.info(f"User message: {user_text}") |
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def set_webhook(): |
|
bot = Bot(token=TOKEN) |
|
bot.remove_webhook() |
|
|
|
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}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main(): |
|
await set_webhook() |
|
|
|
application = Application.builder().token(TOKEN).build() |
|
|
|
|
|
application.add_handler(CommandHandler("start", start)) |
|
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) |
|
|
|
|
|
logger.info("Starting bot in webhook mode...") |
|
|
|
|
|
|
|
|
|
|
|
|
|
await application.run_webhook( |
|
listen="0.0.0.0", |
|
port=443, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
nest_asyncio.apply() |
|
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop() |
|
loop.run_until_complete(main()) |
|
|