(20241219) SMS module fully re-organized.

This commit is contained in:
2024-12-19 17:26:18 +05:30
parent d89a88ab8e
commit 79b771b48c
28 changed files with 1086 additions and 827 deletions
+8 -84
View File
@@ -10,8 +10,7 @@
OBJECTIVE:
To handle all SMS related behaviour for Nimbus It's service from one place.
This service is for India only.
To handle all SMS related behaviour for all third-party clients from one place.
REFERENCES:
@@ -103,7 +102,7 @@ import asyncio
# *****************************************************************************************************************
class NimbusSMSIndiaController(SMSController):
class AllSMSController(SMSController):
# ┏┓
# ┃ ┏┓┏┓┏╋┏┓┓┏┏╋┏┓┏┓
@@ -115,7 +114,7 @@ class NimbusSMSIndiaController(SMSController):
http_client: httpx.AsyncClient = None,
alert_url: str = None,
debug: bool = True,
debug_prefix: str = "Nimbus SMS (C) | ",
debug_prefix: str = "All SMS (C) | ",
debug_only_errors: bool = True
):
@@ -135,7 +134,7 @@ class NimbusSMSIndiaController(SMSController):
cache = cache,
alert_url = alert_url,
http_client = http_client,
base_filter = {"client": "nimbusSmsIndia"},
base_filter = None,
debug = debug,
debug_prefix = debug_prefix,
debug_only_errors = debug_only_errors
@@ -156,7 +155,7 @@ class NimbusSMSIndiaController(SMSController):
) -> SMSSendOneResult:
"""
Use this to send one SMS. There are just 2 steps here - send the SMS, and store its details in the database.
Just a placeholder to match the abstract parent.
:param mongo_data_conn: The database connection to use to perform this task.
:param auth_token: The auth token that will be used to send this message.
:param client: The third-party SMS client to use to send this message.
@@ -165,48 +164,7 @@ class NimbusSMSIndiaController(SMSController):
:return: The structured result of sending one message.
"""
# Send the SMS:
client_response = await client.send_sms(
recipient_number = message.recipientNo,
message = message.text,
template_id = message.templateId
)
# Convert the format of the SMS client's response to the core message model.
sent_message_model = CoreMessageModel(
ts = client_response.ts,
syncTs = date_time.get_current_utc_date_time(as_string = False),
tokenId = auth_token.authTokenId,
serviceType = auth_token.serviceType,
client = auth_token.client,
clientMessageId = client_response.messageId,
clientThreadId = message.recipientNo,
isSent = True,
isBroadcast = False,
sentSuccessfully = client_response.success,
sender = None,
recipient = message.recipientNo,
chat = None,
message = client_response.model_dump(),
snippet = message.text,
aiSnippet = None,
tags = list(set(tags + ["SMS", "Nimbus SMS", "India"]))
)
# Save the result to the database:
message_id = await self.save_one_message(
mongo_data_conn = mongo_data_conn,
message = sent_message_model
)
self._printer(message_id, client_response.success)
# Done here:
success = True if client_response.success and message_id else False
return SMSSendOneResult(
success = success,
message = "SMS sent successfully." if client_response.success else "SMS sending failed.",
smsMessage = message
)
raise NotImplementedError
async def send_many_sms(
self,
@@ -217,7 +175,7 @@ class NimbusSMSIndiaController(SMSController):
) -> SMSSendManyResults:
"""
Use this to send multiple SMS messages. This method just calls the individual SMS sending method for every
Just a placeholder to match the abstract parent.
individual message, and then aggregates the results.
:param mongo_data_conn: The database connection to use to perform this task.
:param auth_token: The auth token that will be used to send this message.
@@ -227,41 +185,7 @@ class NimbusSMSIndiaController(SMSController):
:return: The structured result of sending many SMS messages.
"""
# Start with a blank variable:
cumulative_results = SMSSendManyResults()
# Make the client from the auth-token:
client = AsyncNimbusSMS(
entity_id = auth_token.auth["entityId"],
sender_id = auth_token.auth["senderId"],
user_id = auth_token.auth["userId"],
api_key = auth_token.auth["apiKey"],
http_client = self._http_client
)
# Create and fire all the SMS-sending tasks:
tasks = [
self.send_one_sms(
mongo_data_conn = mongo_data_conn,
auth_token = auth_token,
client = client,
message = message,
tags = tags
)
for message in messages
]
individual_results = await asyncio.gather(*tasks)
# Prepare the final result:
for result in individual_results:
if result.success: cumulative_results.successCount += 1
else: cumulative_results.failureCount += 1
cumulative_results.totalCount += 1
cumulative_results.smsMessages.append(result.smsMessage)
cumulative_results.message = f"{cumulative_results.successCount}/{cumulative_results.totalCount} SMS sent."
# Done here:
return cumulative_results
raise NotImplementedError
# *****************************************************************************************************************