etm-terrain/handlers/materiel.py

56 lines
1.7 KiB
Python

import logging
from telegram import Update
from telegram.ext import ContextTypes
from models.session import set_state, get_data, clear_data, MENU
from handlers import get_menu_keyboard, MENU_TEXT
from handlers.chantier import nom_depuis_dossier
from services.nextcloud import create_deck_card
from services.telegram import notify_materiel as send_notification
from config import DECK_BOARD_ID, DECK_COL_MATERIEL
log = logging.getLogger(__name__)
async def handle_materiel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
msg = update.message
text = (msg.text or "").strip()
user_id = msg.from_user.id
chantier_folder = get_data(user_id, "chantier_folder")
if chantier_folder:
chantier = nom_depuis_dossier(chantier_folder)
liste = text
else:
if not text or "/" not in text:
await msg.reply_text(
"❌ Format requis : `NOM_Ville / - item1 - item2`",
parse_mode="Markdown",
)
return
chantier, _, liste = text.partition("/")
chantier = chantier.strip()
liste = liste.strip()
if not liste:
await msg.reply_text("❌ Liste vide.", parse_mode="Markdown")
return
ouvrier = msg.from_user.full_name
try:
await send_notification(context.bot, ouvrier, chantier, liste)
except Exception as exc:
log.error("notify_materiel : %s", exc)
if DECK_BOARD_ID and DECK_COL_MATERIEL:
await create_deck_card(
DECK_BOARD_ID,
DECK_COL_MATERIEL,
f"Matériel — {chantier}",
f"Ouvrier : {ouvrier}\n{liste}",
)
clear_data(user_id)
set_state(user_id, MENU)
await msg.reply_text(MENU_TEXT, reply_markup=get_menu_keyboard())