@@ -25,6 +25,8 @@ logging.basicConfig(
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
OPENCODE_API_URL = os.getenv("OPENCODE_API_URL", "http://127.0.0.1:4096")
|
OPENCODE_API_URL = os.getenv("OPENCODE_API_URL", "http://127.0.0.1:4096")
|
||||||
|
OPENCODE_SERVER_USERNAME = os.getenv("OPENCODE_SERVER_USERNAME", "opencode")
|
||||||
|
OPENCODE_SERVER_PASSWORD = os.getenv("OPENCODE_SERVER_PASSWORD")
|
||||||
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
||||||
ALLOWED_CHAT_ID = os.getenv("TELEGRAM_ALLOWED_CHAT_ID")
|
ALLOWED_CHAT_ID = os.getenv("TELEGRAM_ALLOWED_CHAT_ID")
|
||||||
|
|
||||||
@@ -47,7 +49,13 @@ def get_session():
|
|||||||
if SESSION_ID:
|
if SESSION_ID:
|
||||||
return SESSION_ID
|
return SESSION_ID
|
||||||
try:
|
try:
|
||||||
r = requests.get(f"{OPENCODE_API_URL}/session", timeout=10)
|
r = requests.get(
|
||||||
|
f"{OPENCODE_API_URL}/session",
|
||||||
|
timeout=10,
|
||||||
|
auth=(OPENCODE_SERVER_USERNAME, OPENCODE_SERVER_PASSWORD)
|
||||||
|
if OPENCODE_SERVER_PASSWORD
|
||||||
|
else None,
|
||||||
|
)
|
||||||
if r.ok:
|
if r.ok:
|
||||||
sessions = r.json()
|
sessions = r.json()
|
||||||
if sessions:
|
if sessions:
|
||||||
@@ -56,7 +64,14 @@ def get_session():
|
|||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Failed to fetch existing sessions")
|
logger.exception("Failed to fetch existing sessions")
|
||||||
try:
|
try:
|
||||||
r = requests.post(f"{OPENCODE_API_URL}/session", json={}, timeout=10)
|
r = requests.post(
|
||||||
|
f"{OPENCODE_API_URL}/session",
|
||||||
|
json={},
|
||||||
|
timeout=10,
|
||||||
|
auth=(OPENCODE_SERVER_USERNAME, OPENCODE_SERVER_PASSWORD)
|
||||||
|
if OPENCODE_SERVER_PASSWORD
|
||||||
|
else None,
|
||||||
|
)
|
||||||
if r.ok:
|
if r.ok:
|
||||||
SESSION_ID = r.json()["id"]
|
SESSION_ID = r.json()["id"]
|
||||||
return SESSION_ID
|
return SESSION_ID
|
||||||
@@ -76,6 +91,9 @@ def send_to_opencode(message):
|
|||||||
f"{OPENCODE_API_URL}/session/{session_id}/message",
|
f"{OPENCODE_API_URL}/session/{session_id}/message",
|
||||||
json={"parts": [{"type": "text", "text": f"[Telegram] {message}"}]},
|
json={"parts": [{"type": "text", "text": f"[Telegram] {message}"}]},
|
||||||
timeout=1200,
|
timeout=1200,
|
||||||
|
auth=(OPENCODE_SERVER_USERNAME, OPENCODE_SERVER_PASSWORD)
|
||||||
|
if OPENCODE_SERVER_PASSWORD
|
||||||
|
else None,
|
||||||
)
|
)
|
||||||
if r.ok:
|
if r.ok:
|
||||||
data = r.json()
|
data = r.json()
|
||||||
@@ -128,7 +146,13 @@ async def status_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|||||||
if not is_authorized(update):
|
if not is_authorized(update):
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
r = requests.get(f"{OPENCODE_API_URL}/global/health", timeout=5)
|
r = requests.get(
|
||||||
|
f"{OPENCODE_API_URL}/global/health",
|
||||||
|
timeout=5,
|
||||||
|
auth=(OPENCODE_SERVER_USERNAME, OPENCODE_SERVER_PASSWORD)
|
||||||
|
if OPENCODE_SERVER_PASSWORD
|
||||||
|
else None,
|
||||||
|
)
|
||||||
healthy = r.ok
|
healthy = r.ok
|
||||||
except Exception:
|
except Exception:
|
||||||
healthy = False
|
healthy = False
|
||||||
|
|||||||
Reference in New Issue
Block a user