Я использовал эту функцию с MarkdownV2
Код: Выделить всё
def escape_markdown_v2(text):
special_chars = r"_[]()~`>#+|{}.!"
for char in special_chars:
text = text.replace(char, f'\\{char}')
return text
Код: Выделить всё
def create_match_message(home_team, away_team, match_date, match_time, stadium, tv_channels):
# Format the text with MarkdownV2 syntax for bold text (wrap in `*`)
message = (
f"*{home_team} vs {away_team}*\n"
f"*Date:* {match_date}\n"
f"*Time:* {match_time}\n"
f"*Venue:* {stadium}\n"
f"*TV Channels:* {tv_channels}"
)
escaped_message = escape_markdown_v2(message)
return escaped_message
Код: Выделить всё
def send_telegram_message(group_id, topic_id, message):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
payload = {
"chat_id": group_id,
"text": message,
"reply_to_message_id": topic_id,
"parse_mode": "MarkdownV2"
}
response = requests.post(url, json=payload)
if response.status_code != 200:
print(f"Error sending message: {response.status_code}, Response: {response.text}")
else:
print("Message sent successfully!")
Код: Выделить всё
*Oxford Utd vs Hull*
*Date:* 2024-11-05
*Time:* 19:45:00
*Venue:* The Kassam Stadium
*TV Channels:* Sky Sports\+
Подробнее здесь: https://stackoverflow.com/questions/791 ... ith-python
Мобильная версия