317 lines
14 KiB
Python
317 lines
14 KiB
Python
import os
|
|
import re
|
|
import logging
|
|
from datetime import datetime, timezone
|
|
from typing import Tuple, Optional
|
|
import httpx
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
from app.config import settings
|
|
from app.models import PublishQueue, ContentDraft, RawPost
|
|
|
|
logger = logging.getLogger("agent_c_publisher")
|
|
|
|
def ensure_official_links(text: str) -> str:
|
|
if not text:
|
|
return text
|
|
fixed = text
|
|
|
|
# Remove competing channel handles or links
|
|
# Standardize official NetVibe links
|
|
official_channel_link = "https://t.me/NetVibe_VPN"
|
|
official_admin = "@NetVibe_admin"
|
|
|
|
# Replace variations of NetVibe channel with the exact official URL
|
|
fixed = re.sub(r'https?://t\.me/(?:joinchat/)?[a-zA-Z0-9_+]*NetVibe[a-zA-Z0-9_]*', official_channel_link, fixed, flags=re.IGNORECASE)
|
|
fixed = re.sub(r'(?<!t\.me/)@NetVibe_?VPN\b', official_channel_link, fixed, flags=re.IGNORECASE)
|
|
|
|
# Replace variations of admin handle with exact official admin
|
|
fixed = re.sub(r'@NetVibe_?admin[a-zA-Z0-9_]*', official_admin, fixed, flags=re.IGNORECASE)
|
|
|
|
# Ensure the standard footer format exists
|
|
if official_channel_link not in fixed:
|
|
fixed = fixed.strip() + f"\n\n🌐 کانال رسمی: {official_channel_link}"
|
|
if official_admin not in fixed:
|
|
fixed = fixed.strip() + f"\n✉️ پشتیبانی و ثبت سفارش: {official_admin}"
|
|
|
|
return fixed
|
|
|
|
|
|
def markdown_to_telegram_html(text: str) -> str:
|
|
"""
|
|
Safely converts Markdown to Telegram HTML format without stripping underscores
|
|
from URLs, channel handles, or code snippets.
|
|
"""
|
|
if not text:
|
|
return ""
|
|
code_blocks = []
|
|
def save_cb(m):
|
|
code_blocks.append(m.group(2) if m.group(2) is not None else "")
|
|
return f"__CB_{len(code_blocks)-1}__"
|
|
text = re.sub(r"```([a-zA-Z0-9_]+)?\n?(.*?)```", save_cb, text, flags=re.DOTALL)
|
|
|
|
inline_codes = []
|
|
def save_ic(m):
|
|
inline_codes.append(m.group(1))
|
|
return f"__IC_{len(inline_codes)-1}__"
|
|
text = re.sub(r"`([^`]+)`", save_ic, text)
|
|
|
|
# Escape HTML special chars
|
|
text = text.replace("&", "&").replace("<", "<").replace(">", ">")
|
|
|
|
# Bold: **bold** -> <b>bold</b>
|
|
text = re.sub(r"\*\*(.+?)\*\*", r"<b>\1</b>", text)
|
|
|
|
# Restore inline code
|
|
for i, ic in enumerate(inline_codes):
|
|
c = ic.replace("&", "&").replace("<", "<").replace(">", ">")
|
|
text = text.replace(f"__IC_{i}__", f"<code>{c}</code>")
|
|
|
|
# Restore code blocks
|
|
for i, cb in enumerate(code_blocks):
|
|
c = cb.replace("&", "&").replace("<", "<").replace(">", ">")
|
|
text = text.replace(f"__CB_{i}__", f"<pre><code>{c}</code></pre>")
|
|
|
|
return text
|
|
|
|
|
|
def resolve_local_banner_path(banner_image_url: Optional[str]) -> Optional[str]:
|
|
"""Finds the physical path for the banner file, supporting relative URLs, casing mismatches, and symlinks."""
|
|
if not banner_image_url:
|
|
return None
|
|
|
|
clean_path = banner_image_url.replace("http://127.0.0.1:8000", "").replace("https://netvibe.ehsan.test.zebellkhan.com", "")
|
|
if clean_path.startswith("/static/"):
|
|
clean_path = clean_path.replace("/static/", "")
|
|
|
|
backend_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # /backend/app
|
|
static_root = os.path.join(backend_dir, "static")
|
|
search_dirs = [
|
|
static_root,
|
|
os.path.join(static_root, "banners"),
|
|
os.path.join(static_root, "banner_pool"),
|
|
os.path.join(static_root, "banner_pool", "gemini"),
|
|
os.path.join(static_root, "banner_pool", "vpn")
|
|
]
|
|
|
|
# Direct candidate
|
|
for sdir in search_dirs:
|
|
cand = os.path.join(sdir, clean_path)
|
|
if os.path.exists(cand) and os.path.isfile(cand):
|
|
return cand
|
|
|
|
# Case-insensitive filename search
|
|
target_filename = os.path.basename(clean_path).lower()
|
|
for sdir in search_dirs:
|
|
if not os.path.isdir(sdir):
|
|
continue
|
|
for root, _, files in os.walk(sdir):
|
|
for f in files:
|
|
if f.lower() == target_filename:
|
|
return os.path.join(root, f)
|
|
|
|
# Fallback to default pool banners if image was specifically expected
|
|
default_gemini = os.path.join(static_root, "banner_pool", "gemini", "Gemini_Pro_999k_Discount_Holo_Pedestal_6Gmail.png")
|
|
default_vpn = os.path.join(static_root, "banner_pool", "vpn", "vpn_master_banner_1.png")
|
|
if "gemini" in banner_image_url.lower() and os.path.exists(default_gemini):
|
|
return default_gemini
|
|
if os.path.exists(default_vpn):
|
|
return default_vpn
|
|
|
|
return None
|
|
|
|
async def send_to_telegram(
|
|
text: str,
|
|
target_channel: str,
|
|
bot_token: str,
|
|
media_type: str = "none",
|
|
media_url: Optional[str] = None,
|
|
banner_image_url: Optional[str] = None
|
|
) -> Tuple[bool, Optional[int], Optional[str]]:
|
|
if not bot_token:
|
|
logger.info(f"[SIMULATED DISPATCH] Sent to {target_channel} without real bot token.")
|
|
return True, 999999, None
|
|
|
|
base_url = f"https://api.telegram.org/bot{bot_token}"
|
|
async with httpx.AsyncClient(timeout=45.0, follow_redirects=True) as client:
|
|
try:
|
|
local_banner_path = resolve_local_banner_path(banner_image_url)
|
|
|
|
if local_banner_path:
|
|
with open(local_banner_path, "rb") as f:
|
|
file_bytes = f.read()
|
|
|
|
# Telegram photo caption max limit is 1024 characters
|
|
caption_text = text
|
|
followup_text = None
|
|
if len(text) > 1024:
|
|
# Split gracefully: first 950 chars as caption, remainder as followup message
|
|
split_idx = text[:950].rfind("\n")
|
|
if split_idx == -1:
|
|
split_idx = 950
|
|
caption_text = text[:split_idx].strip()
|
|
followup_text = text[split_idx:].strip()
|
|
|
|
formatted_caption = markdown_to_telegram_html(caption_text)
|
|
files = {"photo": (os.path.basename(local_banner_path), file_bytes, "image/png")}
|
|
data = {
|
|
"chat_id": target_channel,
|
|
"caption": formatted_caption,
|
|
"parse_mode": "HTML"
|
|
}
|
|
res = await client.post(f"{base_url}/sendPhoto", data=data, files=files)
|
|
# If Telegram fails due to HTML/Markdown entity parsing, retry without parse_mode
|
|
if not res.json().get("ok") and ("can't parse entities" in res.json().get("description", "").lower() or "bad request" in res.json().get("description", "").lower()):
|
|
logger.warning("Entity parse failed, retrying photo dispatch with plain text without parse_mode...")
|
|
files = {"photo": (os.path.basename(local_banner_path), file_bytes, "image/png")}
|
|
data["caption"] = caption_text
|
|
data.pop("parse_mode", None)
|
|
res = await client.post(f"{base_url}/sendPhoto", data=data, files=files)
|
|
|
|
# If long text, send the remaining portion as followup reply
|
|
if res.json().get("ok") and followup_text:
|
|
formatted_followup = markdown_to_telegram_html(followup_text)
|
|
followup_data = {
|
|
"chat_id": target_channel,
|
|
"text": formatted_followup,
|
|
"parse_mode": "HTML",
|
|
"reply_to_message_id": res.json()["result"]["message_id"]
|
|
}
|
|
res_followup = await client.post(f"{base_url}/sendMessage", json=followup_data)
|
|
if not res_followup.json().get("ok") and "can't parse entities" in res_followup.json().get("description", "").lower():
|
|
followup_data["text"] = followup_text
|
|
followup_data.pop("parse_mode", None)
|
|
await client.post(f"{base_url}/sendMessage", json=followup_data)
|
|
elif media_type == "photo" and media_url:
|
|
formatted_caption = markdown_to_telegram_html(text)
|
|
payload = {
|
|
"chat_id": target_channel,
|
|
"photo": media_url,
|
|
"caption": formatted_caption,
|
|
"parse_mode": "HTML"
|
|
}
|
|
res = await client.post(f"{base_url}/sendPhoto", json=payload)
|
|
if not res.json().get("ok") and ("can't parse entities" in res.json().get("description", "").lower() or "bad request" in res.json().get("description", "").lower()):
|
|
payload["caption"] = text
|
|
payload.pop("parse_mode", None)
|
|
res = await client.post(f"{base_url}/sendPhoto", json=payload)
|
|
elif media_type == "video" and media_url:
|
|
formatted_caption = markdown_to_telegram_html(text)
|
|
payload = {
|
|
"chat_id": target_channel,
|
|
"video": media_url,
|
|
"caption": formatted_caption,
|
|
"parse_mode": "HTML"
|
|
}
|
|
res = await client.post(f"{base_url}/sendVideo", json=payload)
|
|
if not res.json().get("ok") and ("can't parse entities" in res.json().get("description", "").lower() or "bad request" in res.json().get("description", "").lower()):
|
|
payload["caption"] = text
|
|
payload.pop("parse_mode", None)
|
|
res = await client.post(f"{base_url}/sendVideo", json=payload)
|
|
else:
|
|
formatted_text = markdown_to_telegram_html(text)
|
|
payload = {
|
|
"chat_id": target_channel,
|
|
"text": formatted_text,
|
|
"parse_mode": "HTML",
|
|
"disable_web_page_preview": False
|
|
}
|
|
res = await client.post(f"{base_url}/sendMessage", json=payload)
|
|
if not res.json().get("ok") and ("can't parse entities" in res.json().get("description", "").lower() or "bad request" in res.json().get("description", "").lower()):
|
|
payload["text"] = text
|
|
payload.pop("parse_mode", None)
|
|
res = await client.post(f"{base_url}/sendMessage", json=payload)
|
|
|
|
data = res.json()
|
|
if data.get("ok"):
|
|
msg_id = data["result"]["message_id"]
|
|
logger.info(f"Successfully published to Telegram {target_channel}, msg_id: {msg_id}")
|
|
return True, msg_id, None
|
|
else:
|
|
err_desc = data.get("description", "Unknown Telegram Error")
|
|
logger.error(f"Telegram dispatch failed: {err_desc}")
|
|
return False, None, err_desc
|
|
except Exception as e:
|
|
logger.error(f"Telegram network request exception: {e}")
|
|
return False, None, str(e)
|
|
|
|
async def dispatch_pending_queue(session: AsyncSession, batch_size: int = 5, force: bool = False) -> int:
|
|
now = datetime.now(timezone.utc)
|
|
conditions = [PublishQueue.status == "queued"]
|
|
if not force:
|
|
conditions.append(PublishQueue.scheduled_time <= now)
|
|
|
|
stmt = (
|
|
select(PublishQueue, ContentDraft, RawPost)
|
|
.join(ContentDraft, PublishQueue.draft_id == ContentDraft.id)
|
|
.outerjoin(RawPost, ContentDraft.raw_post_id == RawPost.id)
|
|
.where(*conditions)
|
|
.order_by(PublishQueue.scheduled_time.asc())
|
|
.limit(batch_size if not force else 1)
|
|
)
|
|
items = (await session.execute(stmt)).all()
|
|
|
|
success_count = 0
|
|
for queue_item, draft, raw_post in items:
|
|
queue_item.status = "publishing"
|
|
await session.commit()
|
|
|
|
success, msg_id, error = await send_to_telegram(
|
|
text=ensure_official_links(draft.current_text),
|
|
target_channel=draft.brand_target or settings.TARGET_CHANNEL,
|
|
bot_token=settings.TELEGRAM_BOT_TOKEN,
|
|
media_type=raw_post.media_type if raw_post else "none",
|
|
media_url=raw_post.media_url if raw_post else None,
|
|
banner_image_url=draft.banner_image_url
|
|
)
|
|
|
|
if success:
|
|
queue_item.status = "success"
|
|
queue_item.telegram_msg_id = msg_id
|
|
draft.status = "published"
|
|
success_count += 1
|
|
else:
|
|
queue_item.status = "failed"
|
|
queue_item.error_log = error
|
|
queue_item.retry_count += 1
|
|
|
|
await session.commit()
|
|
|
|
return success_count
|
|
|
|
|
|
async def dispatch_single_queue_item(session: AsyncSession, queue_id: str) -> tuple[bool, str | None]:
|
|
stmt = (
|
|
select(PublishQueue, ContentDraft, RawPost)
|
|
.join(ContentDraft, PublishQueue.draft_id == ContentDraft.id)
|
|
.outerjoin(RawPost, ContentDraft.raw_post_id == RawPost.id)
|
|
.where(PublishQueue.id == queue_id)
|
|
)
|
|
res = (await session.execute(stmt)).first()
|
|
if not res:
|
|
return False, "Queue item not found"
|
|
|
|
queue_item, draft, raw_post = res
|
|
queue_item.status = "publishing"
|
|
await session.commit()
|
|
|
|
success, msg_id, error = await send_to_telegram(
|
|
text=ensure_official_links(draft.current_text),
|
|
target_channel=draft.brand_target or settings.TARGET_CHANNEL,
|
|
bot_token=settings.TELEGRAM_BOT_TOKEN,
|
|
media_type=raw_post.media_type if raw_post else "none",
|
|
media_url=raw_post.media_url if raw_post else None,
|
|
banner_image_url=draft.banner_image_url
|
|
)
|
|
|
|
if success:
|
|
queue_item.status = "success"
|
|
queue_item.telegram_msg_id = msg_id
|
|
draft.status = "published"
|
|
else:
|
|
queue_item.status = "failed"
|
|
queue_item.error_log = error
|
|
queue_item.retry_count += 1
|
|
|
|
await session.commit()
|
|
return success, error
|