This commit is contained in:
2026-03-01 11:23:48 +01:00
parent eeeef2674a
commit 9b418a11c2
5 changed files with 414 additions and 40 deletions

View File

@@ -1,38 +1,44 @@
import json
DEFAULT_STATE = {"text": "", "image_url": ""}
def _read_state():
try:
with open("/app/state.json", "r") as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
return dict(DEFAULT_STATE)
def _write_state(state):
with open("/app/state.json", "w") as f:
json.dump(state, f)
def main(args):
route = args.get("route")
# Verify file exists, if not create it with default content
try:
with open("/app/state.json", "r") as f:
pass
except FileNotFoundError:
with open("/app/state.json", "w") as f:
json.dump({"text": ""}, f)
f.flush()
body = args.get("body")
body = json.loads(body) if body else {}
if route.startswith("push"):
if route == "push_text":
with open("/app/state.json", "r") as f:
current = json.load(f)
current = _read_state()
current["text"] = body.get("text", "")
with open("/app/state.json", "w") as f:
json.dump(current, f)
_write_state(current)
return {"status": "success"}
elif route == "push_image_url":
current = _read_state()
current["image_url"] = body.get("image_url", "")
_write_state(current)
return {"status": "success"}
else:
return {"status": "error", "message": "Unknown push route"}
elif route.startswith("pull"):
current = _read_state()
if route == "pull_text":
with open("/app/state.json", "r") as f:
current = json.load(f)
return {"status": "success", "text": current.get("text", "")}
elif route == "pull_image_url":
return {"status": "success", "image_url": current.get("image_url", "")}
elif route == "pull_full":
with open("/app/state.json", "r") as f:
current = json.load(f)
return {"status": "success", "state": current}
else:
return {"status": "error", "message": "Unknown pull route"}