Update bot.py
Browse files
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
|
|
|
|
|
8 |
|
9 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
10 |
-
await update.message.reply_text("Send /
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
async def
|
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.
|
19 |
if resp.status_code == 200:
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
else:
|
23 |
-
await update.message.reply_text("Failed to
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
resp = requests.get(f"{BACKEND_URL}/status/{deploy_id}")
|
31 |
if resp.status_code == 200:
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
else:
|
35 |
-
await update.message.reply_text("
|
|
|
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("
|
42 |
-
app.add_handler(
|
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()
|