feat: встроить шаблоны в SD_MAIN_MENU_TEXT при нажатии 3
This commit is contained in:
@@ -112,7 +112,63 @@ async def main_menu(msg: Message):
|
||||
USER_MENU_SHOWN[user_id] = True
|
||||
set_state(user_id, "SD_MODE")
|
||||
log_menu_stats(user_id, "Service Desk", "Вход")
|
||||
# sd_module_handler покажет шаблоны или меню
|
||||
# Загружаем шаблоны и встраиваем их в текст
|
||||
try:
|
||||
import json, asyncio, httpx, urllib3
|
||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||
# Импортируем SD_URL и SD_TOKEN из config
|
||||
import importlib.util
|
||||
spec = importlib.util.spec_from_file_location("custom_config", "/opt/trueconf_bot/config/config.py")
|
||||
custom_config = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(custom_config)
|
||||
SD_URL = custom_config.SD_URL
|
||||
SD_TOKEN = custom_config.SD_TOKEN
|
||||
headers = {"authtoken": SD_TOKEN, "Accept": "application/vnd.manageengine.sdp.v3+json"}
|
||||
input_data = {"list_info": {"row_count": 100, "start_index": 1}}
|
||||
params = {"input_data": json.dumps(input_data)}
|
||||
url = f"{SD_URL}/api/v3/request_templates"
|
||||
async with httpx.AsyncClient(verify=False) as client:
|
||||
resp = await client.get(url, headers=headers, params=params, timeout=30.0)
|
||||
if resp.status_code == 200:
|
||||
data = resp.json()
|
||||
all_templates = data.get("request_templates", [])
|
||||
pre_filtered = [
|
||||
t for t in all_templates
|
||||
if t.get("status") == "ACTIVE"
|
||||
and not t.get("inactive", False)
|
||||
and str(t.get("id")) != "2"
|
||||
]
|
||||
async def check_template(t, cl):
|
||||
try:
|
||||
t_resp = await client.get(url + "/" + str(t["id"]), headers=headers, timeout=15.0)
|
||||
if t_resp.status_code == 200:
|
||||
detailed = t_resp.json().get("request_template", {})
|
||||
return t if detailed.get("show_to_requester") is True else None
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
tasks = [check_template(t, client) for t in pre_filtered]
|
||||
results = await asyncio.gather(*tasks)
|
||||
templates = [t for t in results if t is not None]
|
||||
# Пагинация по 10
|
||||
per_page = 10
|
||||
total_pages = (len(templates) + per_page - 1) // per_page if templates else 1
|
||||
start = 0
|
||||
end = min(per_page, len(templates))
|
||||
page_items = templates[start:end]
|
||||
template_lines = ""
|
||||
for idx, t in enumerate(page_items):
|
||||
num = idx + 1
|
||||
digit = f"{num}⃣"
|
||||
template_lines += f"{digit} - {t['name']}\n"
|
||||
page_info = f"\n📄 <b>Страница 1 из {total_pages}</b>"
|
||||
template_text = f"\n\n📋 <b>Выберите шаблон:</b>\n{template_lines}{page_info}"
|
||||
full_text = SD_MAIN_MENU_TEXT + template_text
|
||||
await msg.answer(full_text, parse_mode="html")
|
||||
else:
|
||||
await msg.answer(SD_MAIN_MENU_TEXT, parse_mode="html")
|
||||
except Exception as e:
|
||||
await msg.answer(SD_MAIN_MENU_TEXT, parse_mode="html")
|
||||
return
|
||||
elif action == "PHOTO":
|
||||
USER_MENU_SHOWN[user_id] = True
|
||||
|
||||
Reference in New Issue
Block a user