This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import httpx
|
||||
@@ -11,14 +12,26 @@ from app.models.user import User
|
||||
MODERATOR_ROLES = {"admin", "verifier", "moderator"}
|
||||
|
||||
|
||||
async def notify_user(user: User, text: str) -> bool:
|
||||
async def notify_user(
|
||||
user: User,
|
||||
text: str,
|
||||
*,
|
||||
web_app_url: str | None = None,
|
||||
button_text: str = "Открыть",
|
||||
) -> bool:
|
||||
if not settings.bot_token or settings.app_env == "test":
|
||||
return False
|
||||
data: dict[str, str] = {"chat_id": str(user.telegram_id), "text": text}
|
||||
if web_app_url:
|
||||
data["reply_markup"] = json.dumps(
|
||||
{"inline_keyboard": [[{"text": button_text, "web_app": {"url": web_app_url}}]]},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=5) as client:
|
||||
response = await client.post(
|
||||
f"https://api.telegram.org/bot{settings.bot_token}/sendMessage",
|
||||
data={"chat_id": str(user.telegram_id), "text": text},
|
||||
data=data,
|
||||
)
|
||||
return response.status_code < 400
|
||||
except Exception:
|
||||
|
||||
Reference in New Issue
Block a user