baha-99 commited on
Commit
ccc4726
·
verified ·
1 Parent(s): c31b9c5

Upload 3 files

Browse files
Files changed (2) hide show
  1. Dockerfile +8 -5
  2. 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 bot files into the container
8
- COPY . .
9
 
10
- # Install dependencies
11
  RUN pip install --no-cache-dir -r requirements.txt
12
 
13
- # Expose a dummy port (Hugging Face requires an exposed port)
 
 
 
14
  EXPOSE 7860
15
 
16
- # Start the bot and keep the container alive
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("Bot is now listening for messages")
104
- asyncio.set_event_loop(asyncio.new_event_loop())
105
- self.app.run_polling()
 
 
 
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(