|
| 1 | +from datetime import datetime |
| 2 | +from django.conf import settings |
| 3 | + |
| 4 | +from core.models import Webhook |
| 5 | +from pydantic import BaseModel |
| 6 | + |
| 7 | + |
| 8 | +class ZammadConfig: |
| 9 | + url = settings.ZAMMAD_URL # servicedesk.europython.eu |
| 10 | + billing_group = settings.ZAMMAD_GROUP_BILLING |
| 11 | + helpdesk_group = settings.ZAMMAD_GROUP_HELPDESK |
| 12 | + |
| 13 | +class ZammadGroup(BaseModel): |
| 14 | + id: int |
| 15 | + name: str |
| 16 | + |
| 17 | + |
| 18 | +class ZammadUser(BaseModel): |
| 19 | + firstname: str |
| 20 | + lastname: str |
| 21 | + |
| 22 | + |
| 23 | +class ZammadTicket(BaseModel): |
| 24 | + id: int |
| 25 | + group: ZammadGroup |
| 26 | + title: str |
| 27 | + owner: ZammadUser |
| 28 | + state: str |
| 29 | + number: str |
| 30 | + customer: ZammadUser |
| 31 | + created_at: datetime |
| 32 | + updated_at: datetime |
| 33 | + updated_by: ZammadUser |
| 34 | + article_ids: list[int] |
| 35 | + |
| 36 | + |
| 37 | +class ZammadArticle(BaseModel): |
| 38 | + sender: str |
| 39 | + internal: bool |
| 40 | + ticket_id: int |
| 41 | + created_at: datetime |
| 42 | + created_by: ZammadUser |
| 43 | + subject: str |
| 44 | + |
| 45 | + |
| 46 | +class ZammadWebhook(BaseModel): |
| 47 | + ticket: ZammadTicket |
| 48 | + article: ZammadArticle | None |
| 49 | + |
| 50 | + |
| 51 | +JsonType = dict[str, str | int | float | list | dict] |
| 52 | + |
| 53 | + |
| 54 | +class ZammadParser: |
| 55 | + |
| 56 | + class Actions: |
| 57 | + new_ticket_created = "new_ticket_created" |
| 58 | + new_message_in_thread = "new_message_in_thread" |
| 59 | + replied_in_thread = "replied_in_thread" |
| 60 | + new_internal_note = "new_internal_note" |
| 61 | + updated_ticket = "updated_ticket" |
| 62 | + |
| 63 | + def __init__(self, content: JsonType): |
| 64 | + self.content = content |
| 65 | + # Ticket is always there, article is optional |
| 66 | + # Example: change of status of the Ticket doesn't contain article |
| 67 | + self.ticket = ZammadTicket.model_validate(self.content["ticket"]) |
| 68 | + self.article = ( |
| 69 | + ZammadArticle.model_validate(self.content["article"]) |
| 70 | + if self.content["article"] |
| 71 | + else None |
| 72 | + ) |
| 73 | + |
| 74 | + @property |
| 75 | + def action(self): |
| 76 | + """ |
| 77 | + Zammad doesn't give us an action inside the webhook, so we can either |
| 78 | + set custom triggers and URLs for every action, or we can try to infer |
| 79 | + the action from the content of the webhook. For simplicity of the |
| 80 | + overall setup, we are implementing the latter here. |
| 81 | +
|
| 82 | + "New Ticket created"? -- has article, and len(article_ids) == 1 |
| 83 | + -- state change will not have article associated with it. |
| 84 | + "New message in the thread" -- article, sender==Customer |
| 85 | + "We sent a new reply in the thread" -- article, sender==Agent |
| 86 | + "New internal note in the thread" -- article, internal==true |
| 87 | + "Updated the ticket ...", -- updated_by.firstname |
| 88 | + """ |
| 89 | + # Implementing this as cascading if statements here is part of the |
| 90 | + # assumptions. |
| 91 | + # For example the "sender == Customer" is going to be True also for their |
| 92 | + # first message that originally creates the ticket. However first time |
| 93 | + # we get a message, we will return "New ticket" and second time "New |
| 94 | + # message in the thread". |
| 95 | + if self.article: |
| 96 | + if len(self.ticket.article_ids) == 1: |
| 97 | + # This means we have an article, and it's a first one, therefore a |
| 98 | + # ticket is new. |
| 99 | + return self.Actions.new_ticket_created |
| 100 | + |
| 101 | + elif self.article.internal is True: |
| 102 | + return self.Actions.new_internal_note |
| 103 | + |
| 104 | + elif self.article.sender == "Customer": |
| 105 | + return self.Actions.new_message_in_thread |
| 106 | + |
| 107 | + elif self.article.sender == "Agent": |
| 108 | + return self.Actions.replied_in_thread |
| 109 | + |
| 110 | + elif not self.article: |
| 111 | + return self.Actions.updated_ticket |
| 112 | + |
| 113 | + raise ValueError("Unsupported scenario") |
| 114 | + |
| 115 | + @property |
| 116 | + def updated_by(self): |
| 117 | + return self.ticket.updated_by.firstname |
| 118 | + |
| 119 | + @property |
| 120 | + def group(self): |
| 121 | + return self.ticket.group.name |
| 122 | + |
| 123 | + @property |
| 124 | + def url(self): |
| 125 | + return f"https://{ZammadConfig.url}/#ticket/zoom/{self.ticket.id}" |
| 126 | + |
| 127 | + def to_discord_message(self): |
| 128 | + message = "{group}: {sender} {action} {details}".format |
| 129 | + |
| 130 | + # Action |
| 131 | + actions = { |
| 132 | + self.Actions.new_ticket_created: "created new ticket", |
| 133 | + self.Actions.new_message_in_thread: "sent a new message", |
| 134 | + self.Actions.replied_in_thread: "replied to a ticket", |
| 135 | + self.Actions.new_internal_note: "created internal note", |
| 136 | + self.Actions.updated_ticket: "updated ticket", |
| 137 | + } |
| 138 | + |
| 139 | + action = actions[self.action] |
| 140 | + |
| 141 | + return message(group=self.group, sender=self.updated_by, action=action, details=self.url) |
| 142 | + |
| 143 | + def meta(self): |
| 144 | + return { |
| 145 | + "group": self.group, |
| 146 | + "sender": self.updated_by, |
| 147 | + "action": self.action, |
| 148 | + "message": self.to_discord_message(), |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | +def prep_zammad_webhook(wh: Webhook): |
| 153 | + """Parse and store some information for later""" |
| 154 | + zp = ZammadParser(wh.content) |
| 155 | + |
| 156 | + wh.event = zp.action |
| 157 | + wh.extra = zp.meta() |
| 158 | + |
| 159 | + wh.save() |
| 160 | + |
| 161 | + return wh |
0 commit comments