revert: return to original instruct SD API behavior

This commit is contained in:
Денис Кривоченко
2026-07-29 01:03:30 +07:00
parent 80b15c86c9
commit ddbcb11685
+10 -10
View File
@@ -94,11 +94,11 @@ def get_ad_user_info(user_id: str) -> dict | None:
raw_mail = str(entry.mail) if 'mail' in entry else "" raw_mail = str(entry.mail) if 'mail' in entry else ""
# mail может быть "[]" (пустой LDAP объект), "" или "N/A" — фильтруем # mail может быть "[]" (пустой LDAP объект), "" или "N/A" — фильтруем
mail = raw_mail if raw_mail and raw_mail not in ("[]", "N/A", "") else None mail = raw_mail if raw_mail and raw_mail not in ("[]", "N/A", "") else None
# Fallback: используем UPN если mail пустой, конвертируем @sibcem.ru → @tcs.sibcem.ru # Fallback: используем UPN если mail пустой
if not mail: if not mail:
raw_upn = str(entry.userPrincipalName) if 'userPrincipalName' in entry else "" raw_upn = str(entry.userPrincipalName) if 'userPrincipalName' in entry else ""
if raw_upn and raw_upn not in ("[]", "N/A", ""): if raw_upn and raw_upn not in ("[]", "N/A", ""):
mail = raw_upn.replace("@sibcem.ru", "@tcs.sibcem.ru") mail = raw_upn
city = str(entry.l) if 'l' in entry and entry.l.value else "Кемерово" city = str(entry.l) if 'l' in entry and entry.l.value else "Кемерово"
return {"cn": cn, "mail": mail, "city": city} return {"cn": cn, "mail": mail, "city": city}
except Exception as e: except Exception as e:
@@ -111,7 +111,7 @@ async def _send_sd_api_request(requester_email: str, subject: str, description:
Автономная прямая отправка HTTP-запроса в Service Desk API v3. Автономная прямая отправка HTTP-запроса в Service Desk API v3.
Автоматически ищет ID группы и техника по именам из config. Автоматически ищет ID группы и техника по именам из config.
""" """
# Если запрашивает системный ИИ или не указан email — ищем по ИМЕНИ # Если запрашивает системный ИИ — ищем по ИМЕНИ
if requester_email == DEFAULT_REQUESTER or requester_email == "ai@sibcem.ru": if requester_email == DEFAULT_REQUESTER or requester_email == "ai@sibcem.ru":
requester_payload = {"name": "Искусственный Интеллект"} requester_payload = {"name": "Искусственный Интеллект"}
else: else:
@@ -121,7 +121,7 @@ async def _send_sd_api_request(requester_email: str, subject: str, description:
group_id = None group_id = None
try: try:
url_groups = f"{SD_URL.rstrip('/')}/api/v3/groups" url_groups = f"{SD_URL.rstrip('/')}/api/v3/groups"
headers = {"authtoken": SD_TOKEN, "Accept": "application/vnd.manageengine.sdp.v3+json"} headers = {"TECHNICIAN_KEY": SD_TOKEN}
async with httpx.AsyncClient(verify=False) as client: async with httpx.AsyncClient(verify=False) as client:
resp = await client.get(url_groups, headers=headers, timeout=10.0) resp = await client.get(url_groups, headers=headers, timeout=10.0)
if resp.status_code == 200: if resp.status_code == 200:
@@ -138,7 +138,7 @@ async def _send_sd_api_request(requester_email: str, subject: str, description:
tech_id = None tech_id = None
try: try:
url_techs = f"{SD_URL.rstrip('/')}/api/v3/technicians" url_techs = f"{SD_URL.rstrip('/')}/api/v3/technicians"
headers = {"authtoken": SD_TOKEN, "Accept": "application/vnd.manageengine.sdp.v3+json"} headers = {"TECHNICIAN_KEY": SD_TOKEN}
async with httpx.AsyncClient(verify=False) as client: async with httpx.AsyncClient(verify=False) as client:
resp = await client.get(url_techs, headers=headers, timeout=10.0) resp = await client.get(url_techs, headers=headers, timeout=10.0)
if resp.status_code == 200: if resp.status_code == 200:
@@ -151,11 +151,10 @@ async def _send_sd_api_request(requester_email: str, subject: str, description:
except Exception as e: except Exception as e:
logger.warning(f"Failed to fetch SD technicians: {e}") logger.warning(f"Failed to fetch SD technicians: {e}")
html_desc = f"<p>{description.replace(chr(10), '<br>')}</p><br><hr><p style='color:#555;font-size:12px;'><i>Создано через TrueConf КЛЕВЕР</i></p>"
payload = { payload = {
"request": { "request": {
"subject": subject, "subject": subject,
"description": html_desc, "description": description,
"requester": requester_payload, "requester": requester_payload,
"udf_fields": { "udf_fields": {
"udf_pick_301": city "udf_pick_301": city
@@ -169,10 +168,11 @@ async def _send_sd_api_request(requester_email: str, subject: str, description:
payload["request"]["assigned_to"] = {"id": tech_id} payload["request"]["assigned_to"] = {"id": tech_id}
url = f"{SD_URL.rstrip('/')}/api/v3/requests" url = f"{SD_URL.rstrip('/')}/api/v3/requests"
headers = {"authtoken": SD_TOKEN, "Accept": "application/vnd.manageengine.sdp.v3+json"} headers = {"TECHNICIAN_KEY": SD_TOKEN}
data = {"input_data": json.dumps(payload)}
async with httpx.AsyncClient(verify=False) as client: async with httpx.AsyncClient(verify=False) as client:
response = await client.post(url, headers=headers, data={"input_data": json.dumps(payload)}, timeout=15.0) response = await client.post(url, headers=headers, data=data, timeout=15.0)
if response.status_code in (200, 201): if response.status_code in (200, 201):
res_json = response.json() res_json = response.json()
@@ -216,7 +216,7 @@ async def create_sd_ticket_for_trueconf(user_id: str, cn: str) -> str | None:
# Получаем email из AD # Получаем email из AD
ad_info = get_ad_user_info(user_id) ad_info = get_ad_user_info(user_id)
requester_email = ad_info.get("mail") or user_id or DEFAULT_REQUESTER requester_email = ad_info.get("mail") or DEFAULT_REQUESTER
city = ad_info.get("city", "Кемерово") if ad_info else "Кемерово" city = ad_info.get("city", "Кемерово") if ad_info else "Кемерово"
description = ( description = (