fix: limit rocket notification size
This commit is contained in:
@@ -14,6 +14,7 @@ STATE_FILE = "tickets_state.json"
|
||||
DEFAULT_TIMEOUT = 30
|
||||
DEFAULT_TICKET_FIELDS = ["id", "name", "stage_id", "partner_id", "user_id", "write_date"]
|
||||
DEFAULT_MAX_NOTIFIED_TICKETS = 50
|
||||
DEFAULT_MAX_MESSAGE_CHARS = 3500
|
||||
DEFAULT_TICKET_URL_TEMPLATE = "{odoo_url}/web#id={id}&model={model}&view_type=form"
|
||||
DEFAULT_ODOO_ORDER = "id asc"
|
||||
|
||||
@@ -44,6 +45,7 @@ def audit():
|
||||
tickets_updated,
|
||||
previous_tickets_state,
|
||||
get_max_notified_tickets(conf_wapt),
|
||||
get_max_message_chars(conf_wapt),
|
||||
get_ticket_url_template(conf_wapt),
|
||||
conf_wapt.get("odoo", "url").rstrip("/"),
|
||||
conf_wapt.get("odoo", "model", fallback="helpdesk.ticket"),
|
||||
@@ -117,6 +119,10 @@ def get_max_notified_tickets(conf_wapt):
|
||||
return conf_wapt.getint("notification", "max_tickets", fallback=DEFAULT_MAX_NOTIFIED_TICKETS)
|
||||
|
||||
|
||||
def get_max_message_chars(conf_wapt):
|
||||
return conf_wapt.getint("notification", "max_message_chars", fallback=DEFAULT_MAX_MESSAGE_CHARS)
|
||||
|
||||
|
||||
def get_ticket_url_template(conf_wapt):
|
||||
return conf_wapt.get("notification", "ticket_url_template", fallback=DEFAULT_TICKET_URL_TEMPLATE)
|
||||
|
||||
@@ -297,20 +303,28 @@ def format_detailed_ticket_line(ticket, previous_tickets_state):
|
||||
return "- " + " | ".join(parts)
|
||||
|
||||
|
||||
def build_notification_message(tickets_updated, previous_tickets_state, max_tickets, ticket_url_template, odoo_url, model):
|
||||
def build_notification_message(tickets_updated, previous_tickets_state, max_tickets, max_message_chars, ticket_url_template, odoo_url, model):
|
||||
if not tickets_updated:
|
||||
return "Aucun ticket n'a ete mis a jour depuis la derniere verification."
|
||||
|
||||
displayed_tickets = tickets_updated[:max_tickets]
|
||||
hidden_count = len(tickets_updated) - len(displayed_tickets)
|
||||
lines = [
|
||||
"Les tickets suivants ont ete mis a jour depuis la derniere verification :",
|
||||
]
|
||||
lines.extend([
|
||||
format_ticket_line(ticket, ticket_url_template, odoo_url, model)
|
||||
for ticket in displayed_tickets
|
||||
])
|
||||
displayed_count = 0
|
||||
|
||||
for ticket in displayed_tickets:
|
||||
ticket_line = format_ticket_line(ticket, ticket_url_template, odoo_url, model)
|
||||
candidate_lines = lines + [ticket_line]
|
||||
candidate_message = "\n".join(candidate_lines)
|
||||
|
||||
if max_message_chars > 0 and len(candidate_message) > max_message_chars:
|
||||
break
|
||||
|
||||
lines.append(ticket_line)
|
||||
displayed_count += 1
|
||||
|
||||
hidden_count = len(tickets_updated) - displayed_count
|
||||
if hidden_count > 0:
|
||||
lines.extend([
|
||||
"",
|
||||
@@ -346,5 +360,7 @@ def send_to_rocket(message_text, attachments=None, conf_wapt=None):
|
||||
headers={"Content-Type": "application/json"},
|
||||
timeout=get_http_timeout(conf_wapt),
|
||||
)
|
||||
if response.status_code >= 400:
|
||||
print("Erreur Rocket.Chat : %s" % response.text)
|
||||
response.raise_for_status()
|
||||
print("Message envoye avec succes.")
|
||||
|
||||
Reference in New Issue
Block a user