Upload files to "/"
This commit is contained in:
16
.gitignore
vendored
Normal file
16
.gitignore
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
# Virtual environments
|
||||
.venv/
|
||||
venv/
|
||||
|
||||
# Python cache
|
||||
__pycache__/
|
||||
*.pyc
|
||||
|
||||
# Environment files
|
||||
.env
|
||||
|
||||
# OS files
|
||||
.DS_Store
|
||||
|
||||
# The telegram bot
|
||||
bot.py
|
||||
79
app.py
Normal file
79
app.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from flask import Flask, render_template_string, request
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
HTML = """
|
||||
<!doctype html>
|
||||
<html lang="nl">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Hello World Spiegel</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
max-width: 700px;
|
||||
margin: 40px auto;
|
||||
padding: 0 20px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
h1 {
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
form {
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
input[type="text"] {
|
||||
width: 100%;\n max-width: 400px;
|
||||
padding: 10px;
|
||||
font-size: 16px;
|
||||
}
|
||||
button {
|
||||
margin-top: 12px;
|
||||
padding: 10px 16px;
|
||||
font-size: 16px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.result {
|
||||
margin-top: 20px;
|
||||
padding: 16px;
|
||||
background: #f4f4f4;
|
||||
border-radius: 8px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello World. Dit is debeste release.........</h1>
|
||||
|
||||
<form method="post">
|
||||
<label for="tekst">Typ iets:</label><br>
|
||||
<input type="text" id="tekst" name="tekst" value="{{ tekst }}" placeholder="Voer tekst in">
|
||||
<br>
|
||||
<button type="submit">Toon in spiegelbeeld ...</button>
|
||||
</form>
|
||||
|
||||
{% if gespiegeld is not none %}
|
||||
<div class="result">
|
||||
<strong>Spiegelbeeld:</strong>
|
||||
<p>{{ gespiegeld }}</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
|
||||
@app.route("/", methods=["GET", "POST"])
|
||||
def home():
|
||||
tekst = ""
|
||||
gespiegeld = None
|
||||
|
||||
if request.method == "POST":
|
||||
tekst = request.form.get("tekst", "")
|
||||
gespiegeld = tekst[::-1]
|
||||
|
||||
return render_template_string(HTML, tekst=tekst, gespiegeld=gespiegeld)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", debug=True)
|
||||
112
bot.py
Normal file
112
bot.py
Normal file
@@ -0,0 +1,112 @@
|
||||
bot.py docker-compose.yml venv
|
||||
lionel@ubuntuserver1:~/krotbot$ cat bot.py
|
||||
import os
|
||||
import time
|
||||
import subprocess
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
||||
ALLOWED_USER_ID = int(os.getenv("TELEGRAM_ALLOWED_USER_ID"))
|
||||
DEPLOY_DIR = os.getenv("DEPLOY_DIR", "/home/lionel/docker-compose/arcade")
|
||||
CI_COMPOSE_FILE = os.getenv("CI_COMPOSE_FILE", "docker-compose.ci.yml")
|
||||
PROD_COMPOSE_FILE = os.getenv("PROD_COMPOSE_FILE", "docker-compose.prod.yml")
|
||||
|
||||
if not BOT_TOKEN:
|
||||
raise RuntimeError("TELEGRAM_BOT_TOKEN ontbreekt")
|
||||
if not ALLOWED_USER_ID:
|
||||
raise RuntimeError("TELEGRAM_ALLOWED_USER_ID ontbreekt")
|
||||
|
||||
BASE_URL = f"https://api.telegram.org/bot{BOT_TOKEN}"
|
||||
offset = None
|
||||
|
||||
|
||||
def send_message(chat_id: int, text: str) -> None:
|
||||
requests.post(
|
||||
f"{BASE_URL}/sendMessage",
|
||||
data={"chat_id": chat_id, "text": text},
|
||||
timeout=20,
|
||||
)
|
||||
|
||||
|
||||
def run_cmd(cmd: str, timeout: int = 300) -> str:
|
||||
result = subprocess.run(
|
||||
["bash", "-lc", cmd],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=timeout,
|
||||
)
|
||||
output = ((result.stdout or "") + "\n" + (result.stderr or "")).strip()
|
||||
if result.returncode != 0:
|
||||
raise RuntimeError(output or f"Commando faalde: {cmd}")
|
||||
return output
|
||||
|
||||
|
||||
def handle_command(chat_id: int, user_id: int, text: str) -> None:
|
||||
text = text.strip().split()[0].split("@")[0]
|
||||
|
||||
if user_id != ALLOWED_USER_ID:
|
||||
send_message(chat_id, "Niet toegestaan.")
|
||||
return
|
||||
|
||||
if text == "/start":
|
||||
send_message(chat_id, "Bot actief. Beschikbare commando's: /status /deploy_stable")
|
||||
return
|
||||
|
||||
if text == "/status":
|
||||
try:
|
||||
output = run_cmd(
|
||||
f"cd {DEPLOY_DIR} && docker compose -f {PROD_COMPOSE_FILE} ps",
|
||||
timeout=60,
|
||||
)
|
||||
send_message(chat_id, f"Productie status:\n\n{output[:3500]}")
|
||||
except Exception as e:
|
||||
send_message(chat_id, f"Status ophalen mislukt:\n{str(e)[:3500]}")
|
||||
return
|
||||
|
||||
if text == "/deploy_stable":
|
||||
send_message(chat_id, "Stable deployment gestart...")
|
||||
|
||||
try:
|
||||
commands = f"""
|
||||
set -e
|
||||
cd {DEPLOY_DIR}
|
||||
docker compose -f {CI_COMPOSE_FILE} down || true
|
||||
docker compose -f {PROD_COMPOSE_FILE} pull
|
||||
docker compose -f {PROD_COMPOSE_FILE} up -d
|
||||
docker compose -f {PROD_COMPOSE_FILE} ps
|
||||
"""
|
||||
output = run_cmd(commands, timeout=300)
|
||||
send_message(chat_id, f"Stable uitgerold.\n\n{output[:3500]}")
|
||||
except Exception as e:
|
||||
send_message(chat_id, f"Deployment mislukt:\n{str(e)[:3500]}")
|
||||
return
|
||||
|
||||
send_message(chat_id, "Onbekend commando. Gebruik /deploy_stable of /status")
|
||||
|
||||
|
||||
while True:
|
||||
try:
|
||||
params = {"timeout": 30}
|
||||
if offset is not None:
|
||||
params["offset"] = offset
|
||||
|
||||
response = requests.get(f"{BASE_URL}/getUpdates", params=params, timeout=40)
|
||||
response.raise_for_status()
|
||||
updates = response.json().get("result", [])
|
||||
|
||||
for update in updates:
|
||||
offset = update["update_id"] + 1
|
||||
message = update.get("message", {})
|
||||
text = message.get("text", "").strip()
|
||||
chat_id = message.get("chat", {}).get("id")
|
||||
user_id = message.get("from", {}).get("id")
|
||||
|
||||
if text and chat_id and user_id:
|
||||
handle_command(chat_id, user_id, text)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Loop error: {e}")
|
||||
time.sleep(5)
|
||||
8
docker-compose.yml
Normal file
8
docker-compose.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
# This docker compose file is used for testing the built image on the GitHub Actions runner.
|
||||
# It's not meant to deploy the app in production.
|
||||
|
||||
services:
|
||||
arcade:
|
||||
image: 10.0.2.109:3000/gitea/arcade:latest
|
||||
ports:
|
||||
- "5000:5000"
|
||||
Reference in New Issue
Block a user