(20250122) Work reminder cron progressing well.
This commit is contained in:
@@ -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,
|
||||||
|
|||||||
+116
-48
@@ -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,6 +404,64 @@ async def send_reminders_by_chat(chat_df: pd.DataFrame) -> None:
|
|||||||
# ---------------------------------------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
async def send_one_reminder_by_mail(reminder_row) -> None:
|
||||||
|
|
||||||
|
# Start with blank variables:
|
||||||
|
log_id = None
|
||||||
|
api_message = None
|
||||||
|
|
||||||
|
# Create the input(s) needed for making the API call:
|
||||||
|
input_json = {
|
||||||
|
"tokenKey": reminder_row["tokenKey"],
|
||||||
|
"to": [reminder_row["to"]],
|
||||||
|
"subject": f"Reminder for Work - {datetime.datetime.now().strftime('%d/%m/%Y')}",
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "html",
|
||||||
|
"part": {
|
||||||
|
"content": reminder_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_mail(mail_df: pd.DataFrame) -> None:
|
async def send_reminders_by_mail(mail_df: pd.DataFrame) -> None:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@@ -406,54 +470,58 @@ async def send_reminders_by_mail(mail_df: pd.DataFrame) -> None:
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Start with blank variables:
|
# Iterate over all the rows and create mail-sending tasks:
|
||||||
log_id = None
|
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():
|
||||||
|
|
||||||
# Iterate over all the rows and send out the mails:
|
# # Create the input(s) needed for making the API call:
|
||||||
for index, row in mail_df.iterrows():
|
# input_json = {
|
||||||
|
# "tokenKey": row["tokenKey"],
|
||||||
# Create the input(s) needed for making the API call:
|
# "to": [row["to"]],
|
||||||
input_json = {
|
# "subject": f"Reminder for Work - {datetime.datetime.now().strftime('%d/%m/%Y')}",
|
||||||
"tokenKey": row["tokenKey"],
|
# "body": [
|
||||||
"to": [row["to"]],
|
# {
|
||||||
"subject": f"Reminder for Work - {datetime.datetime.now().strftime('%d/%m/%Y')}",
|
# "type": "html",
|
||||||
"body": [
|
# "part": {
|
||||||
{
|
# "content": row["content"]
|
||||||
"type": "html",
|
# }
|
||||||
"part": {
|
# }
|
||||||
"content": row["content"]
|
# ]
|
||||||
}
|
# }
|
||||||
}
|
#
|
||||||
]
|
# # We are now ready to make the API call:
|
||||||
}
|
# exception = None
|
||||||
|
# try:
|
||||||
# We are now ready to make the API call:
|
#
|
||||||
exception = None
|
# # Make the API call:
|
||||||
try:
|
# response = await http_client.post(
|
||||||
|
# url = r"https://api.thecaoffice.com/converse/mail",
|
||||||
# Make the API call:
|
# json = input_json
|
||||||
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"]
|
||||||
# Get the log id from the response:
|
#
|
||||||
log_id = response.json()["logId"]
|
# # If the API call failed:
|
||||||
|
# response.raise_for_status()
|
||||||
# If the API call failed:
|
#
|
||||||
response.raise_for_status()
|
# # If the API call goes wrong:
|
||||||
|
# except Exception as e:
|
||||||
# If the API call goes wrong:
|
# exception = e
|
||||||
except Exception as e:
|
# printer(exception)
|
||||||
exception = e
|
#
|
||||||
printer(exception)
|
# # Send out an alert if needed:
|
||||||
|
# if exception is not None:
|
||||||
# Send out an alert if needed:
|
# await send_telegram(
|
||||||
if exception is not None:
|
# message = (
|
||||||
await send_telegram(
|
# f"*Reminders For Work (Mail)*\n\nException: `{exception}`\n\n"
|
||||||
message = f"*Reminders For Work (Mail)*\n\nException: `{exception}`\n\nLog Id: `{log_id}`",
|
# f"Message: `{api_message}`\n\n"
|
||||||
message_type = "error"
|
# f"Log Id: `{log_id}`"
|
||||||
)
|
# ),
|
||||||
|
# message_type = "error"
|
||||||
|
# )
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user