Create bot.py
Browse files
bot.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
import httpx
|
4 |
+
from telegram import Update
|
5 |
+
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
6 |
+
import os
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# Replace this with your Hugging Face Space URL
|
11 |
+
HUGGING_FACE_SPACE_URL = "https://demaking-decision-helper-bot.hf.space"
|
12 |
+
|
13 |
+
# Get Telegram bot token from environment variables
|
14 |
+
TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
15 |
+
if not TOKEN:
|
16 |
+
raise ValueError("Missing Telegram Bot Token. Please set TELEGRAM_BOT_TOKEN environment variable.")
|
17 |
+
|
18 |
+
|
19 |
+
async def call_hugging_face_space(input_data: str):
|
20 |
+
async with httpx.AsyncClient(timeout=45.0) as client:
|
21 |
+
try:
|
22 |
+
response = await client.post(HUGGING_FACE_SPACE_URL, json={"input": input_data})
|
23 |
+
response.raise_for_status() # Raise an error for bad responses
|
24 |
+
return response.json()
|
25 |
+
except httpx.HTTPStatusError as e:
|
26 |
+
print(f"HTTP error occurred: {e}")
|
27 |
+
return {"error": str(e)}
|
28 |
+
except httpx.ConnectError as e:
|
29 |
+
print(f"Connection error: {e}")
|
30 |
+
return {"error": "Could not connect to the Hugging Face Space"}
|
31 |
+
except Exception as e:
|
32 |
+
print(f"An error occurred: {e}")
|
33 |
+
return {"error": str(e)}
|
34 |
+
|
35 |
+
|
36 |
+
@app.post("/webhook/{token}")
|
37 |
+
async def webhook(token: str, request: Request):
|
38 |
+
if token != TOKEN:
|
39 |
+
return JSONResponse(status_code=403, content={"message": "Forbidden"})
|
40 |
+
|
41 |
+
update = Update.de_json(await request.json(), None)
|
42 |
+
message_text = update.message.text
|
43 |
+
|
44 |
+
result = await call_hugging_face_space(message_text)
|
45 |
+
|
46 |
+
return JSONResponse(content=result)
|
47 |
+
|
48 |
+
|
49 |
+
def start_telegram_bot():
|
50 |
+
application = ApplicationBuilder().token(TOKEN).build()
|
51 |
+
|
52 |
+
# Set up a command handler
|
53 |
+
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
54 |
+
await update.message.reply_text("Hello! Tell me your decision-making issue, and I'll try to help.")
|
55 |
+
print("Start command received.")
|
56 |
+
|
57 |
+
|
58 |
+
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
59 |
+
user_text = update.message.text
|
60 |
+
print(f"User message: {user_text}")
|
61 |
+
|
62 |
+
# Send the user text to the FastAPI server and get the response.
|
63 |
+
result = await call_hugging_face_space(user_text)
|
64 |
+
response_text = result.get("response", "Error generating response.")
|
65 |
+
|
66 |
+
print(f"API Response: {response_text}")
|
67 |
+
await update.message.reply_text(response_text)
|
68 |
+
|
69 |
+
application.add_handler(CommandHandler("start", start))
|
70 |
+
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
71 |
+
|
72 |
+
# Start the bot
|
73 |
+
application.run_polling()
|
74 |
+
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
import threading
|
78 |
+
|
79 |
+
# Start the Telegram bot in a separate thread
|
80 |
+
threading.Thread(target=start_telegram_bot).start()
|
81 |
+
|
82 |
+
# Start the FastAPI app
|
83 |
+
import uvicorn
|
84 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
85 |
+
|