feat(telegram): implement asynchronous Telegram alert system and enhance user verification messaging

This commit is contained in:
2025-07-27 21:56:59 +05:00
parent 2bc594da9c
commit 9c082aedf2
4 changed files with 123 additions and 12 deletions
+16 -12
View File
@@ -1,3 +1,4 @@
# pyright: reportGeneralTypeIssues=false
from django.shortcuts import get_object_or_404
from api.models import User
from devices.models import Device
@@ -8,6 +9,8 @@ from django.utils import timezone
# from api.notifications import send_clean_telegram_markdown
from api.omada import Omada
from api.bot import send_telegram_alert, telegram_loop, escape_markdown_v2
import asyncio
from apibase.env import env, BASE_DIR
from procrastinate.contrib.django import app
from procrastinate import builtin_tasks
@@ -147,18 +150,9 @@ def verify_user_with_person_api_task(user_id: int):
if not user:
logger.error(f"User with ID {user_id} not found.")
return None
# Call the Person API to verify the user
# verification_failed_message = f"""
# _The following user verification failed_:
# *ID Card:* {user.id_card}
# *Name:* {user.first_name} {user.last_name}
# *House Name:* {user.address}
# *Date of Birth:* {user.dob}
# *Island:* {(user.atoll.name if user.atoll else "N/A")} {(user.island.name if user.island else "N/A")}
# *Mobile:* {user.mobile}
# Visit [SAR Link Portal](https://portal.sarlink.net) to manually verify this user.
# """
verification_failed_message = f"""*The following user verification failed*:\n\n*ID Card:* {user.id_card}\n*Name:* {user.first_name} {user.last_name}\n*House Name:* {user.address}\n*Date of Birth:* {user.dob}\n*Island:* {(user.atoll.name if user.atoll else "N/A")} {(user.island.name if user.island else "N/A")}\n*Mobile:* {user.mobile}\nVisit [SAR Link Portal](https://portal.sarlink.net/users/{user_id}/details) to manually verify this user.
"""
# logger.info(verification_failed_message)
PERSON_VERIFY_BASE_URL = env.str("PERSON_VERIFY_BASE_URL", default="") # type: ignore
@@ -228,7 +222,7 @@ def verify_user_with_person_api_task(user_id: int):
user.save()
send_sms(
user.mobile,
f"Dear {user.first_name} {user.last_name}, \n\nYour account has been successfully and verified. \n\nYou can now manage your devices and make payments through our portal at https://portal.sarlink.net. \n\n - SAR Link",
f"Dear {user.first_name} {user.last_name}, \n\nYour account has been successfully verified. \n\nYou can now manage your devices and make payments through our portal at https://portal.sarlink.net. \n\n - SAR Link",
)
return True
else:
@@ -240,6 +234,16 @@ def verify_user_with_person_api_task(user_id: int):
f"Dear {user.first_name} {user.last_name}, \n\nYour account registration is being processed. \n\nWe will notify you once verification is complete. \n\n - SAR Link",
)
# send_clean_telegram_markdown(message=verification_failed_message)
try:
asyncio.run_coroutine_threadsafe(
send_telegram_alert(
markdown_message=escape_markdown_v2(verification_failed_message)
),
telegram_loop,
).result()
except Exception as e:
logger.warning("[Registration] TELEGRAM ALERT ERROR", e)
return False
else:
# Handle the error case