Update main.py
Browse files
main.py
CHANGED
@@ -4,6 +4,7 @@ import asyncio
|
|
4 |
import nest_asyncio
|
5 |
import requests
|
6 |
from hugchat import hugchat
|
|
|
7 |
|
8 |
# Import Telegram bot components
|
9 |
from telegram import Update, Bot
|
@@ -14,7 +15,7 @@ from telegram.ext import Application, CommandHandler, MessageHandler, filters, C
|
|
14 |
# -------------------------
|
15 |
# Expected environment variables:
|
16 |
# TELEGRAM_BOT_TOKEN - Telegram bot token
|
17 |
-
# WEBHOOK_DOMAIN -
|
18 |
# HF_EMAIL and HF_PASSWORD
|
19 |
|
20 |
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
@@ -25,6 +26,15 @@ WEBHOOK_DOMAIN = os.getenv("WEBHOOK_DOMAIN")
|
|
25 |
if not WEBHOOK_DOMAIN:
|
26 |
raise ValueError("Missing WEBHOOK_DOMAIN environment variable.")
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
# -------------------------
|
29 |
# Configure logging
|
30 |
# -------------------------
|
@@ -35,23 +45,26 @@ logger = logging.getLogger(__name__)
|
|
35 |
# Set up Hugging Chat integration
|
36 |
# -------------------------
|
37 |
# Logs into Hugging Face or uses a fallback to get cookies.
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
|
48 |
# Create a ChatBot instance from the hugchat library.
|
49 |
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
50 |
|
|
|
51 |
# -------------------------
|
52 |
# Helper Functions
|
53 |
# -------------------------
|
54 |
-
|
55 |
def detect_language(user_input: str) -> str:
|
56 |
"""
|
57 |
Detect language using langid.
|
@@ -138,6 +151,7 @@ async def handle_message(update: Update, context: CallbackContext):
|
|
138 |
# We use the bot token as the URL path for uniqueness.
|
139 |
WEBHOOK_URL = f"https://{WEBHOOK_DOMAIN}/{TELEGRAM_BOT_TOKEN}"
|
140 |
|
|
|
141 |
# -------------------------
|
142 |
# Main function to run the Telegram Bot using Webhook mode
|
143 |
# -------------------------
|
@@ -160,6 +174,7 @@ async def main():
|
|
160 |
webhook_url=WEBHOOK_URL # Full webhook URL that Telegram will call.
|
161 |
)
|
162 |
|
|
|
163 |
# -------------------------
|
164 |
# Run the main function (handling event loop issues)
|
165 |
# -------------------------
|
|
|
4 |
import nest_asyncio
|
5 |
import requests
|
6 |
from hugchat import hugchat
|
7 |
+
from hugchat.login import Login
|
8 |
|
9 |
# Import Telegram bot components
|
10 |
from telegram import Update, Bot
|
|
|
15 |
# -------------------------
|
16 |
# Expected environment variables:
|
17 |
# TELEGRAM_BOT_TOKEN - Telegram bot token
|
18 |
+
# WEBHOOK_DOMAIN - your-space.hf.space
|
19 |
# HF_EMAIL and HF_PASSWORD
|
20 |
|
21 |
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
|
|
26 |
if not WEBHOOK_DOMAIN:
|
27 |
raise ValueError("Missing WEBHOOK_DOMAIN environment variable.")
|
28 |
|
29 |
+
HF_EMAIL = os.getenv("HF_EMAIL")
|
30 |
+
if not HF_EMAIL:
|
31 |
+
raise ValueError("Missing HF_EMAIL environment variable.")
|
32 |
+
|
33 |
+
HF_PASSWORD = os.getenv("HF_PASSWORD")
|
34 |
+
if not HF_PASSWORD:
|
35 |
+
raise ValueError("Missing HF_PASSWORD environment variable.")
|
36 |
+
|
37 |
+
|
38 |
# -------------------------
|
39 |
# Configure logging
|
40 |
# -------------------------
|
|
|
45 |
# Set up Hugging Chat integration
|
46 |
# -------------------------
|
47 |
# Logs into Hugging Face or uses a fallback to get cookies.
|
48 |
+
try:
|
49 |
+
if os.getenv("HF_EMAIL") and os.getenv("HF_PASSWORD"):
|
50 |
+
from hugchat.login import Login
|
51 |
+
logger.info("Logging into Hugging Face using provided credentials.")
|
52 |
+
sign = Login(HF_EMAIL, HF_PASSWORD)
|
53 |
+
cookies = sign.login()
|
54 |
+
else:
|
55 |
+
logger.info("No HF_EMAIL/HF_PASSWORD provided. Using fallback method to get cookies.")
|
56 |
+
cookies = requests.get("https://huggingface.co/chat/").cookies
|
57 |
+
except Exception as e:
|
58 |
+
logger.error(f"Error in Logging-into Hugging Chat: {e}")
|
59 |
+
|
60 |
|
61 |
# Create a ChatBot instance from the hugchat library.
|
62 |
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
63 |
|
64 |
+
|
65 |
# -------------------------
|
66 |
# Helper Functions
|
67 |
# -------------------------
|
|
|
68 |
def detect_language(user_input: str) -> str:
|
69 |
"""
|
70 |
Detect language using langid.
|
|
|
151 |
# We use the bot token as the URL path for uniqueness.
|
152 |
WEBHOOK_URL = f"https://{WEBHOOK_DOMAIN}/{TELEGRAM_BOT_TOKEN}"
|
153 |
|
154 |
+
|
155 |
# -------------------------
|
156 |
# Main function to run the Telegram Bot using Webhook mode
|
157 |
# -------------------------
|
|
|
174 |
webhook_url=WEBHOOK_URL # Full webhook URL that Telegram will call.
|
175 |
)
|
176 |
|
177 |
+
|
178 |
# -------------------------
|
179 |
# Run the main function (handling event loop issues)
|
180 |
# -------------------------
|