#!/usr/bin/env python3 # -*- coding: utf-8 -*- """作业 2 · 步骤 1:把工单模拟服务部署到 207 并启动。""" from __future__ import annotations import sys from pathlib import Path import _bootstrap # noqa: F401 import _config as C from dify_api import Dify REMOTE_DIR = "/opt/ticket_api" LOCAL_API = Path(__file__).resolve().parent / "作业2_ticket_api.py" LOCAL_SH = Path(__file__).resolve().parents[2] / "scripts_bash_cursor" / "作业2_客服意图路由工作流" / "作业2_启动_ticket.sh" def main() -> int: d = Dify(C.DIFY_EMAIL, C.DIFY_PASSWORD) try: d.login() d.sh("mkdir -p %s" % REMOTE_DIR) d.sftp.put(str(LOCAL_API), REMOTE_DIR + "/作业2_ticket_api.py") d.sftp.put(str(LOCAL_SH), REMOTE_DIR + "/start.sh") d.sh("chmod +x %s/start.sh" % REMOTE_DIR) d.sh( "if [[ -f /tmp/ticket_api.pid ]]; then " "kill $(cat /tmp/ticket_api.pid) 2>/dev/null || true; " "rm -f /tmp/ticket_api.pid; fi" ) out, err = d.sh("TICKET_API_PORT=%d bash %s/start.sh" % (C.TICKET_API_PORT, REMOTE_DIR)) print(out or err) r = d.sh( "curl -s -X POST 'http://127.0.0.1:%d/ticket' " "-H 'Content-Type: application/json' " "-d '{\"query\":\"帮我报修设备\",\"category\":\"repair\"}'" % C.TICKET_API_PORT ) print("probe:", r[0].strip()) return 0 if "WO-" in (r[0] or "") else 1 finally: d.close() if __name__ == "__main__": sys.exit(main())