()
| 320 | |
| 321 | |
| 322 | def main() -> int: |
| 323 | parser = argparse.ArgumentParser(description="Start Feishu webhook server with ngrok") |
| 324 | parser.add_argument("--port", type=int, default=DEFAULT_PORT, help="Local webhook port") |
| 325 | parser.add_argument( |
| 326 | "--ngrok-timeout", |
| 327 | type=int, |
| 328 | default=20, |
| 329 | help="Seconds to wait for ngrok tunnel to become ready", |
| 330 | ) |
| 331 | parser.add_argument( |
| 332 | "--ngrok-only", |
| 333 | action="store_true", |
| 334 | help="Do not start webhook server, only ensure the ngrok tunnel exists", |
| 335 | ) |
| 336 | parser.add_argument( |
| 337 | "--ngrok-domain", |
| 338 | type=str, |
| 339 | default=os.environ.get("NGROK_DOMAIN", "").strip() or None, |
| 340 | help="Reserved ngrok static domain (e.g. paperflow-bot.ngrok-free.app). " |
| 341 | "Defaults to NGROK_DOMAIN env var.", |
| 342 | ) |
| 343 | args = parser.parse_args() |
| 344 | |
| 345 | bootstrap_runtime() |
| 346 | verify_required_env() |
| 347 | |
| 348 | if not args.ngrok_only: |
| 349 | if is_webhook_healthy(args.port): |
| 350 | print(f"[OK] Reusing existing healthy webhook server on port {args.port}") |
| 351 | else: |
| 352 | print(f"[INFO] Starting webhook server on port {args.port}...") |
| 353 | _, startup_state = start_webhook_thread(args.port) |
| 354 | if not wait_for_webhook(args.port): |
| 355 | if startup_state.get("error"): |
| 356 | print(f"[ERROR] Webhook server failed to start: {startup_state['error']}") |
| 357 | elif is_port_in_use(args.port): |
| 358 | print( |
| 359 | f"[ERROR] Port {args.port} is already occupied, " |
| 360 | "but /health did not respond as a PaperFlow webhook" |
| 361 | ) |
| 362 | else: |
| 363 | print("[ERROR] Webhook server did not become healthy in time") |
| 364 | return 1 |
| 365 | print("[OK] Webhook server is healthy") |
| 366 | |
| 367 | started_ngrok_process = None |
| 368 | ngrok_log_file = None |
| 369 | created_tunnel_name = None |
| 370 | |
| 371 | existing_tunnel = get_existing_tunnel(args.port) |
| 372 | if existing_tunnel: |
| 373 | public_url = str(existing_tunnel.get("public_url", "")).strip() |
| 374 | print(f"[OK] Reusing existing ngrok tunnel: {public_url}") |
| 375 | else: |
| 376 | if ngrok_api_online(): |
| 377 | created_tunnel_name = f"{DEFAULT_TUNNEL_NAME}-{args.port}" |
| 378 | print("[INFO] Local ngrok agent is already running, creating a tunnel via local API...") |
| 379 | created_tunnel = create_tunnel_via_api(args.port, created_tunnel_name, domain=args.ngrok_domain) |
no test coverage detected