Upload 3 files
Browse files- Dockerfile +17 -0
- bot_telegram.py +80 -0
- requirements.txt +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use the official Python image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Set working directory inside the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy all files from your Hugging Face Space 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 |
+
# Run the bot
|
| 17 |
+
CMD ["python", "bot.py"]
|
bot_telegram.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import logging
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from threading import Thread
|
| 6 |
+
from telegram import Update
|
| 7 |
+
from telegram.ext import Application, CommandHandler, MessageHandler, filters, CallbackContext
|
| 8 |
+
|
| 9 |
+
# Read API credentials from environment variables
|
| 10 |
+
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
| 11 |
+
BASE_URL = os.getenv("BASE_URL")
|
| 12 |
+
API_USERNAME = os.getenv("API_USERNAME")
|
| 13 |
+
API_PASSWORD = os.getenv("API_PASSWORD")
|
| 14 |
+
|
| 15 |
+
class TelegramBot:
|
| 16 |
+
def __init__(self, bot_token, base_url, username, password):
|
| 17 |
+
self.bot_token = bot_token
|
| 18 |
+
self.base_url = base_url
|
| 19 |
+
self.username = username
|
| 20 |
+
self.password = password
|
| 21 |
+
self.auth_token = None
|
| 22 |
+
self.login_url = f"{self.base_url}/api/v1/auth/login"
|
| 23 |
+
self.ai_url = f"{self.base_url}/api/v1/questions/text"
|
| 24 |
+
self.app = Application.builder().token(self.bot_token).build()
|
| 25 |
+
self.setup_handlers()
|
| 26 |
+
self.authenticate()
|
| 27 |
+
|
| 28 |
+
def authenticate(self):
|
| 29 |
+
payload = {"username": self.username, "password": self.password}
|
| 30 |
+
headers = {"Content-Type": "application/x-www-form-urlencoded", "accept": "application/json"}
|
| 31 |
+
try:
|
| 32 |
+
response = requests.post(self.login_url, headers=headers, data=payload)
|
| 33 |
+
if response.status_code == 200:
|
| 34 |
+
self.auth_token = response.json().get("access_token")
|
| 35 |
+
logging.info("Successfully authenticated with API!")
|
| 36 |
+
else:
|
| 37 |
+
logging.error(f"Authentication failed: {response.status_code} - {response.text}")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
logging.error(f"Authentication Error: {e}")
|
| 40 |
+
|
| 41 |
+
async def start_command(self, update: Update, context: CallbackContext):
|
| 42 |
+
await update.message.reply_text("Hello! Ask me any question.")
|
| 43 |
+
|
| 44 |
+
async def chat_with_ai(self, update: Update, context: CallbackContext):
|
| 45 |
+
if not self.auth_token:
|
| 46 |
+
self.authenticate()
|
| 47 |
+
if not self.auth_token:
|
| 48 |
+
await update.message.reply_text("Authentication failed. Please try again later.")
|
| 49 |
+
return
|
| 50 |
+
|
| 51 |
+
headers = {"Authorization": f"Bearer {self.auth_token}", "Content-Type": "application/x-www-form-urlencoded"}
|
| 52 |
+
payload = {"question": update.message.text}
|
| 53 |
+
try:
|
| 54 |
+
response = requests.post(self.ai_url, headers=headers, data=payload)
|
| 55 |
+
bot_reply = response.json().get("answer", "I didn't understand that.") if response.status_code == 200 else f"Error: {response.status_code}"
|
| 56 |
+
except Exception as e:
|
| 57 |
+
bot_reply = f"Connection error: {e}"
|
| 58 |
+
|
| 59 |
+
await update.message.reply_text(bot_reply)
|
| 60 |
+
|
| 61 |
+
def setup_handlers(self):
|
| 62 |
+
self.app.add_handler(CommandHandler("start", self.start_command))
|
| 63 |
+
self.app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self.chat_with_ai))
|
| 64 |
+
|
| 65 |
+
def run(self):
|
| 66 |
+
logging.info("Bot is now listening for messages...")
|
| 67 |
+
self.app.run_polling()
|
| 68 |
+
|
| 69 |
+
def start_telegram_bot():
|
| 70 |
+
bot = TelegramBot(BOT_TOKEN, BASE_URL, API_USERNAME, API_PASSWORD)
|
| 71 |
+
bot.run()
|
| 72 |
+
|
| 73 |
+
telegram_thread = Thread(target=start_telegram_bot, daemon=True)
|
| 74 |
+
telegram_thread.start()
|
| 75 |
+
|
| 76 |
+
def status():
|
| 77 |
+
return "✅ Telegram Bot is Running on Hugging Face!"
|
| 78 |
+
|
| 79 |
+
iface = gr.Interface(fn=status, inputs=[], outputs="text", title="Telegram Bot Status")
|
| 80 |
+
iface.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python-telegram-bot
|
| 2 |
+
requests
|
| 3 |
+
gradio
|