dragxd commited on
Commit
e6d0867
·
verified ·
1 Parent(s): f0a8931

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +37 -0
bot.py CHANGED
@@ -2,6 +2,8 @@ import requests
2
  from telegram import Update, ReplyKeyboardMarkup, ReplyKeyboardRemove, InputFile
3
  from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes, ConversationHandler
4
  import os
 
 
5
 
6
  # Replace with your actual Hugging Face Space backend URL
7
  BACKEND_URL = "https://dragxd-host.hf.space"
@@ -63,6 +65,40 @@ async def upload_env(update: Update, context: ContextTypes.DEFAULT_TYPE):
63
  await update.message.reply_text("Please upload your .env file as a document.")
64
  return ConversationHandler.END
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  def main():
67
  TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN") # Set your bot token as env variable
68
  app = ApplicationBuilder().token(TOKEN).build()
@@ -76,6 +112,7 @@ def main():
76
  )
77
  app.add_handler(CommandHandler("start", start))
78
  app.add_handler(CommandHandler("connect", connect))
 
79
  app.add_handler(conv_handler)
80
  app.run_polling()
81
 
 
2
  from telegram import Update, ReplyKeyboardMarkup, ReplyKeyboardRemove, InputFile
3
  from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes, ConversationHandler
4
  import os
5
+ import time
6
+ import threading
7
 
8
  # Replace with your actual Hugging Face Space backend URL
9
  BACKEND_URL = "https://dragxd-host.hf.space"
 
65
  await update.message.reply_text("Please upload your .env file as a document.")
66
  return ConversationHandler.END
67
 
68
+ async def deploy(update: Update, context: ContextTypes.DEFAULT_TYPE):
69
+ user_id = str(update.effective_user.id)
70
+ # Trigger deployment
71
+ resp = requests.post(f"{BACKEND_URL}/deploy", json={"user_id": user_id, "github_url": ""})
72
+ if resp.status_code != 200 or "deploy_id" not in resp.json():
73
+ await update.message.reply_text("Failed to start deployment. Make sure you have selected a repo and uploaded your .env file.")
74
+ return
75
+ deploy_id = resp.json()["deploy_id"]
76
+ await update.message.reply_text(f"Deployment started! ID: {deploy_id}\nStreaming logs:")
77
+
78
+ def poll_logs():
79
+ last_line = 0
80
+ finished = False
81
+ while not finished:
82
+ try:
83
+ logs_resp = requests.get(f"{BACKEND_URL}/logs/{deploy_id}", params={"last_line": last_line})
84
+ if logs_resp.status_code == 200:
85
+ data = logs_resp.json()
86
+ logs = data.get("logs", [])
87
+ status = data.get("status", "unknown")
88
+ if logs:
89
+ for log in logs:
90
+ context.application.create_task(update.message.reply_text(log))
91
+ last_line = data.get("next_line", last_line)
92
+ if status != "deploying":
93
+ finished = True
94
+ context.application.create_task(update.message.reply_text(f"Deployment finished with status: {status}"))
95
+ break
96
+ time.sleep(2)
97
+ except Exception as e:
98
+ context.application.create_task(update.message.reply_text(f"Error streaming logs: {e}"))
99
+ break
100
+ threading.Thread(target=poll_logs, daemon=True).start()
101
+
102
  def main():
103
  TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN") # Set your bot token as env variable
104
  app = ApplicationBuilder().token(TOKEN).build()
 
112
  )
113
  app.add_handler(CommandHandler("start", start))
114
  app.add_handler(CommandHandler("connect", connect))
115
+ app.add_handler(CommandHandler("deploy", deploy))
116
  app.add_handler(conv_handler)
117
  app.run_polling()
118