This commit is contained in:
+24
-7
@@ -22,7 +22,8 @@ from telegram_relay import (
|
||||
EDIT_LINKS_INITIAL_DELAY_SEC,
|
||||
EDIT_LINKS_MAX_ATTEMPTS,
|
||||
EDIT_LINKS_RETRY_INTERVAL_SEC,
|
||||
published_message_has_links,
|
||||
published_message_has_expected_links,
|
||||
extract_text_link_urls,
|
||||
)
|
||||
from season_links import subscription_start_link
|
||||
|
||||
@@ -154,10 +155,12 @@ async def download_image(image_url, httpx_client):
|
||||
raise
|
||||
|
||||
|
||||
async def _edit_post_add_links(bot, message_id, message_has_image, final_text, vk_post_id):
|
||||
async def _edit_post_add_links(
|
||||
bot, message_id, message_has_image, final_text, vk_post_id, expected_urls, min_length
|
||||
):
|
||||
"""
|
||||
Второй шаг публикации: editMessageCaption/Text со ссылками.
|
||||
Повторяет попытку, пока в ответе API нет text_link / меток ссылок.
|
||||
Повторяет попытку, пока в ответе API нет всех expected_urls в text_link entities.
|
||||
"""
|
||||
await asyncio.sleep(EDIT_LINKS_INITIAL_DELAY_SEC)
|
||||
|
||||
@@ -183,16 +186,21 @@ async def _edit_post_add_links(bot, message_id, message_has_image, final_text, v
|
||||
published_text = edited.text or ""
|
||||
entities = edited.entities or []
|
||||
|
||||
if published_message_has_links(published_text, entities):
|
||||
if published_message_has_expected_links(
|
||||
entities, expected_urls, result_text=published_text, min_length=min_length
|
||||
):
|
||||
logger.info(
|
||||
f"Ссылки добавлены для записи VK ID {vk_post_id} "
|
||||
f"(попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS})"
|
||||
)
|
||||
return True
|
||||
|
||||
found = extract_text_link_urls(entities)
|
||||
logger.warning(
|
||||
f"editMessage* ok, но ссылки не обнаружены в ответе "
|
||||
f"(попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS}) для VK ID {vk_post_id}"
|
||||
f"editMessage* ok, но footer-ссылки не подтверждены "
|
||||
f"(попытка {attempt}/{EDIT_LINKS_MAX_ATTEMPTS}) для VK ID {vk_post_id}; "
|
||||
f"ожидались {expected_urls}, найдены text_link: {found}, "
|
||||
f"len(text)={len(published_text)}, min_len={min_length}"
|
||||
)
|
||||
except TimedOut as e:
|
||||
if attempt >= EDIT_LINKS_MAX_ATTEMPTS:
|
||||
@@ -519,9 +527,18 @@ async def publish_to_tg(vk_post_id):
|
||||
|
||||
links_line = " | ".join(updated_links)
|
||||
final_text = f"{text}\n\n{links_line}"
|
||||
expected_urls = []
|
||||
if post['vk_post_url'] and (post['vk_post_url'].startswith('http://') or post['vk_post_url'].startswith('https://')):
|
||||
expected_urls.append(post['vk_post_url'])
|
||||
if post['is_event'] and USE_SUBSCRIPTION_BOT:
|
||||
expected_urls.append(
|
||||
subscription_start_link(RESPONDER_BOT_NAME, "post", message_id)
|
||||
)
|
||||
min_length = len(final_text) - 15
|
||||
|
||||
if not await _edit_post_add_links(
|
||||
bot, message_id, message_has_image, final_text, vk_post_id
|
||||
bot, message_id, message_has_image, final_text, vk_post_id,
|
||||
expected_urls, min_length,
|
||||
):
|
||||
logger.error(
|
||||
f"Запись VK ID {vk_post_id}: сообщение отправлено (id={message_id}), "
|
||||
|
||||
Reference in New Issue
Block a user