Update bot_from_replit.py
Browse files- bot_from_replit.py +2 -50
bot_from_replit.py
CHANGED
@@ -3,9 +3,7 @@ from telegram import Update
|
|
3 |
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
4 |
from utils import clean_text
|
5 |
from os import environ
|
6 |
-
import httpx
|
7 |
from app import generate_response, detect_language
|
8 |
-
from responses import get_response
|
9 |
|
10 |
|
11 |
# Configure logging
|
@@ -21,35 +19,6 @@ if not TOKEN:
|
|
21 |
raise ValueError("No TELEGRAM_BOT_TOKEN found in environment variables!")
|
22 |
|
23 |
|
24 |
-
# -------------------------
|
25 |
-
# API URL of the FastAPI server (running on Hugging Face)
|
26 |
-
# -------------------------
|
27 |
-
#API_URL = "https://demaking-decision-helper-bot.hf.space/generate_response"
|
28 |
-
|
29 |
-
|
30 |
-
# -------------------------
|
31 |
-
# Function to fetch response from FastAPI
|
32 |
-
# -------------------------
|
33 |
-
# async def fetch_response(user_text: str):
|
34 |
-
# """
|
35 |
-
# Sends a POST request to the FastAPI API with the user's text and returns the JSON response.
|
36 |
-
# """
|
37 |
-
# async with httpx.AsyncClient(timeout=45.0) as client:
|
38 |
-
# try:
|
39 |
-
# response = await client.post(API_URL, json={"text": user_text})
|
40 |
-
# response.raise_for_status() # Raise exception for HTTP 4XX/5XX errors
|
41 |
-
# return response.json()
|
42 |
-
# except httpx.HTTPStatusError as e:
|
43 |
-
# logger.error(f"HTTP Error: {e.response.status_code} - {e.response.text}")
|
44 |
-
# return {"response": "Error: API returned an error."}
|
45 |
-
# except httpx.RequestError as e:
|
46 |
-
# logger.error(f"Request Error: {e}")
|
47 |
-
# return {"response": "Error: Could not reach API."}
|
48 |
-
# except Exception as e:
|
49 |
-
# logger.error(f"Unexpected Error: {e}")
|
50 |
-
# return {"response": "Error: Unexpected error occurred."}
|
51 |
-
|
52 |
-
|
53 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
54 |
"""Handler for the /start command."""
|
55 |
if not update.message or not update.message.text:
|
@@ -79,25 +48,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
79 |
logger.error(f"Error processing message: {e}")
|
80 |
|
81 |
|
82 |
-
|
83 |
-
# result = await fetch_response(user_text)
|
84 |
-
# response_text = result.get("response", "Error generating response.")
|
85 |
-
|
86 |
-
# logger.info(f"API Response: {response_text}")
|
87 |
-
# await update.message.reply_text(response_text)
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
# async def decide(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
92 |
-
# """Handle the /decide command."""
|
93 |
-
# if not context.args:
|
94 |
-
# user_lang = detect_language(update.message.from_user.language_code)
|
95 |
-
# await update.message.reply_text(get_response('decide_help', user_lang))
|
96 |
-
# return
|
97 |
-
|
98 |
-
# question = ' '.join(context.args)
|
99 |
-
# response = generate_response(question)
|
100 |
-
# await update.message.reply_text(response)
|
101 |
|
102 |
|
103 |
# async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
@@ -127,6 +78,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
127 |
# except Exception as e:
|
128 |
# logger.error(f"Error in error handler: {e}")
|
129 |
|
|
|
130 |
def main():
|
131 |
"""Start the bot."""
|
132 |
|
|
|
3 |
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
4 |
from utils import clean_text
|
5 |
from os import environ
|
|
|
6 |
from app import generate_response, detect_language
|
|
|
7 |
|
8 |
|
9 |
# Configure logging
|
|
|
19 |
raise ValueError("No TELEGRAM_BOT_TOKEN found in environment variables!")
|
20 |
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
23 |
"""Handler for the /start command."""
|
24 |
if not update.message or not update.message.text:
|
|
|
48 |
logger.error(f"Error processing message: {e}")
|
49 |
|
50 |
|
51 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
|
54 |
# async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
|
78 |
# except Exception as e:
|
79 |
# logger.error(f"Error in error handler: {e}")
|
80 |
|
81 |
+
|
82 |
def main():
|
83 |
"""Start the bot."""
|
84 |
|