-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMessage.py
50 lines (44 loc) · 2.12 KB
/
Message.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import uuid
from typing import *
class MessageSegment:
@staticmethod
def text(text: str, *, buttons: List[List[dict] | dict] = None):
if not text or text.isspace():
raise ValueError("text cannot be empty")
message = {"contentType": "text", "content": {"text": text}}
if buttons is not None:
message["content"]["buttons"] = buttons
return message
@staticmethod
def markdown(text: str, *, buttons: List[List[dict] | dict] = None):
if not text or text.isspace():
raise ValueError("text cannot be empty")
message = {"contentType": "markdown", "content": {"text": text, "buttons": buttons}}
if buttons is not None:
message["content"]["buttons"] = buttons
return message
@staticmethod
def image(*, fileName: str = None, fileUrl: str, buttons: List[List[dict] | dict] = None):
if not fileUrl or fileUrl.isspace():
raise ValueError("fileUrl cannot be empty")
message = {"contentType": "image", "content": {"imageUrl": fileUrl, "buttons": buttons}}
if buttons is not None:
message["content"]["buttons"] = buttons
return message
@staticmethod
def file(*, fileName: str = uuid.uuid4(), fileUrl: str, buttons: List[List[dict] | dict] = None):
if not fileUrl or fileUrl.isspace():
raise ValueError("fileUrl cannot be empty")
message = {"contentType": "file", "content": {"fileName": fileName, "fileUrl": fileUrl, "buttons": buttons}}
if buttons is not None:
message["content"]["buttons"] = buttons
return message
@staticmethod
def button(*, text: str, actionType: int, url: str = "", value: str = ""):
if actionType == 1:
if not url or url.isspace():
raise ValueError("url cannot be empty")
elif actionType == (2 or 3):
if not value or value.isspace():
raise ValueError("value cannot be empty")
return {"actionType": actionType, "content": {"text": text, "url": url, "value": value}}