Create bot_from_replit.py
Browse files- bot_from_replit.py +150 -0
bot_from_replit.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
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
|
| 12 |
+
logging.basicConfig(
|
| 13 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 14 |
+
level=logging.INFO
|
| 15 |
+
)
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
+
|
| 18 |
+
# Get token from environment variable
|
| 19 |
+
TOKEN = environ.get("TELEGRAM_BOT_TOKEN")
|
| 20 |
+
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:
|
| 56 |
+
return
|
| 57 |
+
await update.message.reply_text("Hello! Tell me your decision-making issue, and I'll try to help.")
|
| 58 |
+
logger.info("Start command received.")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# -------------------------
|
| 62 |
+
# Message handler for incoming text messages
|
| 63 |
+
# -------------------------
|
| 64 |
+
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 65 |
+
"""
|
| 66 |
+
Handler for incoming messages.
|
| 67 |
+
Sends the user's message to the API and replies with the response.
|
| 68 |
+
"""
|
| 69 |
+
try:
|
| 70 |
+
if not update.message or not update.message.text:
|
| 71 |
+
return
|
| 72 |
+
|
| 73 |
+
user_text = clean_text(update.message.text)
|
| 74 |
+
logger.info(f"User message: {user_text}")
|
| 75 |
+
|
| 76 |
+
response = generate_response(user_text)
|
| 77 |
+
await update.message.reply_text(response)
|
| 78 |
+
except Exception as e:
|
| 79 |
+
logger.error(f"Error processing message: {e}")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# Send the user text to the FastAPI server and get the response.
|
| 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):
|
| 104 |
+
# """Handle regular messages."""
|
| 105 |
+
# if not update.message or not update.message.text:
|
| 106 |
+
# return
|
| 107 |
+
|
| 108 |
+
# text = clean_text(update.message.text)
|
| 109 |
+
# if '?' in text:
|
| 110 |
+
# response = generate_response(text)
|
| 111 |
+
# else:
|
| 112 |
+
# user_lang = detect_language(text)
|
| 113 |
+
# response = get_response('no_question', user_lang)
|
| 114 |
+
|
| 115 |
+
# await update.message.reply_text(response)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# async def error_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 119 |
+
# """Handle errors."""
|
| 120 |
+
# logger.error(f"Error occurred: {context.error}")
|
| 121 |
+
# try:
|
| 122 |
+
# if update and update.message:
|
| 123 |
+
# user_lang = detect_language(update.message.from_user.language_code)
|
| 124 |
+
# await update.message.reply_text(
|
| 125 |
+
# get_response('error', user_lang)
|
| 126 |
+
# )
|
| 127 |
+
# except Exception as e:
|
| 128 |
+
# logger.error(f"Error in error handler: {e}")
|
| 129 |
+
|
| 130 |
+
def main():
|
| 131 |
+
"""Start the bot."""
|
| 132 |
+
|
| 133 |
+
# Create the application
|
| 134 |
+
application = Application.builder().token(TOKEN).build()
|
| 135 |
+
|
| 136 |
+
# Add handlers
|
| 137 |
+
application.add_handler(CommandHandler("start", start))
|
| 138 |
+
# application.add_handler(CommandHandler("help", help_command))
|
| 139 |
+
# application.add_handler(CommandHandler("decide", decide))
|
| 140 |
+
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 141 |
+
|
| 142 |
+
# Add error handler
|
| 143 |
+
# application.add_error_handler(error_handler)
|
| 144 |
+
|
| 145 |
+
# Start the bot
|
| 146 |
+
application.run_polling(allowed_updates=Update.ALL_TYPES)
|
| 147 |
+
|
| 148 |
+
if __name__ == '__main__':
|
| 149 |
+
main()
|
| 150 |
+
|