203 lines
7.2 KiB
Python
203 lines
7.2 KiB
Python
"""
|
|
|
|
AUTHOR:
|
|
|
|
Khushal P Soonderji
|
|
|
|
DATE:
|
|
|
|
Friday, 20th Jun., 2025
|
|
|
|
OBJECTIVE:
|
|
|
|
To send out SMS messages for internal use.
|
|
|
|
REFERENCES:
|
|
|
|
N/A
|
|
|
|
DOWNLOADS:
|
|
|
|
N/A
|
|
|
|
NOTES:
|
|
|
|
N/A
|
|
|
|
"""
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** IMPORT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# To make sibling directories accessible for imports:
|
|
import sys
|
|
sys.path.append(".")
|
|
sys.path.append("..")
|
|
|
|
# For using Quart:
|
|
from quart import Blueprint, current_app
|
|
|
|
# Common:
|
|
from shared import constants
|
|
|
|
# My utils:
|
|
from utils_v2.string import json
|
|
from utils_v2.date_time import date_time
|
|
from utils_v2.security.otp import HashedOTP
|
|
from utils_v2.sms.india.nimbus.controllers.async_nimbus import AsyncNimbusSMS
|
|
from utils_v2.api.response import ResponseModel
|
|
from utils_v2.api.codes import StatusCodes, HttpCodes
|
|
from utils_v2.api.async_quart import (
|
|
log_request_to_mongo,
|
|
only_whitelisted_ips,
|
|
should_not_be_under_maintenance,
|
|
read_input,
|
|
limit_rate,
|
|
validate_input,
|
|
set_api_version,
|
|
log_chain_to_mongo,
|
|
handle_cancelled_request
|
|
)
|
|
|
|
# Data models:
|
|
from models.sms.nimbus_sms import NimbusSMSSendRequest
|
|
|
|
# For asynchronous activities:
|
|
import asyncio
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MACROS / ONE-TIME INIT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# Related to Quart:
|
|
sms_bp = Blueprint("sms_bp", __name__)
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** VARIABLES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** FUNCTIONS ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
@sms_bp.record_once
|
|
def init(blueprint_setup_state):
|
|
|
|
# This gets called when the blueprint is registered.
|
|
# Consider this to be a one-time setup for the whole blueprint:
|
|
pass
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
@sms_bp.route("/nimbus", methods = ["POST"])
|
|
@sms_bp.route("/nimbus/send", methods = ["POST"])
|
|
@set_api_version(api_version = "1.0.0")
|
|
@read_input(sanitize_headers = True, sanitize_data = True)
|
|
@log_request_to_mongo(
|
|
attr_name = "logs_mongo",
|
|
project = constants.PROJECT_NAME,
|
|
log_type = "timedOTP",
|
|
operation = "generate",
|
|
log_input = True,
|
|
log_output = True,
|
|
sensitive_keys = None
|
|
)
|
|
@log_chain_to_mongo(attr_name = "logs_mongo")
|
|
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
|
|
@only_whitelisted_ips(attr_name = "whitelisted_ips")
|
|
@validate_input(data_validator = lambda x: NimbusSMSSendRequest(**x))
|
|
@limit_rate(
|
|
attr_name = "rate_limit_cache",
|
|
rate_limit = 1,
|
|
seconds = 30,
|
|
data_keys = ["recipientNo"],
|
|
allow_if_exception = True,
|
|
count_for_http_codes = [200]
|
|
)
|
|
@limit_rate(
|
|
attr_name = "rate_limit_cache",
|
|
rate_limit = 5,
|
|
seconds = 86_400,
|
|
data_keys = ["recipientNo"],
|
|
allow_if_exception = False,
|
|
count_for_http_codes = [200]
|
|
)
|
|
@handle_cancelled_request()
|
|
async def nimbus_send_sms(
|
|
inbound_headers: dict = None,
|
|
inbound_data: dict | NimbusSMSSendRequest = None,
|
|
inbound_files: dict = None,
|
|
**kwargs
|
|
):
|
|
|
|
"""
|
|
Sends one SMS out through Nimbus IT's system.
|
|
:param inbound_headers: auto-extracted by the decorators from 'async_quart.py'.
|
|
:param inbound_data: auto-extracted by the decorators from 'async_quart.py'.
|
|
:param inbound_files: auto-extracted by the decorators from 'async_quart.py'.
|
|
:return: A standard response structure from the function in 'async_quart.py'.
|
|
"""
|
|
|
|
# Get the default credentials ready:
|
|
default_nimbus_creds = current_app.script_data["sms"]["nimbus"]["tcaoff"]
|
|
|
|
# Initialise an object of Nimbus SMS:
|
|
sms_client = AsyncNimbusSMS(
|
|
entity_id = inbound_data.entityId or default_nimbus_creds["entityId"],
|
|
sender_id = inbound_data.senderId or default_nimbus_creds["senderId"],
|
|
user_id = inbound_data.userId or default_nimbus_creds["userId"],
|
|
api_key = inbound_data.apiKey or default_nimbus_creds["apiKey"]
|
|
)
|
|
|
|
# Send the message out:
|
|
response = await sms_client.send_sms(
|
|
template_id = inbound_data.templateId,
|
|
message = inbound_data.message,
|
|
recipient_number = inbound_data.recipientNo
|
|
)
|
|
|
|
# Done here:
|
|
return ResponseModel(
|
|
http_code = HttpCodes.SUCCESS if response.success else HttpCodes.INTERNAL_SERVER_ERROR,
|
|
status_code = StatusCodes.OK if response.success else StatusCodes.FAILED,
|
|
data = {
|
|
"client": response.client,
|
|
"ts": response.ts.timestamp(),
|
|
"messageId": response.messageId,
|
|
"rawResponse": response.rawResponse,
|
|
}
|
|
)
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MAIN PROGRAM ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|