94 lines
2.5 KiB
Python
94 lines
2.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from dataclasses import dataclass
|
||
|
|
from typing import Any, Literal
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass
|
||
|
|
class TemplateCardButton:
|
||
|
|
text: str
|
||
|
|
style: Literal["default", "primary", "danger"] = "default"
|
||
|
|
key: str = ""
|
||
|
|
url: str | None = None
|
||
|
|
miniprogram_appid: str | None = None
|
||
|
|
miniprogram_pagepath: str | None = None
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass
|
||
|
|
class TemplateCard:
|
||
|
|
card_type: Literal["text_notice", "news_notice", "button_interaction"] = "text_notice"
|
||
|
|
title: str = ""
|
||
|
|
description: str = ""
|
||
|
|
url: str | None = None
|
||
|
|
image_url: str | None = None
|
||
|
|
buttons: list[TemplateCardButton] | None = None
|
||
|
|
emphasis_title: str | None = None
|
||
|
|
sub_title: str | None = None
|
||
|
|
task_id: str | None = None
|
||
|
|
extra: dict[str, Any] | None = None
|
||
|
|
|
||
|
|
|
||
|
|
class WeChatTemplateCardBuilder:
|
||
|
|
@staticmethod
|
||
|
|
def text_notice(title: str, description: str = "", url: str | None = None) -> TemplateCard:
|
||
|
|
return TemplateCard(
|
||
|
|
card_type="text_notice",
|
||
|
|
title=title,
|
||
|
|
description=description,
|
||
|
|
url=url,
|
||
|
|
)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def news_notice(
|
||
|
|
title: str,
|
||
|
|
description: str = "",
|
||
|
|
url: str | None = None,
|
||
|
|
image_url: str | None = None,
|
||
|
|
) -> TemplateCard:
|
||
|
|
return TemplateCard(
|
||
|
|
card_type="news_notice",
|
||
|
|
title=title,
|
||
|
|
description=description,
|
||
|
|
url=url,
|
||
|
|
image_url=image_url,
|
||
|
|
)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def button_interaction(
|
||
|
|
title: str,
|
||
|
|
description: str = "",
|
||
|
|
buttons: list[TemplateCardButton] | None = None,
|
||
|
|
task_id: str | None = None,
|
||
|
|
) -> TemplateCard:
|
||
|
|
return TemplateCard(
|
||
|
|
card_type="button_interaction",
|
||
|
|
title=title,
|
||
|
|
description=description,
|
||
|
|
buttons=buttons or [],
|
||
|
|
task_id=task_id,
|
||
|
|
)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def button(
|
||
|
|
text: str,
|
||
|
|
key: str = "",
|
||
|
|
url: str | None = None,
|
||
|
|
style: Literal["default", "primary", "danger"] = "default",
|
||
|
|
) -> TemplateCardButton:
|
||
|
|
return TemplateCardButton(text=text, key=key, url=url, style=style)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def miniprogram_button(
|
||
|
|
text: str,
|
||
|
|
appid: str,
|
||
|
|
pagepath: str,
|
||
|
|
style: Literal["default", "primary", "danger"] = "default",
|
||
|
|
) -> TemplateCardButton:
|
||
|
|
return TemplateCardButton(
|
||
|
|
text=text,
|
||
|
|
key="miniprogram",
|
||
|
|
miniprogram_appid=appid,
|
||
|
|
miniprogram_pagepath=pagepath,
|
||
|
|
style=style,
|
||
|
|
)
|