(20241213) SMS sending ready.
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To receive auth details for various software.
|
||||
To receive auth details for various financial institutions.
|
||||
|
||||
REFERENCES:
|
||||
|
||||
@@ -81,7 +81,7 @@ import asyncio
|
||||
|
||||
|
||||
# Related to Quart:
|
||||
sw_auth_bp = Blueprint("sw_auth", __name__)
|
||||
fi_auth_bp = Blueprint("fi_auth", __name__)
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
@@ -101,7 +101,7 @@ sw_auth_bp = Blueprint("sw_auth", __name__)
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
@sw_auth_bp.record_once
|
||||
@fi_auth_bp.record_once
|
||||
def init(blueprint_setup_state):
|
||||
|
||||
# This gets called when the blueprint is registered.
|
||||
@@ -112,7 +112,7 @@ def init(blueprint_setup_state):
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@sw_auth_bp.route("/auth", methods = ["POST"])
|
||||
@fi_auth_bp.route("/auth", methods = ["POST"])
|
||||
@set_api_version(api_version = "1.0.0")
|
||||
@read_input(sanitize_headers = False, sanitize_data = False)
|
||||
@get_session_info(key = "X-Session-Token", session_coro = "get_session")
|
||||
|
||||
+15
-23
@@ -157,30 +157,16 @@ async def send_sms(
|
||||
http_code = HttpCodes.UNAUTHORIZED
|
||||
)
|
||||
|
||||
# Fetch the auth-token to use to send this message:
|
||||
auth_token = await current_app.sms_auth_model.get(
|
||||
mongo_conn = current_app.data_mongo,
|
||||
token_id = inbound_data.tokenId
|
||||
)
|
||||
|
||||
# If no auth-token was found, we return with failure:
|
||||
if not auth_token: return ResponseModel(
|
||||
status_code = StatusCodes.FAILED,
|
||||
http_code = HttpCodes.BAD_REQUEST,
|
||||
message = f"no such token id"
|
||||
)
|
||||
|
||||
# ┏┓ ┓ ┏┳┓┓ ┏┓┳┳┓┏┓
|
||||
# ┗┓┏┓┏┓┏┫ ┃ ┣┓┏┓ ┗┓┃┃┃┗┓
|
||||
# ┗┛┗ ┛┗┗┻ ┻ ┛┗┗ ┗┛┛ ┗┗┛
|
||||
|
||||
# client_response = await current_app.sms_send_model.send_sms(
|
||||
# mongo_conn = current_app.data_mongo,
|
||||
# token_id = inbound_data.tokenId,
|
||||
# auth_token = auth_token,
|
||||
# inbound_data = inbound_data,
|
||||
# session_token = inbound_headers["X-Session-Token"]
|
||||
# )
|
||||
client_response = await current_app.sms_controller.send(
|
||||
mongo_conn = current_app.data_mongo,
|
||||
http_client = current_app.http_client,
|
||||
token_id = inbound_data.tokenId,
|
||||
messages = inbound_data.message
|
||||
)
|
||||
|
||||
# ┳┓
|
||||
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
|
||||
@@ -188,10 +174,16 @@ async def send_sms(
|
||||
# ┛
|
||||
|
||||
# Done here:
|
||||
success = True if client_response.successCount else False
|
||||
return ResponseModel(
|
||||
status_code = StatusCodes.OK if client_response.success else StatusCodes.FAILED,
|
||||
http_code = HttpCodes.SUCCESS if client_response.success else HttpCodes.INTERNAL_SERVER_ERROR,
|
||||
message = None if client_response.success else f"SMS Client: {client_response.brief}"
|
||||
status_code = StatusCodes.OK if success else StatusCodes.FAILED,
|
||||
http_code = HttpCodes.SUCCESS if success else HttpCodes.INTERNAL_SERVER_ERROR,
|
||||
data = {
|
||||
"successCount": client_response.successCount,
|
||||
"failureCount": client_response.failureCount,
|
||||
"totalCount": client_response.totalCount,
|
||||
},
|
||||
message = client_response.message
|
||||
)
|
||||
|
||||
|
||||
|
||||
+2
-2
@@ -92,7 +92,7 @@ from api.blueprints.mail.retrieve.list import mail_list_bp
|
||||
from api.blueprints.mail.retrieve.get import mail_get_bp
|
||||
from api.blueprints.mail.tags.update import mail_tags_update_bp
|
||||
from api.blueprints.sms.auth import sms_auth_bp
|
||||
# from api.blueprints.sms.send import sms_send_bp
|
||||
from api.blueprints.sms.send import sms_send_bp
|
||||
# from api.blueprints.chat.auth import chat_auth_bp
|
||||
# from api.blueprints.chat.webhook import chat_webhook_bp
|
||||
from api.blueprints.software.auth import sw_auth_bp
|
||||
@@ -133,7 +133,7 @@ app.register_blueprint(mail_list_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
app.register_blueprint(mail_get_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
app.register_blueprint(mail_tags_update_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
app.register_blueprint(sms_auth_bp, url_prefix = f"/{MODULE_BASE}/sms")
|
||||
# app.register_blueprint(sms_send_bp, url_prefix = f"/{MODULE_BASE}/sms")
|
||||
app.register_blueprint(sms_send_bp, url_prefix = f"/{MODULE_BASE}/sms")
|
||||
# app.register_blueprint(chat_auth_bp, url_prefix = f"/{MODULE_BASE}/chat")
|
||||
# app.register_blueprint(chat_webhook_bp, url_prefix = f"/{MODULE_BASE}/chat")
|
||||
app.register_blueprint(sw_auth_bp, url_prefix = f"/{MODULE_BASE}/software")
|
||||
|
||||
+75
-5
@@ -21,7 +21,7 @@
|
||||
N/A
|
||||
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** IMPORT ***
|
||||
@@ -64,6 +64,7 @@ from utils_v2.sms.models.data.sms_message import SentSMSMessageModel
|
||||
|
||||
# To work with MongoDB:
|
||||
from bson import ObjectId
|
||||
from pymongo import InsertOne
|
||||
|
||||
# To work with datatypes:
|
||||
from typing import Literal, List, Dict, Any
|
||||
@@ -223,6 +224,7 @@ class SMSController:
|
||||
isBroadcast = False,
|
||||
sentSuccessfully = client_response.success,
|
||||
sender = None,
|
||||
recipient = message.recipientNo,
|
||||
chat = None,
|
||||
message = client_response.model_dump(),
|
||||
snippet = message.text,
|
||||
@@ -233,13 +235,67 @@ class SMSController:
|
||||
# Done here:
|
||||
return send_results
|
||||
|
||||
@staticmethod
|
||||
async def __send_from_savvy_bulk_sms_kenya(
|
||||
http_client: httpx.AsyncClient,
|
||||
token_id: ObjectId | str,
|
||||
auth_token: CoreAuthTokenModel,
|
||||
messages: List[SavvyBulkSMSKenyaMessage],
|
||||
) -> SMSSendManyResults:
|
||||
|
||||
# Start with a blank variable:
|
||||
send_results = SMSSendManyResults()
|
||||
|
||||
# Initialize the third-party client:
|
||||
client = AsyncSavvyBulkSMS(
|
||||
partner_id = auth_token.auth["partnerId"],
|
||||
short_code = auth_token.auth["shortCode"],
|
||||
api_key = auth_token.auth["apiKey"],
|
||||
http_client = http_client
|
||||
)
|
||||
|
||||
# Iterate over all the messages you need to send:
|
||||
for message in messages:
|
||||
|
||||
# Send the SMS and return the response:
|
||||
client_response = await client.send_sms(
|
||||
recipient_number = message.recipientNo,
|
||||
message = message.text
|
||||
)
|
||||
|
||||
# Note down the results:
|
||||
send_results.totalCount += 1
|
||||
if client_response.success: send_results.successCount += 1
|
||||
else: send_results.failureCount += 1
|
||||
send_results.smsMessages.append(CoreMessageModel(
|
||||
ts = client_response.ts,
|
||||
syncTs = date_time.get_current_utc_date_time(as_string = False),
|
||||
tokenId = ObjectId(token_id),
|
||||
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 = ["sms", "savvyBulkSmsKenya"]
|
||||
))
|
||||
|
||||
# Done here:
|
||||
return send_results
|
||||
|
||||
async def send(
|
||||
self,
|
||||
mongo_conn: AsyncMongo,
|
||||
http_client: httpx.AsyncClient,
|
||||
token_id: ObjectId | str,
|
||||
messages: List[NimbusSMSIndiaMessage | SavvyBulkSMSKenyaMessage],
|
||||
session_token: str
|
||||
messages: List[NimbusSMSIndiaMessage | SavvyBulkSMSKenyaMessage]
|
||||
) -> SMSSendManyResults:
|
||||
|
||||
# Start by assuming failure:
|
||||
@@ -266,14 +322,28 @@ class SMSController:
|
||||
messages = messages
|
||||
)
|
||||
case "savvyBulkSmsKenya":
|
||||
pass
|
||||
send_results = await self.__send_from_savvy_bulk_sms_kenya(
|
||||
http_client = http_client,
|
||||
token_id = token_id,
|
||||
auth_token = auth_token,
|
||||
messages = messages
|
||||
)
|
||||
case _:
|
||||
send_results.message = f"invalid client {auth_token.client}"
|
||||
|
||||
# Save the results to MongoDB:
|
||||
|
||||
tasks = []
|
||||
for sms in send_results.smsMessages:
|
||||
message_json = sms.model_dump()
|
||||
message_json.pop("_id", None)
|
||||
tasks.append(current_app.core_message_controller.insert(
|
||||
mongo_conn = mongo_conn,
|
||||
message = message_json
|
||||
))
|
||||
results = await asyncio.gather(*tasks)
|
||||
|
||||
# Done here:
|
||||
send_results.message = f"{send_results.successCount}/{send_results.totalCount} message(s) sent"
|
||||
return send_results
|
||||
|
||||
# ┓ • ┏┓ ┏┓ ┳┳┓
|
||||
|
||||
@@ -178,7 +178,7 @@ class SMSSendRequestHeaders(BaseModel):
|
||||
class SMSSendRequestData(BaseModel):
|
||||
|
||||
tokenId: ObjectId = Field(description = "the auth token to use to send this message")
|
||||
message: Union[List[NimbusSMSIndiaMessage], List[SavvyBulkSMSKenyaMessage]]
|
||||
message: List[NimbusSMSIndiaMessage] | List[SavvyBulkSMSKenyaMessage]
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
@@ -220,7 +220,7 @@ class SMSSendOneResult(BaseModel):
|
||||
default = None
|
||||
)
|
||||
|
||||
smsMessage: Union[NimbusSMSIndiaMessage, SavvyBulkSMSKenyaMessage] = Field(
|
||||
smsMessage: NimbusSMSIndiaMessage | SavvyBulkSMSKenyaMessage = Field(
|
||||
description = "the actual data of the sms",
|
||||
default = None
|
||||
)
|
||||
|
||||
@@ -153,6 +153,12 @@ class CoreMessageModel(BaseModel):
|
||||
frozen = True
|
||||
)
|
||||
|
||||
recipient: str | None = Field(
|
||||
description = "the name of the recipient; null if you are the recipient",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
chat: str | None = Field(
|
||||
description = "the name of the chat where the message was exchanged; relevant in chat apps like telegram",
|
||||
frozen = True
|
||||
|
||||
Reference in New Issue
Block a user