(20250122) Work reminder cron progressing well.

This commit is contained in:
2025-01-22 16:54:30 +05:30
parent 477dd66452
commit ff037e596e
2 changed files with 129 additions and 48 deletions
+13
View File
@@ -42,6 +42,7 @@ from utils_v2.date_time import date_time
from utils_v2.database.async_mysql_v2 import AsyncMySQL from utils_v2.database.async_mysql_v2 import AsyncMySQL
from utils_v2.database.async_mongo_v2 import AsyncMongo from utils_v2.database.async_mongo_v2 import AsyncMongo
from utils_v2.cache.async_redis_cache_v2 import AsyncRedisCache from utils_v2.cache.async_redis_cache_v2 import AsyncRedisCache
from utils_v2.logging.context import AsyncLoggerContext
# Controllers: # Controllers:
from controllers_v2.message.mail.base import MailController from controllers_v2.message.mail.base import MailController
@@ -63,6 +64,9 @@ from utils_v2.goog.controllers.gmail.gmail_client import AsyncGmailClient, SCOPE
from utils_v2.goog.controllers.gmail.gmail_message import GmailMessage from utils_v2.goog.controllers.gmail.gmail_message import GmailMessage
from utils_v2.goog.models.auth_tokens import GoogleAuthTokens from utils_v2.goog.models.auth_tokens import GoogleAuthTokens
# Shared:
from shared import constants
# To work with datatypes: # To work with datatypes:
from typing import List, Any from typing import List, Any
@@ -367,6 +371,15 @@ class GmailController(MailController):
# Done here: # Done here:
return response return response
@AsyncLoggerContext.log_it(
api_version = "1.0.0",
project = constants.PROJECT_NAME,
log_type = constants.MODULE_NAME,
operation = "gmailTokRefresh",
log_input = False,
log_output = False,
sensitive_keys = ["session_token"]
)
async def refresh_authorization( async def refresh_authorization(
self, self,
sql_conn: AsyncMySQL, sql_conn: AsyncMySQL,
+83 -15
View File
@@ -346,6 +346,7 @@ async def send_reminders_by_chat(chat_df: pd.DataFrame) -> None:
# Start with blank variables: # Start with blank variables:
log_id = None log_id = None
api_message = None
# Get a list of unique token-keys: # Get a list of unique token-keys:
unique_token_keys = chat_df["tokenKey"].unique().tolist() unique_token_keys = chat_df["tokenKey"].unique().tolist()
@@ -378,6 +379,7 @@ async def send_reminders_by_chat(chat_df: pd.DataFrame) -> None:
# Get the log id from the response: # Get the log id from the response:
log_id = response.json()["logId"] log_id = response.json()["logId"]
api_message = response.json()["message"]
# If the API call failed: # If the API call failed:
response.raise_for_status() response.raise_for_status()
@@ -390,7 +392,11 @@ async def send_reminders_by_chat(chat_df: pd.DataFrame) -> None:
# Send out an alert if needed: # Send out an alert if needed:
if exception is not None: if exception is not None:
await send_telegram( await send_telegram(
message = f"*Reminders For Work (Chat)*\n\nException: `{exception}`\n\nLog Id: `{log_id}`", message = (
f"*Reminders For Work (Chat)*\n\nException: `{exception}`\n\n"
f"Message: `{api_message}`\n\n"
f"Log Id: `{log_id}`"
),
message_type = "error" message_type = "error"
) )
@@ -398,30 +404,22 @@ async def send_reminders_by_chat(chat_df: pd.DataFrame) -> None:
# --------------------------------------------------------------------------------------------------------------------- # ---------------------------------------------------------------------------------------------------------------------
async def send_reminders_by_mail(mail_df: pd.DataFrame) -> None: async def send_one_reminder_by_mail(reminder_row) -> None:
"""
To send out reminders by e-mail.
:param mail_df: The part of the tabulated data that has reminders that need to be sent over e-mail.
:return: None
"""
# Start with blank variables: # Start with blank variables:
log_id = None log_id = None
api_message = None
# Iterate over all the rows and send out the mails:
for index, row in mail_df.iterrows():
# Create the input(s) needed for making the API call: # Create the input(s) needed for making the API call:
input_json = { input_json = {
"tokenKey": row["tokenKey"], "tokenKey": reminder_row["tokenKey"],
"to": [row["to"]], "to": [reminder_row["to"]],
"subject": f"Reminder for Work - {datetime.datetime.now().strftime('%d/%m/%Y')}", "subject": f"Reminder for Work - {datetime.datetime.now().strftime('%d/%m/%Y')}",
"body": [ "body": [
{ {
"type": "html", "type": "html",
"part": { "part": {
"content": row["content"] "content": reminder_row["content"]
} }
} }
] ]
@@ -439,6 +437,7 @@ async def send_reminders_by_mail(mail_df: pd.DataFrame) -> None:
# Get the log id from the response: # Get the log id from the response:
log_id = response.json()["logId"] log_id = response.json()["logId"]
api_message = response.json()["message"]
# If the API call failed: # If the API call failed:
response.raise_for_status() response.raise_for_status()
@@ -451,7 +450,11 @@ async def send_reminders_by_mail(mail_df: pd.DataFrame) -> None:
# Send out an alert if needed: # Send out an alert if needed:
if exception is not None: if exception is not None:
await send_telegram( await send_telegram(
message = f"*Reminders For Work (Mail)*\n\nException: `{exception}`\n\nLog Id: `{log_id}`", message = (
f"*Reminders For Work (Mail)*\n\nException: `{exception}`\n\n"
f"Message: `{api_message}`\n\n"
f"Log Id: `{log_id}`"
),
message_type = "error" message_type = "error"
) )
@@ -459,6 +462,71 @@ async def send_reminders_by_mail(mail_df: pd.DataFrame) -> None:
# --------------------------------------------------------------------------------------------------------------------- # ---------------------------------------------------------------------------------------------------------------------
async def send_reminders_by_mail(mail_df: pd.DataFrame) -> None:
"""
To send out reminders by e-mail.
:param mail_df: The part of the tabulated data that has reminders that need to be sent over e-mail.
:return: None
"""
# Iterate over all the rows and create mail-sending tasks:
tasks = [send_one_reminder_by_mail(row) for index, row in mail_df.iterrows()]
await asyncio.gather(*tasks)
# for index, row in mail_df.iterrows():
# # Create the input(s) needed for making the API call:
# input_json = {
# "tokenKey": row["tokenKey"],
# "to": [row["to"]],
# "subject": f"Reminder for Work - {datetime.datetime.now().strftime('%d/%m/%Y')}",
# "body": [
# {
# "type": "html",
# "part": {
# "content": row["content"]
# }
# }
# ]
# }
#
# # We are now ready to make the API call:
# exception = None
# try:
#
# # Make the API call:
# response = await http_client.post(
# url = r"https://api.thecaoffice.com/converse/mail",
# json = input_json
# )
#
# # Get the log id from the response:
# log_id = response.json()["logId"]
# api_message = response.json()["message"]
#
# # If the API call failed:
# response.raise_for_status()
#
# # If the API call goes wrong:
# except Exception as e:
# exception = e
# printer(exception)
#
# # Send out an alert if needed:
# if exception is not None:
# await send_telegram(
# message = (
# f"*Reminders For Work (Mail)*\n\nException: `{exception}`\n\n"
# f"Message: `{api_message}`\n\n"
# f"Log Id: `{log_id}`"
# ),
# message_type = "error"
# )
# ---------------------------------------------------------------------------------------------------------------------
async def send_reminders_by_sms(chat_df: pd.DataFrame): async def send_reminders_by_sms(chat_df: pd.DataFrame):
print("SMS DF:", chat_df.to_string()) print("SMS DF:", chat_df.to_string())