(20241209) SMS auth and sending ready.
This commit is contained in:
@@ -64,6 +64,7 @@ from shared import constants
|
||||
|
||||
# Data Models:
|
||||
from models.data.api.ai.llm import LLMRequestHeaders, LLMInput
|
||||
from models.data.core.user_info import CoreUserInfoModel
|
||||
|
||||
# For asynchronous activities:
|
||||
import asyncio
|
||||
@@ -163,7 +164,7 @@ async def invoke_llm(
|
||||
# Call the LLM and see if its service worked or not:
|
||||
llm_response = await current_app.llm.invoke(
|
||||
mongo_conn = current_app.data_mongo,
|
||||
user_info = kwargs["session_info"],
|
||||
user_info = CoreUserInfoModel(**kwargs["session_info"]),
|
||||
llm_input = inbound_data
|
||||
)
|
||||
success = False if llm_response.output is None else True
|
||||
@@ -183,6 +184,7 @@ async def invoke_llm(
|
||||
"model": llm_response.model,
|
||||
"output": llm_response.output,
|
||||
"tokens": llm_response.tokens.model_dump(),
|
||||
"invocationId": llm_response.invocationId
|
||||
} if success else None
|
||||
)
|
||||
|
||||
|
||||
@@ -72,6 +72,7 @@ from shared import constants
|
||||
# Data Models:
|
||||
from models.data.api.mail.sync import MailSyncRequestHeaders, MailSyncRequestData
|
||||
from models.data.api.mail.sync import MailSyncOneResult, MailSyncManyResults
|
||||
from models.data.core.user_info import CoreUserInfoModel
|
||||
|
||||
# To work with datatypes:
|
||||
from typing import Literal
|
||||
@@ -126,7 +127,7 @@ def init(blueprint_setup_state):
|
||||
|
||||
|
||||
async def sync_mails(
|
||||
user_info: dict,
|
||||
user_info: CoreUserInfoModel,
|
||||
mongo_conn: AsyncMongo,
|
||||
llm: ChatOpenAI,
|
||||
inbound_headers: dict,
|
||||
@@ -136,6 +137,7 @@ async def sync_mails(
|
||||
"""
|
||||
A very simple function, but kept separate so that we get the option to switch between running it in the foreground
|
||||
and running it in the background.
|
||||
:param user_info: The information of the user as extracted from the session token.
|
||||
:param mongo_conn: The instance of the database connector to use to sync the mails.
|
||||
:param llm: The instance of the LLM to use to summarize the mails.
|
||||
:param inbound_headers: The headers that came in with the request.
|
||||
@@ -215,7 +217,7 @@ async def sync_mail(
|
||||
if mode in ["background", "bg"]:
|
||||
current_app.add_background_task(
|
||||
sync_mails,
|
||||
user_info = kwargs["session_info"],
|
||||
user_info = CoreUserInfoModel(**kwargs["session_info"]),
|
||||
mongo_conn = current_app.data_mongo,
|
||||
llm = current_app.llm,
|
||||
inbound_headers = inbound_headers,
|
||||
@@ -229,7 +231,7 @@ async def sync_mail(
|
||||
|
||||
# Otherwise we process it right here:
|
||||
sync_results = await sync_mails(
|
||||
user_info = kwargs["session_info"],
|
||||
user_info = CoreUserInfoModel(**kwargs["session_info"]),
|
||||
mongo_conn = current_app.data_mongo,
|
||||
llm = current_app.llm,
|
||||
inbound_headers = inbound_headers,
|
||||
|
||||
+53
-29
@@ -59,15 +59,12 @@ from utils_v2.api.async_quart import (
|
||||
handle_cancelled_request
|
||||
)
|
||||
|
||||
# SMS-related utils:
|
||||
from utils_v2.sms.nimbus.async_nimbus import AsyncNimbusSMS
|
||||
from utils_v2.sms.savvy_bulk_sms.async_savvy_bulk_sms import AsyncSavvyBulkSMS
|
||||
|
||||
# Common:
|
||||
from shared import constants
|
||||
|
||||
# Data Models:
|
||||
from models.data.api.sms.auth import SMSAuthRequestHeaders, SMSAuthRequestData
|
||||
from models.data.core.auth_token import CoreAuthTokenModel
|
||||
|
||||
# For asynchronous activities:
|
||||
import asyncio
|
||||
@@ -132,7 +129,7 @@ def init(blueprint_setup_state):
|
||||
data_validator = lambda x: SMSAuthRequestData(**x)
|
||||
)
|
||||
@handle_cancelled_request()
|
||||
async def request_oauth_authorization_url(
|
||||
async def authorize_sms_client(
|
||||
inbound_headers: dict | SMSAuthRequestHeaders = None,
|
||||
inbound_data: dict | SMSAuthRequestData = None,
|
||||
inbound_files: dict = None,
|
||||
@@ -167,22 +164,36 @@ async def request_oauth_authorization_url(
|
||||
# ┣ ┏┓┏┓ ┃┃┓┏┳┓┣┓┓┏┏ ┗┓┃┃┃┗┓ ┃┏┓┏┫┓┏┓
|
||||
# ┻ ┗┛┛ ┛┗┗┛┗┗┗┛┗┻┛ ┗┛┛ ┗┗┛ ┻┛┗┗┻┗┗┻
|
||||
|
||||
if inbound_data.messageClient == "nimbusSmsIndia":
|
||||
if inbound_data.smsClient == "nimbusSmsIndia":
|
||||
|
||||
token_id = await current_app.sms_auth_model.set(
|
||||
db_conn = current_app.sql_writer,
|
||||
mongo_conn = current_app.data_mongo,
|
||||
user_info = kwargs["session_info"],
|
||||
client_user_id = {
|
||||
"userId": inbound_data.auth.userId,
|
||||
"senderId": inbound_data.auth.senderId,
|
||||
"entityId": inbound_data.auth.entityId
|
||||
},
|
||||
auth = inbound_data.auth.model_dump(),
|
||||
token = None,
|
||||
service_client = inbound_data.smsClient,
|
||||
auth_type = "auth",
|
||||
sync_freq = 300,
|
||||
auth_token = CoreAuthTokenModel(
|
||||
serviceType = "sms",
|
||||
client = inbound_data.smsClient,
|
||||
authType = "auth",
|
||||
auth = inbound_data.auth.model_dump(),
|
||||
user = kwargs.get("session_info"),
|
||||
clientUserId = {
|
||||
"userId": inbound_data.auth.userId,
|
||||
"senderId": inbound_data.auth.senderId,
|
||||
"entityId": inbound_data.auth.entityId
|
||||
},
|
||||
status = "active",
|
||||
syncFreq = 60
|
||||
),
|
||||
# user_info = kwargs["session_info"],
|
||||
# client_user_id = {
|
||||
# "userId": inbound_data.auth.userId,
|
||||
# "senderId": inbound_data.auth.senderId,
|
||||
# "entityId": inbound_data.auth.entityId
|
||||
# },
|
||||
# auth = inbound_data.auth.model_dump(),
|
||||
# token = None,
|
||||
# service_client = inbound_data.smsClient,
|
||||
# auth_type = "auth",
|
||||
# sync_freq = 300,
|
||||
session_token = inbound_headers["X-Session-Token"]
|
||||
)
|
||||
|
||||
@@ -191,21 +202,34 @@ async def request_oauth_authorization_url(
|
||||
# ┻ ┗┛┛ ┗┛┗┻┗┛┗┛┗┫ ┻┛┗┻┗┛┗ ┗┛┛ ┗┗┛ ┛┗┛┗ ┛┗┗┫┗┻
|
||||
# ┛ ┛
|
||||
|
||||
if inbound_data.messageClient == "savvyBulkSmsKenya":
|
||||
elif inbound_data.smsClient == "savvyBulkSmsKenya":
|
||||
|
||||
token_id = await current_app.sms_auth_model.set(
|
||||
db_conn = current_app.sql_writer,
|
||||
mongo_conn = current_app.data_mongo,
|
||||
user_info = kwargs["session_info"],
|
||||
client_user_id = {
|
||||
"partnerId": inbound_data.auth.partnerId,
|
||||
"shortCode": inbound_data.auth.shortCode
|
||||
},
|
||||
auth = inbound_data.auth.model_dump(),
|
||||
token = None,
|
||||
service_client = inbound_data.smsClient,
|
||||
auth_type = "auth",
|
||||
sync_freq = 300,
|
||||
auth_token=CoreAuthTokenModel(
|
||||
serviceType = "sms",
|
||||
client = inbound_data.smsClient,
|
||||
authType = "auth",
|
||||
auth = inbound_data.auth.model_dump(),
|
||||
user = kwargs.get("session_info"),
|
||||
clientUserId = {
|
||||
"partnerId": inbound_data.auth.partnerId,
|
||||
"shortCode": inbound_data.auth.shortCode
|
||||
},
|
||||
status = "active",
|
||||
syncFreq = 60
|
||||
),
|
||||
# user_info = kwargs["session_info"],
|
||||
# client_user_id = {
|
||||
# "partnerId": inbound_data.auth.partnerId,
|
||||
# "shortCode": inbound_data.auth.shortCode
|
||||
# },
|
||||
# auth = inbound_data.auth.model_dump(),
|
||||
# token = None,
|
||||
# service_client = inbound_data.smsClient,
|
||||
# auth_type = "auth",
|
||||
# sync_freq = 300,
|
||||
session_token = inbound_headers["X-Session-Token"]
|
||||
)
|
||||
|
||||
@@ -219,7 +243,7 @@ async def request_oauth_authorization_url(
|
||||
status_code = StatusCodes.OK if token_id else StatusCodes.FAILED,
|
||||
http_code = HttpCodes.SUCCESS if token_id else HttpCodes.INTERNAL_SERVER_ERROR,
|
||||
data = {
|
||||
"smsClient": inbound_data.messageClient,
|
||||
"smsClient": inbound_data.smsClient,
|
||||
"authorized": True
|
||||
}
|
||||
)
|
||||
|
||||
+37
-95
@@ -6,7 +6,7 @@
|
||||
|
||||
DATE:
|
||||
|
||||
Thursday, 5th Dec., 2024
|
||||
Monday, 9th Dec., 2024
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
@@ -59,17 +59,13 @@ from utils_v2.api.async_quart import (
|
||||
handle_cancelled_request
|
||||
)
|
||||
|
||||
# SMS-related utils:
|
||||
from utils_v2.sms.nimbus.async_nimbus import AsyncNimbusSMS
|
||||
from utils_v2.sms.savvy_bulk_sms.async_savvy_bulk_sms import AsyncSavvyBulkSMS
|
||||
# Data Models:
|
||||
from models.data.api.sms.send import SMSSendRequestHeaders, SMSSendRequestData
|
||||
from models.data.core.auth_token import CoreAuthTokenModel
|
||||
|
||||
# Common:
|
||||
from shared import constants
|
||||
|
||||
# Data Models:
|
||||
from models.data.api.sms.auth import SMSAuthRequestHeaders, SMSAuthRequestData
|
||||
from models.data.core.auth_token import CoreAuthTokenModel
|
||||
|
||||
# For asynchronous activities:
|
||||
import asyncio
|
||||
|
||||
@@ -82,7 +78,7 @@ import asyncio
|
||||
|
||||
|
||||
# Related to Quart:
|
||||
sms_auth_bp = Blueprint("sms_auth", __name__)
|
||||
sms_send_bp = Blueprint("sms_send", __name__)
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
@@ -102,7 +98,7 @@ sms_auth_bp = Blueprint("sms_auth", __name__)
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
@sms_auth_bp.record_once
|
||||
@sms_send_bp.record_once
|
||||
def init(blueprint_setup_state):
|
||||
|
||||
# This gets called when the blueprint is registered.
|
||||
@@ -113,7 +109,7 @@ def init(blueprint_setup_state):
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@sms_auth_bp.route("/auth", methods = ["POST"])
|
||||
@sms_send_bp.route("/send", 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")
|
||||
@@ -124,18 +120,18 @@ def init(blueprint_setup_state):
|
||||
operation = "smsAuthApi",
|
||||
log_input = True,
|
||||
log_output = True,
|
||||
sensitive_keys = ["sessionToken", "X-Session-Token"]
|
||||
sensitive_keys = ["sessionToken", "X-Session-Token", "tokenId"]
|
||||
)
|
||||
@log_chain_to_mongo(attr_name = "logs_mongo")
|
||||
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
|
||||
@validate_input(
|
||||
header_validator = lambda x: SMSAuthRequestHeaders(**x).model_dump(),
|
||||
data_validator = lambda x: SMSAuthRequestData(**x)
|
||||
header_validator = lambda x: SMSSendRequestHeaders(**x).model_dump(),
|
||||
data_validator = lambda x: SMSSendRequestData(**x)
|
||||
)
|
||||
@handle_cancelled_request()
|
||||
async def request_oauth_authorization_url(
|
||||
inbound_headers: dict | SMSAuthRequestHeaders = None,
|
||||
inbound_data: dict | SMSAuthRequestData = None,
|
||||
async def send_sms(
|
||||
inbound_headers: dict | SMSSendRequestHeaders = None,
|
||||
inbound_data: dict | SMSSendRequestData = None,
|
||||
inbound_files: dict = None,
|
||||
**kwargs
|
||||
):
|
||||
@@ -161,81 +157,30 @@ async def request_oauth_authorization_url(
|
||||
http_code = HttpCodes.UNAUTHORIZED
|
||||
)
|
||||
|
||||
# Start by assuming failure:
|
||||
token_id = None
|
||||
# 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"
|
||||
)
|
||||
|
||||
if inbound_data.smsClient == "nimbusSmsIndia":
|
||||
# ┏┓ ┓ ┏┳┓┓ ┏┓┳┳┓┏┓
|
||||
# ┗┓┏┓┏┓┏┫ ┃ ┣┓┏┓ ┗┓┃┃┃┗┓
|
||||
# ┗┛┗ ┛┗┗┻ ┻ ┛┗┗ ┗┛┛ ┗┗┛
|
||||
|
||||
token_id = await current_app.sms_auth_model.set(
|
||||
db_conn = current_app.sql_writer,
|
||||
mongo_conn = current_app.data_mongo,
|
||||
auth_token = CoreAuthTokenModel(
|
||||
serviceType = "sms",
|
||||
client = inbound_data.smsClient,
|
||||
authType = "auth",
|
||||
auth = inbound_data.auth.model_dump(),
|
||||
user = kwargs.get("session_info"),
|
||||
clientUserId = {
|
||||
"userId": inbound_data.auth.userId,
|
||||
"senderId": inbound_data.auth.senderId,
|
||||
"entityId": inbound_data.auth.entityId
|
||||
},
|
||||
status = "active",
|
||||
syncFreq = 60
|
||||
),
|
||||
# user_info = kwargs["session_info"],
|
||||
# client_user_id = {
|
||||
# "userId": inbound_data.auth.userId,
|
||||
# "senderId": inbound_data.auth.senderId,
|
||||
# "entityId": inbound_data.auth.entityId
|
||||
# },
|
||||
# auth = inbound_data.auth.model_dump(),
|
||||
# token = None,
|
||||
# service_client = inbound_data.smsClient,
|
||||
# auth_type = "auth",
|
||||
# sync_freq = 300,
|
||||
session_token = inbound_headers["X-Session-Token"]
|
||||
)
|
||||
|
||||
# ┏┓ ┏┓ ┳┓ ┓┓ ┏┓┳┳┓┏┓ ┓┏┓
|
||||
# ┣ ┏┓┏┓ ┗┓┏┓┓┏┓┏┓┏ ┣┫┓┏┃┃┏ ┗┓┃┃┃┗┓ ┃┫ ┏┓┏┓┓┏┏┓
|
||||
# ┻ ┗┛┛ ┗┛┗┻┗┛┗┛┗┫ ┻┛┗┻┗┛┗ ┗┛┛ ┗┗┛ ┛┗┛┗ ┛┗┗┫┗┻
|
||||
# ┛ ┛
|
||||
|
||||
elif inbound_data.smsClient == "savvyBulkSmsKenya":
|
||||
|
||||
token_id = await current_app.sms_auth_model.set(
|
||||
db_conn = current_app.sql_writer,
|
||||
mongo_conn = current_app.data_mongo,
|
||||
auth_token=CoreAuthTokenModel(
|
||||
serviceType = "sms",
|
||||
client = inbound_data.smsClient,
|
||||
authType = "auth",
|
||||
auth = inbound_data.auth.model_dump(),
|
||||
user = kwargs.get("session_info"),
|
||||
clientUserId = {
|
||||
"partnerId": inbound_data.auth.partnerId,
|
||||
"shortCode": inbound_data.auth.shortCode
|
||||
},
|
||||
status = "active",
|
||||
syncFreq = 60
|
||||
),
|
||||
# user_info = kwargs["session_info"],
|
||||
# client_user_id = {
|
||||
# "partnerId": inbound_data.auth.partnerId,
|
||||
# "shortCode": inbound_data.auth.shortCode
|
||||
# },
|
||||
# auth = inbound_data.auth.model_dump(),
|
||||
# token = None,
|
||||
# service_client = inbound_data.smsClient,
|
||||
# auth_type = "auth",
|
||||
# sync_freq = 300,
|
||||
session_token = inbound_headers["X-Session-Token"]
|
||||
)
|
||||
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"]
|
||||
)
|
||||
|
||||
# ┳┓
|
||||
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
|
||||
@@ -244,12 +189,9 @@ async def request_oauth_authorization_url(
|
||||
|
||||
# Done here:
|
||||
return ResponseModel(
|
||||
status_code = StatusCodes.OK if token_id else StatusCodes.FAILED,
|
||||
http_code = HttpCodes.SUCCESS if token_id else HttpCodes.INTERNAL_SERVER_ERROR,
|
||||
data = {
|
||||
"smsClient": inbound_data.smsClient,
|
||||
"authorized": True
|
||||
}
|
||||
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}"
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ from models.data.core.user_info import CoreUserInfoModel
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
async def get_session(session_token):
|
||||
async def get_session(session_token) -> CoreUserInfoModel:
|
||||
|
||||
"""
|
||||
Gets the session's info from the session token.
|
||||
|
||||
+13
-2
@@ -72,9 +72,10 @@ from utils_v2.goog.gmail.gmail_client import AsyncGMailClient
|
||||
|
||||
# Behaviour Models:
|
||||
from models.behaviour.mail.oauth_v3 import MailOAuthModel
|
||||
from models.behaviour.mail.sync_v2 import MailSyncModel
|
||||
from models.behaviour.mail.sync_v3 import MailSyncModel
|
||||
from models.behaviour.mail.retrieve import MailRetrieveModel
|
||||
from models.behaviour.sms.auth import SMSAuthModel
|
||||
from models.behaviour.sms.auth_v2 import SMSAuthModel
|
||||
from models.behaviour.sms.send import SMSSendModel
|
||||
from models.behaviour.ai.llm.open_ai import LLMOpenAI
|
||||
|
||||
# To make REST API calls:
|
||||
@@ -90,6 +91,7 @@ from api.blueprints.mail.sync import mail_sync_bp
|
||||
from api.blueprints.mail.list import mail_list_bp
|
||||
from api.blueprints.mail.retrieve import mail_retrieve_bp
|
||||
from api.blueprints.sms.auth import sms_auth_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.tech.chat_alerts import tech_chat_alert_bp
|
||||
@@ -128,6 +130,7 @@ app.register_blueprint(mail_sync_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
app.register_blueprint(mail_list_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
app.register_blueprint(mail_retrieve_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(chat_auth_bp, url_prefix = f"/{MODULE_BASE}/chat")
|
||||
app.register_blueprint(chat_webhook_bp, url_prefix = f"/{MODULE_BASE}/chat")
|
||||
app.register_blueprint(tech_chat_alert_bp, url_prefix = f"/{MODULE_BASE}/tech/alert")
|
||||
@@ -352,6 +355,14 @@ async def app_startup(**kwargs):
|
||||
debug_prefix = "SMS-Auth | ",
|
||||
debug_only_errors = True
|
||||
)
|
||||
current_app.sms_send_model = SMSSendModel(
|
||||
cache = current_app.module_cache,
|
||||
alert_url = current_app.script_data["alerts"]["url"],
|
||||
http_client = current_app.http_client,
|
||||
debug = enable_debugging,
|
||||
debug_prefix = "SMS-Send | ",
|
||||
debug_only_errors = True
|
||||
)
|
||||
|
||||
current_app.printer("Internal models ready.")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user