(20241209) SMS auth and sending ready.
This commit is contained in:
@@ -123,7 +123,7 @@ class SMSAuthModel(BaseModel):
|
||||
mongo_json = await mongo_conn.find_one_and_update(
|
||||
collection = self.AUTH_COLLECTION,
|
||||
filter = mongo_conn.dict_to_dot_notation({
|
||||
"serviceType": "email",
|
||||
"serviceType": auth_token.serviceType,
|
||||
"user": {
|
||||
"entityId": auth_token.user.entityId,
|
||||
"billingAccountId": auth_token.user.billingAccountId
|
||||
|
||||
+89
-102
@@ -6,12 +6,11 @@
|
||||
|
||||
DATE:
|
||||
|
||||
ORIGINAL: Thursday, 5th Dec., 2024
|
||||
UPGRADED: Monday, 9th Dec., 2024
|
||||
Monday, 9th Dec., 2024
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To work with auth details of SMS clients like Nimbus SMS (India) and Savvy Bulk SMS (Kenya).
|
||||
To send SMS from clients like Nimbus SMS (India) and Savvy Bulk SMS (Kenya).
|
||||
|
||||
REFERENCES:
|
||||
|
||||
@@ -41,11 +40,23 @@ from utils_v2.date_time import date_time
|
||||
from utils_v2.database.async_mysql_v2 import AsyncMySQL
|
||||
from utils_v2.database.async_mongo_v2 import AsyncMongo
|
||||
|
||||
# SMS-related utils:
|
||||
from utils_v2.sms.models.behaviour.nimbus.async_nimbus import AsyncNimbusSMS
|
||||
from utils_v2.sms.models.behaviour.savvy_bulk_sms.async_savvy_bulk_sms import AsyncSavvyBulkSMS
|
||||
|
||||
# Base model:
|
||||
from models.behaviour.base import BaseModel
|
||||
|
||||
# Data models:
|
||||
from models.data.core.auth_token import CoreAuthTokenModel
|
||||
from models.data.core.message import CoreMessageModel
|
||||
from models.data.api.sms.send import (
|
||||
SMSSendRequestHeaders,
|
||||
SMSSendRequestData,
|
||||
NimbusSMSIndiaMessage,
|
||||
SavvyBulkSMSKenyaMessage
|
||||
)
|
||||
from utils_v2.sms.models.data.sms_message import SentSMSMessageModel
|
||||
|
||||
# To work with MongoDB:
|
||||
from bson import ObjectId
|
||||
@@ -94,125 +105,101 @@ import copy
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class SMSAuthModel(BaseModel):
|
||||
class SMSSendModel(BaseModel):
|
||||
|
||||
AUTH_COLLECTION = "_authTokens"
|
||||
MESSAGES_COLLECTION = "_messages"
|
||||
|
||||
async def set(
|
||||
async def send_sms(
|
||||
self,
|
||||
db_conn: AsyncMySQL,
|
||||
mongo_conn: AsyncMongo,
|
||||
token_id: ObjectId | str,
|
||||
auth_token: CoreAuthTokenModel,
|
||||
inbound_data: SMSSendRequestData,
|
||||
session_token: str = None
|
||||
) -> ObjectId | None:
|
||||
) -> SentSMSMessageModel:
|
||||
|
||||
"""
|
||||
To store auth/tokens for a particular service to the database.
|
||||
:param db_conn: The database connection (MariaDB) to use to perform the action.
|
||||
:param mongo_conn: The database connection (MongoDB) to use to perform the action.
|
||||
:param auth_token: An instance of the core auth-token model that holds data in the database.
|
||||
:param session_token: The session token of the user who requested this service.
|
||||
:return: An ObjectId to later store the granted tokens.
|
||||
"""
|
||||
|
||||
# Note down the timestamp at which this event occurred:
|
||||
request_ts = date_time.get_current_utc_date_time(as_string = False)
|
||||
# Basic prep:
|
||||
event_ts = date_time.get_current_utc_date_time(as_string = False)
|
||||
client_response = None
|
||||
message_id = None
|
||||
sms_sent = None
|
||||
|
||||
# Get the identifier from the database:
|
||||
# BE CAREFUL WITH THE KEYS HERE, THEY SHOULD MATCH THE FIELDS OF THE CORE AUTH-TOKEN MODEL:
|
||||
mongo_json = await mongo_conn.find_one_and_update(
|
||||
collection = self.AUTH_COLLECTION,
|
||||
filter = mongo_conn.dict_to_dot_notation({
|
||||
"serviceType": auth_token.serviceType,
|
||||
"user": {
|
||||
"entityId": auth_token.user.entityId,
|
||||
"billingAccountId": auth_token.user.billingAccountId
|
||||
},
|
||||
"clientUserId": auth_token.clientUserId
|
||||
}),
|
||||
update = {
|
||||
"$set": {
|
||||
"lastRequestTs": auth_token.lastRequestTs,
|
||||
"status": auth_token.status,
|
||||
"syncFreq": auth_token.syncFreq
|
||||
},
|
||||
"$setOnInsert": {
|
||||
"version": auth_token.version,
|
||||
"serviceType": auth_token.serviceType,
|
||||
"client": auth_token.client,
|
||||
"authType": auth_token.authType,
|
||||
"user": auth_token.user.model_dump(),
|
||||
"clientUserId": auth_token.clientUserId,
|
||||
"auth": auth_token.auth,
|
||||
"token": auth_token.token,
|
||||
"firstRefreshTs": auth_token.firstRefreshTs,
|
||||
"lastRefreshTs": auth_token.lastRefreshTs,
|
||||
"firstRequestTs": auth_token.firstRequestTs or request_ts
|
||||
}
|
||||
},
|
||||
projection = {
|
||||
"_id": True
|
||||
},
|
||||
upsert = True,
|
||||
return_updated = True
|
||||
)
|
||||
# ┏┓ ┳┓• ┓ ┏┓┳┳┓┏┓ ┳ ┓•
|
||||
# ┣ ┏┓┏┓ ┃┃┓┏┳┓┣┓┓┏┏ ┗┓┃┃┃┗┓ ┃┏┓┏┫┓┏┓
|
||||
# ┻ ┗┛┛ ┛┗┗┛┗┗┗┛┗┻┛ ┗┛┛ ┗┗┛ ┻┛┗┗┻┗┗┻
|
||||
|
||||
# Tell MariaDB that an authorization request was initiated:
|
||||
db_json = {}
|
||||
if mongo_json is not None:
|
||||
token_notes = auth_token.clientUserId
|
||||
db_json = await self.call_procedure(
|
||||
db_conn = db_conn,
|
||||
proc_name = "entity_integration_save",
|
||||
proc_args = (
|
||||
auth_token.user.entityId, # ..................................... 'p_entity_id'
|
||||
auth_token.client, # ............................................ 'p_provider'
|
||||
auth_token.status, # ............................................ 'p_current_status'
|
||||
"Auth Details Accepted", # ...................................... 'p_last_action'
|
||||
None, # ......................................................... 'p_display_name'
|
||||
None, # ......................................................... 'p_display_picture'
|
||||
str(mongo_json["_id"]), # ....................................... 'p_token_id'
|
||||
json.to_string(python_data = token_notes, no_space = True), # ... 'p_notes'
|
||||
auth_token.user.userId # ........................................ 'p_created_by'
|
||||
),
|
||||
session_token = session_token
|
||||
if isinstance(inbound_data.message, NimbusSMSIndiaMessage):
|
||||
|
||||
# Prepare the client:
|
||||
sms_client = AsyncNimbusSMS(
|
||||
entity_id = auth_token.auth.get("entityId"),
|
||||
sender_id = auth_token.auth.get("senderId"),
|
||||
user_id = auth_token.auth.get("userId"),
|
||||
api_key = auth_token.auth.get("apiKey"),
|
||||
http_client = self._http_client
|
||||
)
|
||||
|
||||
# Send the SMS:
|
||||
client_response = await sms_client.send_sms(
|
||||
recipient_number = inbound_data.message.recipientNo,
|
||||
message = inbound_data.message.text,
|
||||
template_id = inbound_data.message.templateId
|
||||
)
|
||||
|
||||
# ┏┓ ┏┓ ┳┓ ┓┓ ┏┓┳┳┓┏┓ ┓┏┓
|
||||
# ┣ ┏┓┏┓ ┗┓┏┓┓┏┓┏┓┏ ┣┫┓┏┃┃┏ ┗┓┃┃┃┗┓ ┃┫ ┏┓┏┓┓┏┏┓
|
||||
# ┻ ┗┛┛ ┗┛┗┻┗┛┗┛┗┫ ┻┛┗┻┗┛┗ ┗┛┛ ┗┗┛ ┛┗┛┗ ┛┗┗┫┗┻
|
||||
# ┛ ┛
|
||||
|
||||
elif isinstance(inbound_data.message, SavvyBulkSMSKenyaMessage):
|
||||
|
||||
# Prepare the client:
|
||||
sms_client = AsyncSavvyBulkSMS(
|
||||
api_key = auth_token.auth.get("apiKey"),
|
||||
partner_id = auth_token.auth.get("partnerId"),
|
||||
short_code = auth_token.auth.get("shortCode"),
|
||||
http_client = self._http_client
|
||||
)
|
||||
|
||||
# Send the SMS:
|
||||
client_response = await sms_client.send_sms(
|
||||
recipient_number = inbound_data.message.recipientNo,
|
||||
message = inbound_data.message.text
|
||||
)
|
||||
|
||||
# ┏┓ ┏┳┓┓ ┳┳┓
|
||||
# ┗┓┏┓┓┏┏┓ ┃ ┣┓┏┓ ┃┃┃┏┓┏┏┏┓┏┓┏┓
|
||||
# ┗┛┗┻┗┛┗ ┻ ┛┗┗ ┛ ┗┗ ┛┛┗┻┗┫┗
|
||||
# ┛
|
||||
|
||||
# Save the message:
|
||||
if client_response:
|
||||
message_id = await mongo_conn.insert_one(
|
||||
collection = self.MESSAGES_COLLECTION,
|
||||
document = CoreMessageModel(
|
||||
ts = event_ts,
|
||||
readTs = event_ts,
|
||||
tokenId = ObjectId(token_id),
|
||||
serviceType = auth_token.serviceType,
|
||||
client = auth_token.client,
|
||||
clientMessageId = client_response.messageId,
|
||||
clientThreadId = None,
|
||||
isInward = False,
|
||||
sentSuccessfully = client_response.success,
|
||||
payload = client_response.model_dump()
|
||||
).model_dump()
|
||||
)
|
||||
|
||||
# Done here:
|
||||
return mongo_json["_id"] if mongo_json and db_json.get("status") == 1 else None
|
||||
|
||||
async def get(
|
||||
self,
|
||||
mongo_conn: AsyncMongo,
|
||||
token_id: ObjectId | str = None,
|
||||
**kwargs
|
||||
) -> dict | None:
|
||||
|
||||
"""
|
||||
To retrieve stored auth/tokens from the database.
|
||||
:param mongo_conn: The database connection (MongoDB) to use to perform the action.
|
||||
:param token_id: The identifier granted providing auth details for the first time in 'set_token'.
|
||||
:param kwargs: Any set of key-value pairs to build custom search criteria. This could be things like the user
|
||||
info, the client, the type of authentication used, or even the kind of service.
|
||||
:return: The retrieved record that has the token, and information about the service and client if found, else
|
||||
None when there is no matching record.
|
||||
"""
|
||||
|
||||
# Build the filter:
|
||||
filter_json = {k: v for k, v in kwargs.items()}
|
||||
if token_id: filter_json["_id"] = ObjectId(token_id)
|
||||
|
||||
# If there is no search criteria, we exit with failure:
|
||||
if not filter_json: return None
|
||||
|
||||
# If there is some filtering possible, we fetch the token:
|
||||
token = await mongo_conn.find_one(
|
||||
collection = self.AUTH_COLLECTION,
|
||||
filter = filter_json,
|
||||
)
|
||||
|
||||
# Done here:
|
||||
return CoreAuthTokenModel(**token) if token else None
|
||||
return client_response
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
|
||||
Reference in New Issue
Block a user