Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 4 additions & 37 deletions laya/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import re
from typing import Dict, List, Optional

# One definition, in the module that holds the other presets. Re-exported here because
# `from laya.email import email_questions` is a path callers already have.
from .presets import email_questions # noqa: F401

_QUOTE_HEADERS = [
re.compile(r"^\s*On .{0,300}wrote:\s*$", re.I),
# "Em resposta ao que você escreveu:" is body text; a client's attribution always carries a date
Expand Down Expand Up @@ -182,40 +186,3 @@ def email_state(subject: str, body: str, sender: Optional[str] = None, clean: bo
state["from"] = sender
state.update({k: v for k, v in extra.items() if v is not None})
return state


def email_questions(categories: Optional[Dict[str, str]] = None) -> Dict:
"""Standard pre-built questions for email triage."""
categories = categories or {
"billing": "invoices, payments, refunds",
"technical": "bugs, outages, integrations",
"sales": "pricing, demos, new purchases",
"security": "phishing, scams, account compromise",
"hr": "hiring, leave, payroll",
"other": "none of the above",
}
return {
"category": {
"type": "choice",
"instructions": "Which team should handle the email in `body`?",
"criteria": categories,
},
"is_spam": {
"type": "noul",
"instructions": "Is this email unsolicited spam or bulk marketing?",
},
"is_phishing": {
"type": "noul",
"instructions": "Is this email a phishing or scam attempt to steal money, credentials, or personal data?",
"criteria": {"true": "phishing, scam, or fraud", "false": "a legitimate email"},
},
"urgency": {
"type": "score",
"instructions": "How urgent is the request in `body`?",
"criteria": ["no time pressure", "needs attention soon", "blocking issue or hard deadline"],
},
"needs_reply": {
"type": "noul",
"instructions": "Does the sender expect a reply?",
},
}
30 changes: 30 additions & 0 deletions tests/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@

clean_email_body("Hi,\\n\\nThanks for the quick reply.\\nCould you refund invoice 4411?")
# before: 'Hi,' <- the request was cut away

The last block guards a different kind of silence. `email_questions` was defined twice, here and
in `laya/presets.py`, and `laya/__init__.py` re-exports the `presets` one. Editing the copy in
`laya/email.py` moved `laya.email.email_questions` and left `laya.email_questions` where it was,
with no test and no lint failing.
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import laya # noqa: E402
from laya import email as email_module # noqa: E402
from laya import presets # noqa: E402
from laya.email import clean_email_body, email_state # noqa: E402

PASS, FAIL = [], []
Expand Down Expand Up @@ -390,6 +398,28 @@ def check_true(name, cond, detail=""):
)


# ------------------------------------------- email_questions has exactly one definition
check_true(
"email_questions/one definition behind both module paths",
email_module.email_questions is presets.email_questions,
"laya.email.email_questions is not laya.presets.email_questions",
)
check_true(
"email_questions/the package export is that same object",
laya.email_questions is presets.email_questions,
)
check(
"email_questions/both paths answer the same",
email_module.email_questions(),
laya.email_questions(),
)
check(
"email_questions/a caller override reaches both paths",
email_module.email_questions({"legal": "contracts"})["category"]["criteria"],
laya.email_questions({"legal": "contracts"})["category"]["criteria"],
)


print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f in FAIL:
print(" FAIL " + f)
Expand Down
Loading