mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-06-05 16:56:19 +00:00
Add SMS and Telegram notification functions to handle user verification messages
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m15s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m15s
This commit is contained in:
parent
a9e1973f4a
commit
cdd032ac54
@ -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)}
|
17
api/tasks.py
17
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
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user