File tree 3 files changed +22
-2
lines changed
3 files changed +22
-2
lines changed Original file line number Diff line number Diff line change 1
1
import ast
2
-
2
+ import re
3
3
import parse
4
4
from markdown_it import MarkdownIt
5
5
@@ -48,3 +48,21 @@ def format_number_with_suffix(num: int) -> str:
48
48
def unmarkdown (text : str ) -> str :
49
49
"""markdown to plaintext"""
50
50
return MarkdownIt (renderer_cls = RendererPlain ).render (text )
51
+
52
+
53
+ def wa_markdown (text : str ) -> str :
54
+ patterns = [
55
+ (
56
+ re .compile (r"^(#+)\s+(.*)$" , flags = re .MULTILINE ),
57
+ lambda m : f"*{ m .group (2 )} *" ,
58
+ ), # "# headings" -> "*headings*"
59
+ (
60
+ re .compile (r"\*\*(.+?)\*\*" ),
61
+ lambda m : f"*{ m .group (1 )} *" ,
62
+ ), # "**bold**"" -> "*bold*"
63
+ ]
64
+
65
+ for pattern , repl in patterns :
66
+ text = pattern .sub (repl , text )
67
+
68
+ return text
Original file line number Diff line number Diff line change 10
10
from daras_ai_v2 .bots import BotInterface , ReplyButton , ButtonPressed
11
11
from daras_ai_v2 .exceptions import raise_for_status
12
12
from daras_ai_v2 .text_splitter import text_splitter
13
+ from daras_ai .text_format import wa_markdown
13
14
14
15
WA_MSG_MAX_SIZE = 1024
15
16
@@ -137,6 +138,7 @@ def send_msg_to(
137
138
) -> str | None :
138
139
# see https://developers.facebook.com/docs/whatsapp/api/messages/media/
139
140
141
+ text = wa_markdown (text )
140
142
# split text into chunks if too long
141
143
if text and len (text ) > WA_MSG_MAX_SIZE :
142
144
splits = text_splitter (
Original file line number Diff line number Diff line change 32
32
re .compile (sentence_end + new_para ),
33
33
re .compile (new_para ),
34
34
re .compile (sentence_end + new_line ),
35
- re .compile (sentence_end + whitespace ),
35
+ # re.compile(sentence_end + whitespace),
36
36
re .compile (new_line ),
37
37
re .compile (whitespace ),
38
38
)
You can’t perform that action at this time.
0 commit comments