feat: enable HTML rich messages in Telegram poster

- Strip separator lines (━, —, -, =, etc.) automatically
- Format first line as bold HTML title (<b>Title</b>)
- Keep body text escaped and formatted
- Append hashtags at the end (#category #source)
- Pass parse_mode='HTML' in Telegram API requests
This commit is contained in:
Your Name
2026-07-22 16:32:02 +05:00
parent 254c506815
commit 36f836f5b1
2 changed files with 42 additions and 7 deletions
+39 -4
View File
@@ -31,12 +31,47 @@ def publication_hashtags(category_tag: str, source_tag: str) -> str:
return " ".join(f"#{tag}" for tag in tags if tag)
def build_publication_text(text: str, category_tag: str, source_tag: str) -> str:
import html
def build_publication_text(
text: str,
category_tag: str,
source_tag: str,
format_title: bool = False,
parse_mode: str | None = None,
) -> str:
base = strip_trailing_hashtag_line(text)
lines = base.splitlines()
sep_pattern = re.compile(r"^\s*[━—─\-=\*\#_]{2,}\s*$")
filtered_lines = [line for line in lines if not sep_pattern.match(line)]
if not filtered_lines:
return publication_hashtags(category_tag, source_tag)
title_idx = None
for idx, line in enumerate(filtered_lines):
if line.strip():
title_idx = idx
break
formatted_lines = []
for idx, line in enumerate(filtered_lines):
if idx == title_idx and format_title:
if parse_mode == "html":
escaped_title = html.escape(line.strip())
formatted_lines.append(f"<b>{escaped_title}</b>")
elif parse_mode == "markdown":
formatted_lines.append(f"**{line.strip()}**")
else:
formatted_lines.append(line.strip())
else:
if parse_mode == "html":
formatted_lines.append(html.escape(line))
else:
formatted_lines.append(line)
body = "\n".join(formatted_lines).strip()
hashtags = publication_hashtags(category_tag, source_tag)
if not hashtags:
return base
return f"{base}\n\n{hashtags}" if base else hashtags
if hashtags:
return f"{body}\n\n{hashtags}" if body else hashtags
return body
def parse_categories(value: object) -> list[str]: