jupiter0913 commited on
Commit ·
6f8d7cc
1
Parent(s): bd7ae30
feature(#98): implemented read emails api
Browse files
Brain/src/model/requests/request_model.py
CHANGED
|
@@ -215,3 +215,15 @@ class AutoTaskDelete(BasicReq):
|
|
| 215 |
reference_link: str
|
| 216 |
|
| 217 |
data: Body
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
reference_link: str
|
| 216 |
|
| 217 |
data: Body
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
"""endpoint /read_emails"""
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
class EmailReader(BasicReq):
|
| 224 |
+
class Body(BaseModel):
|
| 225 |
+
sender: str
|
| 226 |
+
pwd: str
|
| 227 |
+
imap_folder: str
|
| 228 |
+
|
| 229 |
+
data: Body
|
Brain/src/router/api.py
CHANGED
|
@@ -19,6 +19,7 @@ from Brain.src.model.requests.request_model import (
|
|
| 19 |
ClientInfo,
|
| 20 |
get_client_info,
|
| 21 |
AutoTaskDelete,
|
|
|
|
| 22 |
)
|
| 23 |
from Brain.src.rising_plugin.risingplugin import (
|
| 24 |
getCompletion,
|
|
@@ -27,6 +28,7 @@ from Brain.src.rising_plugin.risingplugin import (
|
|
| 27 |
)
|
| 28 |
from Brain.src.firebase.cloudmessage import CloudMessage
|
| 29 |
from Brain.src.rising_plugin.image_embedding import embed_image_text, query_image_text
|
|
|
|
| 30 |
|
| 31 |
from Brain.src.logs import logger
|
| 32 |
from Brain.src.model.basic_model import BasicModel
|
|
@@ -492,4 +494,60 @@ def construct_blueprint_api() -> APIRouter:
|
|
| 492 |
200, "Deleted data from real-time database of firebase", ""
|
| 493 |
)
|
| 494 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
return router
|
|
|
|
| 19 |
ClientInfo,
|
| 20 |
get_client_info,
|
| 21 |
AutoTaskDelete,
|
| 22 |
+
EmailReader,
|
| 23 |
)
|
| 24 |
from Brain.src.rising_plugin.risingplugin import (
|
| 25 |
getCompletion,
|
|
|
|
| 28 |
)
|
| 29 |
from Brain.src.firebase.cloudmessage import CloudMessage
|
| 30 |
from Brain.src.rising_plugin.image_embedding import embed_image_text, query_image_text
|
| 31 |
+
from Brain.src.rising_plugin.gmail.manage_gmail import EmailManager
|
| 32 |
|
| 33 |
from Brain.src.logs import logger
|
| 34 |
from Brain.src.model.basic_model import BasicModel
|
|
|
|
| 494 |
200, "Deleted data from real-time database of firebase", ""
|
| 495 |
)
|
| 496 |
|
| 497 |
+
"""@generator.request_body(
|
| 498 |
+
{
|
| 499 |
+
"token": "String",
|
| 500 |
+
"uuid": "String",
|
| 501 |
+
"data": {
|
| 502 |
+
"sender": "test@gmail.com",
|
| 503 |
+
"pwd": "use app password of your google account",
|
| 504 |
+
"imap_folder": "inbox or drafts"
|
| 505 |
+
},
|
| 506 |
+
}
|
| 507 |
+
)
|
| 508 |
+
|
| 509 |
+
@generator.response(
|
| 510 |
+
status_code=200, schema={"message": "message", "result": [{
|
| 511 |
+
"From": "testfrom@test.com",
|
| 512 |
+
"To": "test@gmail.com",
|
| 513 |
+
"Date": "Tue, 04 Jul 2023 12:55:19 +0000",
|
| 514 |
+
"CC": "",
|
| 515 |
+
"Subject": "subject",
|
| 516 |
+
"Message Body": "message"
|
| 517 |
+
}]}
|
| 518 |
+
)
|
| 519 |
+
|
| 520 |
+
"""
|
| 521 |
+
|
| 522 |
+
@router.post("/read_emails")
|
| 523 |
+
def read_emails(data: EmailReader):
|
| 524 |
+
# firebase admin init
|
| 525 |
+
try:
|
| 526 |
+
setting, firebase_app = firebase_admin_with_setting(data)
|
| 527 |
+
except BrainException as ex:
|
| 528 |
+
return assembler.to_response(ex.code, ex.message, "")
|
| 529 |
+
try:
|
| 530 |
+
token = setting.token
|
| 531 |
+
uuid = setting.uuid
|
| 532 |
+
|
| 533 |
+
# if imap_folder is drafts, then search is ALL
|
| 534 |
+
imap_search_cmd = "UNSEEN"
|
| 535 |
+
if data.data.imap_folder not in "inbox":
|
| 536 |
+
imap_search_cmd = "(ALL)"
|
| 537 |
+
|
| 538 |
+
# read emails
|
| 539 |
+
email_manager = EmailManager()
|
| 540 |
+
result = email_manager.read_emails(
|
| 541 |
+
sender=data.data.sender,
|
| 542 |
+
pwd=data.data.pwd,
|
| 543 |
+
imap_folder=data.data.imap_folder,
|
| 544 |
+
imap_search_command=imap_search_cmd,
|
| 545 |
+
)
|
| 546 |
+
result = json.loads(result)
|
| 547 |
+
except Exception as e:
|
| 548 |
+
if isinstance(e, BrainException):
|
| 549 |
+
return e.get_response_exp()
|
| 550 |
+
return assembler.to_response(400, "Failed to read emails", "")
|
| 551 |
+
return assembler.to_response(200, "", result)
|
| 552 |
+
|
| 553 |
return router
|