(20241204) Updated Gmail client and Nimbus client.

This commit is contained in:
2024-12-04 13:03:51 +05:30
parent 293f753381
commit e51a6de5fc
4 changed files with 28 additions and 63 deletions
+16 -60
View File
@@ -140,6 +140,7 @@ class AsyncGMailClient(AsyncGoogleBase):
DOCUMENTATION:
1. https://developers.google.com/gmail/api/reference/rest/v1/users/getProfile
2. https://developers.google.com/people/api/rest/v1/people/get
3. https://developers.google.com/people/api/rest/v1/people#Person
:param tokens: The object that holds the access token to the service.
:param user_id: The id of the user for which this service must be run. Typically, the e-mail id itself or "me".
:return: A structured response where the list of labels will be in the 'data' variable.
@@ -164,11 +165,13 @@ class AsyncGMailClient(AsyncGoogleBase):
if gmail_api_response.httpCode in [200]:
gmail_api_response.success = True
gmail_api_response.data = await gmail_api_response.get_json()
gmail_api_response.data["displayName"] = None
gmail_api_response.data["displayPictureUrl"] = None
# Make the People API call:
if not self._debug_only_errors: self._printer("Getting User Profile.")
people_api_response = await self.get(
url = f"https://people.googleapis.com/v1/people/me",
url = f"https://people.googleapis.com/v1/people/me?personFields=names,photos,birthdays,phoneNumbers,genders,emailAddresses,addresses",
headers = {"Authorization": f"Bearer {tokens.accessToken}"}
)
@@ -176,10 +179,15 @@ class AsyncGMailClient(AsyncGoogleBase):
if people_api_response.httpCode in [200]:
people_api_response.success = True
people_api_response.data = await people_api_response.get_json()
for k, v in gmail_api_response.data.items(): people_api_response.data[k] = v
for item in people_api_response.data.get("names", []):
if item["metadata"]["primary"]:
gmail_api_response.data["displayName"] = item.get("displayName")
for item in people_api_response.data.get("photos", []):
if item["metadata"]["primary"]:
gmail_api_response.data["displayPictureUrl"] = item.get("url")
# Done here:
return people_api_response
return gmail_api_response
# ┓ ┓ ┓
# ┃ ┏┓┣┓┏┓┃┏
@@ -911,11 +919,6 @@ if __name__ == "__main__":
secrets_file = r"../../../creds/goog/app/google_tcaoff_test_oauth_20241125.json"
secrets_dict = json.from_file(secrets_file)
# Read the user's tokens that will give you app access to that user's account:
tokens_file = r"../../../creds/goog/user/test_user_tokens_20241125.json"
tokens_dict = json.from_file(tokens_file)
test_tokens = GoogleAuthTokens(**tokens_dict)
async def main():
# Create an instance of the client:
@@ -937,61 +940,14 @@ if __name__ == "__main__":
))
# Get tokens from callback:
# print("TOKENS:", await my_gmail.get_authorization_tokens(
# redirect_url = r"https://api.thecaoffice.com/converse/mail/callback/gmail?state=User123-Bhopli..."
# ))
# Create a sample mail:
my_mail = GMailMessage(
from_email = "pskhushal@gmail.com",
to_email = "orangebhopli@gmail.com",
subject = "Bhopli is the best! (Parts Sequence Test)",
cc_emails = None,
bcc_emails = None
test_tokens = await my_gmail.get_authorization_tokens(
scopes = SCOPES_GMAIL_MAIL_MANAGEMENT,
redirect_url = input("Paste the redirect URL here: ")
)
my_mail.add_html(
"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sample HTML String</title>
<style>
.heading {
color: #ff9025;
}
.sub-heading {
color: #000000;
}
</style>
</head>
<body>
<h1 class="heading">Hello, Bhopli!</h1>
<h2 class="sub-heading">Bhopli is the best, most well-behaved cat in the known universe.</h2>
</body>
</html>
"""
)
my_mail.add_text("(3rd part - text) This is how you should pet her 👇")
my_mail.add_inline_image(r"../../../data/images/cat_petting.png")
my_mail.add_attachment(r"../../../data/pdf/sample_label.pdf")
# print(my_mail.get_raw_message(as_base64 = False))
print("TOKENS:", test_tokens)
# Test some feature:
# auth_url = await my_gmail.get_authorization_url(
# state = "1234567890",
# scopes = SCOPES_GMAIL_MAIL_MANAGEMENT,
# approval_prompt = "force",
# user_email = "pskhushal@gmail.com"
# )
# print("AUTH URL:", auth_url)
# response = await my_gmail.list_labels(
# tokens = test_tokens,
# )
# response = await my_gmail.get_message(
# tokens = test_tokens,
# message_id = "1936bfbfc912b86f"
# )
response = await my_gmail.get_user_profile(tokens = test_tokens)
print("SUCCESS:", response.success)
print("SUMMARY:", response.to_markdown())
print("\n\n---\n\n")
+2
View File
@@ -90,6 +90,8 @@ class GoogleAuthTokens(BaseModel):
expiresAt: AwareDatetime = Field(description = "the time (utc) at which the token will expire")
scopes: List[str] = Field(description = "the list of permissions", default = [])
email: str | None = Field(description = "the email id of the user", default = None)
displayName: str | None = Field(description = "the display name of the user", default = None)
displayPictureUrl: str | None = Field(description = "the url to the display picture of the user", default = None)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
View File
@@ -79,6 +79,9 @@ class AsyncNimbusSMS:
MESSAGE_TYPE_REGULAR = 0
MESSAGE_TYPE_UNICODE = 1
MESSAGE_TYPE_NOT_FLASH = 0
MESSAGE_TYPE_FLASH = 1
def __init__(
self,
entity_id,
@@ -147,7 +150,8 @@ class AsyncNimbusSMS:
template_id,
recipient_number,
message,
message_type = MESSAGE_TYPE_REGULAR
message_type = MESSAGE_TYPE_REGULAR,
flash = MESSAGE_TYPE_NOT_FLASH
):
"""
@@ -160,6 +164,7 @@ class AsyncNimbusSMS:
:param message: The message to send to the recipient. Should match the template that is being sent.
:param message_type: Choose between 'AsyncNimbusSMS.MESSAGE_TYPE_REGULAR' (default) and
'AsyncNimbusSMS.MESSAGE_TYPE_UNICODE' based on the type of characters being sent. Both are class variables.
:param flash: to choose between a regular inbox SMS, or a flash SMS.
:return: The dict of all the details of the message that was sent including whether, or not, it was successfully
sent. Other details depend on the service provider (Nimbus IT in this case).
"""
@@ -173,7 +178,8 @@ class AsyncNimbusSMS:
"message": message,
"length": len(message),
"template_id": template_id,
"raw": None
"raw": None,
"isFlash": True if flash else False
}
try:
@@ -192,7 +198,8 @@ class AsyncNimbusSMS:
"text": message,
"entityid": self.__entity_id,
"templateid": template_id,
"type": message_type
"type": message_type,
"flash": flash
}
)