diff --git a/api/sms.py b/api/notifications.py similarity index 62% rename from api/sms.py rename to api/notifications.py index 4d330d5..8329426 100644 --- a/api/sms.py +++ b/api/notifications.py @@ -6,6 +6,8 @@ import logging logger = logging.getLogger(__name__) api_url = str(config("SMS_API_URL", cast=str, default="")) api_key = str(config("SMS_API_KEY", cast=str, default="")) +bot_token = str(config("TG_BOT_TOKEN", cast=str, default="")) +chat_id = str(config("TG_CHAT_ID", cast=str, default="")) def send_otp(mobile: str, message: str): @@ -55,3 +57,29 @@ def send_sms(mobile: str, message: str): except requests.exceptions.RequestException as e: logger.debug(f"Failed to send SMS. Error: {e}") return False + + +# def escape_markdown_v2(text: str) -> str: +# special_chars = r"\_*[]()~`>#+-=|{}.!" +# return "".join(["\\" + c if c in special_chars else c for c in text]) + + +def send_telegram_markdown(message: str): + """ + Sends a MarkdownV2-formatted message to a Telegram chat. + + Parameters: + bot_token (str): Your Telegram bot token + chat_id (str): Target chat ID (e.g., "-1001234567890" for channels/groups) + message (str): The message content, already escaped for MarkdownV2 + """ + try: + url = f"https://api.telegram.org/bot{bot_token}/sendMessage" + payload = {"chat_id": chat_id, "text": message, "parse_mode": "MarkdownV2"} + + response = requests.post(url, data=payload) + response.raise_for_status() + return response.json() + except requests.RequestException as e: + logger.error(f"Error sending Telegram message: {e}") + return {"error": str(e)} diff --git a/api/tasks.py b/api/tasks.py index 6e56993..3325735 100644 --- a/api/tasks.py +++ b/api/tasks.py @@ -1,13 +1,14 @@ from django.shortcuts import get_object_or_404 from api.models import User from devices.models import Device -from api.sms import send_sms +from api.notifications import send_sms import requests from apibase.env import env, BASE_DIR import os import logging from celery import shared_task from django.utils import timezone +from api.notifications import send_telegram_markdown logger = logging.getLogger(__name__) @@ -157,6 +158,19 @@ def verify_user_with_person_api_task(user_id: int): user = get_object_or_404(User, id=user_id) # Call the Person API to verify the user + + verification_failed_message = f""" + _The following user verification failed_: + *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} {user.island} \n + *Mobile:* {user.mobile} \n + + Visit [SAR Link Portal](https://portal.sarlink.net) to manually verify this user. + """ + if not PERSON_VERIFY_BASE_URL: raise ValueError( "PERSON_VERIFY_BASE_URL is not set in the environment variables." @@ -227,6 +241,7 @@ def verify_user_with_person_api_task(user_id: int): user.mobile, 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_telegram_markdown(message=verification_failed_message) return False else: # Handle the error case diff --git a/api/views.py b/api/views.py index 295a199..02f6606 100644 --- a/api/views.py +++ b/api/views.py @@ -31,7 +31,7 @@ import re from typing import cast, Dict, Any from django.core.mail import send_mail from django.db.models import Q -from api.sms import send_otp +from api.notifications import send_otp from .tasks import add, add_new_devices_to_omada from devices.models import Device