swarmauri_tool_gmail 0.6.0.dev154__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- swarmauri_tool_gmail/GmailReadTool.py +99 -0
- swarmauri_tool_gmail/GmailSendTool.py +104 -0
- swarmauri_tool_gmail/__init__.py +17 -0
- swarmauri_tool_gmail-0.6.0.dev154.dist-info/METADATA +19 -0
- swarmauri_tool_gmail-0.6.0.dev154.dist-info/RECORD +7 -0
- swarmauri_tool_gmail-0.6.0.dev154.dist-info/WHEEL +4 -0
- swarmauri_tool_gmail-0.6.0.dev154.dist-info/entry_points.txt +4 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
from google.oauth2 import service_account
|
|
2
|
+
from googleapiclient.discovery import build
|
|
3
|
+
from swarmauri_core.ComponentBase import ComponentBase
|
|
4
|
+
from swarmauri_base.tools.ToolBase import ToolBase
|
|
5
|
+
from swarmauri_standard.tools.Parameter import Parameter
|
|
6
|
+
from typing import List, Literal, Dict, Optional
|
|
7
|
+
from pydantic import Field
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@ComponentBase.register_type(ToolBase, "GmailReadTool")
|
|
11
|
+
class GmailReadTool(ToolBase):
|
|
12
|
+
SCOPES: List[str] = ["https://www.googleapis.com/auth/gmail.readonly"]
|
|
13
|
+
version: str = "1.0.0"
|
|
14
|
+
parameters: List[Parameter] = Field(
|
|
15
|
+
default_factory=lambda: [
|
|
16
|
+
Parameter(
|
|
17
|
+
name="query",
|
|
18
|
+
type="string",
|
|
19
|
+
description="""The query to filter emails. For example, "is:unread" or "from:example@gmail.com".""",
|
|
20
|
+
required=True,
|
|
21
|
+
),
|
|
22
|
+
Parameter(
|
|
23
|
+
name="max_results",
|
|
24
|
+
type="integer",
|
|
25
|
+
description="""The maximum number of emails to return. Defaults to 10.""",
|
|
26
|
+
),
|
|
27
|
+
]
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
name: str = "GmailReadTool"
|
|
31
|
+
description: str = "Read emails from a Gmail account."
|
|
32
|
+
type: Literal["GmailReadTool"] = "GmailReadTool"
|
|
33
|
+
credentials_path: str
|
|
34
|
+
sender_email: str
|
|
35
|
+
service: Optional[object] = None
|
|
36
|
+
|
|
37
|
+
def authenticate(self):
|
|
38
|
+
"""
|
|
39
|
+
Authenticates the user and creates a Gmail API service.
|
|
40
|
+
"""
|
|
41
|
+
credentials = service_account.Credentials.from_service_account_file(
|
|
42
|
+
self.credentials_path, scopes=self.SCOPES
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
delegated_credentials = credentials.with_subject(self.sender_email)
|
|
46
|
+
self.service = build("gmail", "v1", credentials=delegated_credentials)
|
|
47
|
+
|
|
48
|
+
def __call__(self, query: str = "", max_results: int = 10) -> Dict[str, str]:
|
|
49
|
+
"""
|
|
50
|
+
Fetches emails from the authenticated Gmail account based on the given query.
|
|
51
|
+
|
|
52
|
+
Parameters:
|
|
53
|
+
query (str): The query to filter emails. For example, "is:unread".
|
|
54
|
+
max_results (int): The maximum number of email messages to fetch.
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
Dict[str, str]: A dictionary containing the email messages.
|
|
58
|
+
"""
|
|
59
|
+
try:
|
|
60
|
+
self.authenticate()
|
|
61
|
+
# Call the Gmail API
|
|
62
|
+
gmail_messages = self.service.users().messages()
|
|
63
|
+
results = gmail_messages.list(
|
|
64
|
+
userId="me", q=query, maxResults=max_results
|
|
65
|
+
).execute()
|
|
66
|
+
messages = results.get("messages", [])
|
|
67
|
+
message_data = ""
|
|
68
|
+
for message in messages:
|
|
69
|
+
msg = gmail_messages.get(
|
|
70
|
+
userId="me", id=message["id"], format="full"
|
|
71
|
+
).execute()
|
|
72
|
+
headers = msg["payload"]["headers"]
|
|
73
|
+
|
|
74
|
+
sender = next(
|
|
75
|
+
header["value"] for header in headers if header["name"] == "From"
|
|
76
|
+
)
|
|
77
|
+
subject = next(
|
|
78
|
+
header["value"] for header in headers if header["name"] == "Subject"
|
|
79
|
+
)
|
|
80
|
+
reply_to = next(
|
|
81
|
+
(
|
|
82
|
+
header["value"]
|
|
83
|
+
for header in headers
|
|
84
|
+
if header["name"] == "Reply-To"
|
|
85
|
+
),
|
|
86
|
+
subject,
|
|
87
|
+
)
|
|
88
|
+
date_time = next(
|
|
89
|
+
header["value"] for header in headers if header["name"] == "Date"
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
formatted_msg = f"\nSender: {sender}\nReply-To: {reply_to}\nSubject: {subject}\nDate: {date_time}\n"
|
|
93
|
+
message_data += formatted_msg
|
|
94
|
+
|
|
95
|
+
return {"gmail_messages": message_data}
|
|
96
|
+
except Exception as e:
|
|
97
|
+
return f"An error occurred: {str(e)}"
|
|
98
|
+
finally:
|
|
99
|
+
del self.service
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
from email.mime.multipart import MIMEMultipart
|
|
3
|
+
from email.mime.text import MIMEText
|
|
4
|
+
from google.oauth2 import service_account
|
|
5
|
+
from googleapiclient.discovery import build, Resource
|
|
6
|
+
from swarmauri_core.ComponentBase import ComponentBase
|
|
7
|
+
from swarmauri_base.tools.ToolBase import ToolBase
|
|
8
|
+
from swarmauri_standard.tools.Parameter import Parameter
|
|
9
|
+
from typing import List, Dict, Literal
|
|
10
|
+
from pydantic import Field
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@ComponentBase.register_type(ToolBase, "GmailSendTool")
|
|
14
|
+
class GmailSendTool(ToolBase):
|
|
15
|
+
SCOPES: List[str] = ["https://www.googleapis.com/auth/gmail.send"]
|
|
16
|
+
|
|
17
|
+
version: str = "1.0.0"
|
|
18
|
+
parameters: List[Parameter] = Field(
|
|
19
|
+
default_factory=lambda: [
|
|
20
|
+
Parameter(
|
|
21
|
+
name="recipients",
|
|
22
|
+
type="string",
|
|
23
|
+
description="The email addresses of the recipients, separated by commas",
|
|
24
|
+
required=True,
|
|
25
|
+
),
|
|
26
|
+
Parameter(
|
|
27
|
+
name="subject",
|
|
28
|
+
type="string",
|
|
29
|
+
description="The subject of the email",
|
|
30
|
+
required=True,
|
|
31
|
+
),
|
|
32
|
+
Parameter(
|
|
33
|
+
name="htmlMsg",
|
|
34
|
+
type="string",
|
|
35
|
+
description="The HTML message to be sent as the email body",
|
|
36
|
+
required=True,
|
|
37
|
+
),
|
|
38
|
+
]
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
name: str = "GmailSendTool"
|
|
42
|
+
description: str = "Sends an email using the Gmail API."
|
|
43
|
+
type: Literal["GmailSendTool"] = "GmailSendTool"
|
|
44
|
+
|
|
45
|
+
credentials_path: str
|
|
46
|
+
sender_email: str
|
|
47
|
+
|
|
48
|
+
def authenticate(self) -> Resource:
|
|
49
|
+
"""
|
|
50
|
+
Authenticates the user and creates a Gmail API service for sending emails.
|
|
51
|
+
This method returns the service instance, without saving it as an attribute.
|
|
52
|
+
"""
|
|
53
|
+
credentials = service_account.Credentials.from_service_account_file(
|
|
54
|
+
self.credentials_path, scopes=self.SCOPES
|
|
55
|
+
)
|
|
56
|
+
delegated_credentials = credentials.with_subject(self.sender_email)
|
|
57
|
+
return build("gmail", "v1", credentials=delegated_credentials)
|
|
58
|
+
|
|
59
|
+
def create_message(
|
|
60
|
+
self, to: str, subject: str, message_text: str
|
|
61
|
+
) -> Dict[str, str]:
|
|
62
|
+
"""
|
|
63
|
+
Create a MIMEText message for sending an email.
|
|
64
|
+
|
|
65
|
+
Parameters:
|
|
66
|
+
to (str): The email address of the recipient.
|
|
67
|
+
subject (str): The subject of the email.
|
|
68
|
+
message_text (str): The HTML body of the email.
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
Dict[str, str]: The created MIMEText message in a format suitable for Gmail API.
|
|
72
|
+
"""
|
|
73
|
+
message = MIMEMultipart("alternative")
|
|
74
|
+
message["from"] = self.sender_email
|
|
75
|
+
message["to"] = to
|
|
76
|
+
message["subject"] = subject
|
|
77
|
+
mime_text = MIMEText(message_text, "html")
|
|
78
|
+
message.attach(mime_text)
|
|
79
|
+
raw_message = base64.urlsafe_b64encode(
|
|
80
|
+
message.as_string().encode("utf-8")
|
|
81
|
+
).decode("utf-8")
|
|
82
|
+
return {"raw": raw_message}
|
|
83
|
+
|
|
84
|
+
def __call__(self, recipients: str, subject: str, htmlMsg: str) -> Dict[str, str]:
|
|
85
|
+
"""
|
|
86
|
+
Sends an email to the specified recipients with the given subject and HTML message.
|
|
87
|
+
|
|
88
|
+
Parameters:
|
|
89
|
+
recipients (str): The email address of the recipients, separated by commas.
|
|
90
|
+
subject (str): The subject of the email.
|
|
91
|
+
htmlMsg (str): The HTML content of the email body.
|
|
92
|
+
|
|
93
|
+
Returns:
|
|
94
|
+
Dict[str, str]: A message indicating the status of the email sending process.
|
|
95
|
+
"""
|
|
96
|
+
service = (
|
|
97
|
+
self.authenticate()
|
|
98
|
+
) # Authenticate within this function and do not store in the object state
|
|
99
|
+
try:
|
|
100
|
+
message = self.create_message(recipients, subject, htmlMsg)
|
|
101
|
+
service.users().messages().send(userId="me", body=message).execute()
|
|
102
|
+
return {"success": f"Email sent successfully to {recipients}"}
|
|
103
|
+
except Exception as e:
|
|
104
|
+
return {"error": f"An error occurred in sending the email: {str(e)}"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from .GmailReadTool import GmailReadTool
|
|
2
|
+
from .GmailSendTool import GmailSendTool
|
|
3
|
+
|
|
4
|
+
__version__ = "0.6.0.dev26"
|
|
5
|
+
__long_desc__ = """
|
|
6
|
+
|
|
7
|
+
# Swarmauri Gmail Plugin
|
|
8
|
+
|
|
9
|
+
Components Included:
|
|
10
|
+
- GmailSendTool
|
|
11
|
+
- GmailReadTool
|
|
12
|
+
|
|
13
|
+
Visit us at: https://swarmauri.com
|
|
14
|
+
Follow us at: https://github.com/swarmauri
|
|
15
|
+
Star us at: https://github.com/swarmauri/swarmauri-sdk
|
|
16
|
+
|
|
17
|
+
"""
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: swarmauri_tool_gmail
|
|
3
|
+
Version: 0.6.0.dev154
|
|
4
|
+
Summary: example community package
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Author: Jacob Stewart
|
|
7
|
+
Author-email: jacob@swarmauri.com
|
|
8
|
+
Requires-Python: >=3.10,<3.13
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Requires-Dist: swarmauri_base (>=0.6.0.dev154,<0.7.0)
|
|
15
|
+
Requires-Dist: swarmauri_core (>=0.6.0.dev154,<0.7.0)
|
|
16
|
+
Project-URL: Repository, http://github.com/swarmauri/swarmauri-sdk
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# Swarmauri Example Community Package
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
swarmauri_tool_gmail/__init__.py,sha256=OP2GmCi_2uvbC9XNyu_-ZB9IspXjiSviyPSelVv4e4A,367
|
|
2
|
+
swarmauri_tool_gmail/GmailReadTool.py,sha256=jtBufJMhZEmhPv27mEvPhcwBQyJUTnk09TjnnaqWupk,3843
|
|
3
|
+
swarmauri_tool_gmail/GmailSendTool.py,sha256=S1nv5XQKDidPlWIs_90twBspTBcFeTU2ygBx3TT17m8,4042
|
|
4
|
+
swarmauri_tool_gmail-0.6.0.dev154.dist-info/entry_points.txt,sha256=A_9u_TVdgpcYPrcqK314-8Uj2_9qmdvnORyC0R0HYDc,145
|
|
5
|
+
swarmauri_tool_gmail-0.6.0.dev154.dist-info/METADATA,sha256=aJU3_vmQfeDgtveu2ywA1ooeiso4y-Gme2guC7vVYcU,730
|
|
6
|
+
swarmauri_tool_gmail-0.6.0.dev154.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
|
|
7
|
+
swarmauri_tool_gmail-0.6.0.dev154.dist-info/RECORD,,
|