103 lines
2.7 KiB
Python
103 lines
2.7 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from dataclasses import dataclass, field
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass
|
||
|
|
class LazadaMessageTemplate:
|
||
|
|
template_id: int
|
||
|
|
payload: dict = field(default_factory=dict)
|
||
|
|
|
||
|
|
def to_dict(self) -> dict:
|
||
|
|
return {"template_id": self.template_id, **self.payload}
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def text(cls, txt: str, translate_txt: str | None = None) -> LazadaMessageTemplate:
|
||
|
|
payload = {"txt": txt}
|
||
|
|
if translate_txt:
|
||
|
|
payload["translateTxt"] = translate_txt
|
||
|
|
return cls(template_id=1, payload=payload)
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def system(cls, txt: str) -> LazadaMessageTemplate:
|
||
|
|
return cls(template_id=2, payload={"txt": txt})
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def image(cls, img_url: str, width: int = 800, height: int = 600) -> LazadaMessageTemplate:
|
||
|
|
return cls(
|
||
|
|
template_id=3,
|
||
|
|
payload={
|
||
|
|
"img_url": img_url,
|
||
|
|
"width": width,
|
||
|
|
"height": height,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def emoji(cls, txt: str) -> LazadaMessageTemplate:
|
||
|
|
return cls(template_id=4, payload={"txt": txt})
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def video(cls, video_id: str, width: int = 800, height: int = 600) -> LazadaMessageTemplate:
|
||
|
|
return cls(
|
||
|
|
template_id=6,
|
||
|
|
payload={
|
||
|
|
"video_id": video_id,
|
||
|
|
"width": width,
|
||
|
|
"height": height,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def product_card(cls, item_id: str) -> LazadaMessageTemplate:
|
||
|
|
return cls(template_id=10006, payload={"item_id": item_id})
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def order_card(cls, order_id: str) -> LazadaMessageTemplate:
|
||
|
|
return cls(template_id=10007, payload={"order_id": order_id})
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def coupon(cls, promotion_id: str) -> LazadaMessageTemplate:
|
||
|
|
return cls(template_id=10008, payload={"promotion_id": promotion_id})
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def invite_follow(cls) -> LazadaMessageTemplate:
|
||
|
|
return cls(template_id=10010, payload={})
|
||
|
|
|
||
|
|
|
||
|
|
TEMPLATE_DESCRIPTIONS = {
|
||
|
|
1: "普通文本",
|
||
|
|
2: "系统消息",
|
||
|
|
3: "图片",
|
||
|
|
4: "表情 (Emoji)",
|
||
|
|
6: "视频",
|
||
|
|
10006: "商品卡片",
|
||
|
|
10007: "订单卡片",
|
||
|
|
10008: "优惠券",
|
||
|
|
10010: "邀请关注店铺",
|
||
|
|
}
|
||
|
|
|
||
|
|
INCOMING_TEMPLATE_LABELS = {
|
||
|
|
1: "text",
|
||
|
|
2: "system",
|
||
|
|
3: "image",
|
||
|
|
4: "emoji",
|
||
|
|
6: "video",
|
||
|
|
10006: "product_card",
|
||
|
|
10007: "order_card",
|
||
|
|
10008: "coupon",
|
||
|
|
10010: "invite_follow",
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def truncate_text(content: str, max_chars: int = 2000) -> str:
|
||
|
|
if len(content) <= max_chars:
|
||
|
|
return content
|
||
|
|
return content[: max_chars - 3] + "..."
|
||
|
|
|
||
|
|
|
||
|
|
def strip_html(content: str) -> str:
|
||
|
|
import re
|
||
|
|
|
||
|
|
return re.sub(r"<[^>]+>", "", content or "")
|