Upload 3 files
Browse files- Dockerfile +8 -5
- bot_telegram.py +13 -6
Dockerfile
CHANGED
|
@@ -4,14 +4,17 @@ FROM python:3.10-slim
|
|
| 4 |
# Set the working directory inside the container
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
-
# Copy
|
| 8 |
-
COPY . .
|
| 9 |
|
| 10 |
-
# Install dependencies
|
| 11 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
|
|
|
| 14 |
EXPOSE 7860
|
| 15 |
|
| 16 |
-
# Start the bot
|
| 17 |
CMD ["python", "bot_telegram.py"]
|
|
|
|
| 4 |
# Set the working directory inside the container
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
+
# Copy only necessary files first (for faster caching)
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
|
| 10 |
+
# Install dependencies before copying everything
|
| 11 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
|
| 13 |
+
# Now copy the rest of the bot files
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# Expose a dummy port (Hugging Face requires this)
|
| 17 |
EXPOSE 7860
|
| 18 |
|
| 19 |
+
# Start the bot
|
| 20 |
CMD ["python", "bot_telegram.py"]
|
bot_telegram.py
CHANGED
|
@@ -5,6 +5,9 @@ import requests
|
|
| 5 |
from telegram import Update
|
| 6 |
from telegram.ext import Application, CommandHandler, MessageHandler, filters, CallbackContext
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
# Load environment variables from Hugging Face Secrets
|
| 9 |
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
| 10 |
BASE_URL = os.getenv("BASE_URL")
|
|
@@ -31,15 +34,16 @@ class TelegramBot:
|
|
| 31 |
self.setup_handlers()
|
| 32 |
|
| 33 |
# Authenticate on startup
|
|
|
|
| 34 |
self.authenticate()
|
| 35 |
|
| 36 |
def authenticate(self):
|
| 37 |
"""Authenticate with the API and retrieve an access token."""
|
| 38 |
payload = {"username": self.username, "password": self.password}
|
| 39 |
-
headers = {"Content-Type": "application/json", "accept": "application/json"}
|
| 40 |
|
| 41 |
try:
|
| 42 |
-
response = requests.post(self.login_url, headers=headers, json=payload)
|
| 43 |
|
| 44 |
if response.status_code == 200:
|
| 45 |
self.auth_token = response.json().get("access_token")
|
|
@@ -74,7 +78,7 @@ class TelegramBot:
|
|
| 74 |
payload = {"question": user_message}
|
| 75 |
|
| 76 |
try:
|
| 77 |
-
response = requests.post(self.ai_url, headers=headers, json=payload)
|
| 78 |
|
| 79 |
if response.status_code == 200:
|
| 80 |
bot_reply = response.json().get("answer", "I didn't understand that.")
|
|
@@ -100,9 +104,12 @@ class TelegramBot:
|
|
| 100 |
|
| 101 |
def run(self):
|
| 102 |
"""Start the bot and listen for messages."""
|
| 103 |
-
logging.info("
|
| 104 |
-
asyncio.set_event_loop(asyncio.new_event_loop())
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
bot = TelegramBot(
|
|
|
|
| 5 |
from telegram import Update
|
| 6 |
from telegram.ext import Application, CommandHandler, MessageHandler, filters, CallbackContext
|
| 7 |
|
| 8 |
+
# Configure logging
|
| 9 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 10 |
+
|
| 11 |
# Load environment variables from Hugging Face Secrets
|
| 12 |
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
| 13 |
BASE_URL = os.getenv("BASE_URL")
|
|
|
|
| 34 |
self.setup_handlers()
|
| 35 |
|
| 36 |
# Authenticate on startup
|
| 37 |
+
logging.info("Authenticating with API...")
|
| 38 |
self.authenticate()
|
| 39 |
|
| 40 |
def authenticate(self):
|
| 41 |
"""Authenticate with the API and retrieve an access token."""
|
| 42 |
payload = {"username": self.username, "password": self.password}
|
| 43 |
+
headers = {"Content-Type": "application/json", "accept": "application/json"} # Ensure JSON format
|
| 44 |
|
| 45 |
try:
|
| 46 |
+
response = requests.post(self.login_url, headers=headers, json=payload) # Send JSON request
|
| 47 |
|
| 48 |
if response.status_code == 200:
|
| 49 |
self.auth_token = response.json().get("access_token")
|
|
|
|
| 78 |
payload = {"question": user_message}
|
| 79 |
|
| 80 |
try:
|
| 81 |
+
response = requests.post(self.ai_url, headers=headers, json=payload) # Send JSON request
|
| 82 |
|
| 83 |
if response.status_code == 200:
|
| 84 |
bot_reply = response.json().get("answer", "I didn't understand that.")
|
|
|
|
| 104 |
|
| 105 |
def run(self):
|
| 106 |
"""Start the bot and listen for messages."""
|
| 107 |
+
logging.info("Starting Telegram bot...")
|
| 108 |
+
asyncio.set_event_loop(asyncio.new_event_loop()) # Fixes async event loop issue
|
| 109 |
+
try:
|
| 110 |
+
self.app.run_polling()
|
| 111 |
+
except Exception as e:
|
| 112 |
+
logging.error(f"Bot failed to start: {e}")
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
bot = TelegramBot(
|