dragxd commited on
Commit
576915b
·
verified ·
1 Parent(s): 89de4ed

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +63 -26
bot.py CHANGED
@@ -1,46 +1,83 @@
1
  import requests
2
- from telegram import Update
3
- from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
4
  import os
5
 
6
  # Replace with your actual Hugging Face Space backend URL
7
- BACKEND_URL = "https://dragxd-host.hf.space"
 
 
8
 
9
  async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
10
- await update.message.reply_text("Send /deploy <github_repo_url> to deploy your project!")
 
 
 
 
 
 
 
 
 
11
 
12
- async def deploy(update: Update, context: ContextTypes.DEFAULT_TYPE):
13
- if len(context.args) != 1:
14
- await update.message.reply_text("Usage: /deploy <github_repo_url>")
15
- return
16
- github_url = context.args[0]
17
  user_id = str(update.effective_user.id)
18
- resp = requests.post(f"{BACKEND_URL}/deploy", json={"github_url": github_url, "user_id": user_id})
19
  if resp.status_code == 200:
20
- deploy_id = resp.json()["deploy_id"]
21
- await update.message.reply_text(f"Deployment started! ID: {deploy_id}\nCheck status with /status {deploy_id}")
 
 
 
 
 
 
22
  else:
23
- await update.message.reply_text("Failed to start deployment.")
24
-
25
- async def status(update: Update, context: ContextTypes.DEFAULT_TYPE):
26
- if len(context.args) != 1:
27
- await update.message.reply_text("Usage: /status <deploy_id>")
28
- return
29
- deploy_id = context.args[0]
30
- resp = requests.get(f"{BACKEND_URL}/status/{deploy_id}")
31
  if resp.status_code == 200:
32
- data = resp.json()
33
- await update.message.reply_text(f"Status: {data['status']}\nURL: {data.get('url', 'N/A')}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  else:
35
- await update.message.reply_text("Deployment not found.")
 
36
 
37
  def main():
38
  TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN") # Set your bot token as env variable
39
  app = ApplicationBuilder().token(TOKEN).build()
 
 
 
 
 
 
 
 
40
  app.add_handler(CommandHandler("start", start))
41
- app.add_handler(CommandHandler("deploy", deploy))
42
- app.add_handler(CommandHandler("status", status))
43
  app.run_polling()
44
 
45
  if __name__ == "__main__":
46
- main()
 
1
  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://<your-hf-space-username>-<space-name>.hf.space"
8
+
9
+ SELECT_REPO, UPLOAD_ENV = range(2)
10
 
11
  async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
12
+ await update.message.reply_text("Send /connect to link your GitHub account.")
13
+
14
+ async def connect(update: Update, context: ContextTypes.DEFAULT_TYPE):
15
+ user_id = str(update.effective_user.id)
16
+ resp = requests.get(f"{BACKEND_URL}/github/login", params={"user_id": user_id})
17
+ if resp.status_code == 200:
18
+ auth_url = resp.json()["auth_url"]
19
+ await update.message.reply_text(f"Connect your GitHub: {auth_url}\nAfter connecting, send /repos to choose a repo.")
20
+ else:
21
+ await update.message.reply_text("Failed to get GitHub auth URL.")
22
 
23
+ async def repos(update: Update, context: ContextTypes.DEFAULT_TYPE):
 
 
 
 
24
  user_id = str(update.effective_user.id)
25
+ resp = requests.get(f"{BACKEND_URL}/github/repos", params={"user_id": user_id})
26
  if resp.status_code == 200:
27
+ repos = resp.json()
28
+ if not repos:
29
+ await update.message.reply_text("No repos found.")
30
+ return ConversationHandler.END
31
+ repo_names = [r["full_name"] for r in repos]
32
+ markup = ReplyKeyboardMarkup([[name] for name in repo_names], one_time_keyboard=True)
33
+ await update.message.reply_text("Select a repo:", reply_markup=markup)
34
+ return SELECT_REPO
35
  else:
36
+ await update.message.reply_text("Failed to fetch repos. Make sure you are connected with GitHub.")
37
+ return ConversationHandler.END
38
+
39
+ async def select_repo(update: Update, context: ContextTypes.DEFAULT_TYPE):
40
+ user_id = str(update.effective_user.id)
41
+ repo_full_name = update.message.text
42
+ resp = requests.post(f"{BACKEND_URL}/repo/select", json={"user_id": user_id, "repo_full_name": repo_full_name})
 
43
  if resp.status_code == 200:
44
+ await update.message.reply_text(f"Repo selected: {repo_full_name}\nNow upload your .env file.", reply_markup=ReplyKeyboardRemove())
45
+ return UPLOAD_ENV
46
+ else:
47
+ await update.message.reply_text("Failed to select repo.", reply_markup=ReplyKeyboardRemove())
48
+ return ConversationHandler.END
49
+
50
+ async def upload_env(update: Update, context: ContextTypes.DEFAULT_TYPE):
51
+ user_id = str(update.effective_user.id)
52
+ if update.message.document:
53
+ file = await update.message.document.get_file()
54
+ file_bytes = await file.download_as_bytearray()
55
+ files = {"file": (update.message.document.file_name, file_bytes)}
56
+ data = {"user_id": user_id}
57
+ resp = requests.post(f"{BACKEND_URL}/env/upload", data=data, files=files)
58
+ if resp.status_code == 200:
59
+ await update.message.reply_text(".env uploaded! Now you can /deploy your project.")
60
+ else:
61
+ await update.message.reply_text("Failed to upload .env.")
62
  else:
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()
69
+ conv_handler = ConversationHandler(
70
+ entry_points=[CommandHandler("repos", repos)],
71
+ states={
72
+ SELECT_REPO: [MessageHandler(filters.TEXT & ~filters.COMMAND, select_repo)],
73
+ UPLOAD_ENV: [MessageHandler(filters.Document.ALL, upload_env)]
74
+ },
75
+ fallbacks=[]
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
 
82
  if __name__ == "__main__":
83
+ main()