jupiter0913 commited on
Commit
984d010
·
1 Parent(s): b78efaf

feature(#98): implemented send email...

Browse files
Brain/src/model/requests/request_model.py CHANGED
@@ -229,6 +229,23 @@ class EmailReader(BasicReq):
229
  data: Body
230
 
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  """endpoint : /contact/get_by_ids"""
233
 
234
 
 
229
  data: Body
230
 
231
 
232
+ """endpoint /send_email"""
233
+
234
+
235
+ class EmailSender(BasicReq):
236
+ class Body(BaseModel):
237
+ sender: str
238
+ pwd: str
239
+ to: str
240
+ subject: str
241
+ body: str
242
+ to_send: bool
243
+ filename: str | None
244
+ file_content: str | None
245
+
246
+ data: Body
247
+
248
+
249
  """endpoint : /contact/get_by_ids"""
250
 
251
 
Brain/src/rising_plugin/llm/llms.py CHANGED
@@ -43,7 +43,7 @@ If user said that ask about the content in website, for example, if users ask so
43
  If user said that open a website using web browsers, please answer belowing json format. The url user is going to open can exist or not. If user doesn\\'t say exact url and want to open some sites, you have to find the best proper url. If user didn\\'t say any url and you can't find proper url, please set website url to "https://www.google.com". {"program": "browser", "url": "website url that user is going to open"}
44
  If users are going to ask something based on the data of website, please answer belowing json format. {"program": "askwebsite", "content": ""}
45
  If user is going to set or create alarm with time and label, please answer belowing json format.\n {"program": "alarm", "content": {"type":"create", "time":"please set time as 24-hours format that user is going to set. If user did not provide any alarm time, set "0:0"", "label":"please set label that user is going to set. If user did not provide any label, set "alarm""}}\n This is example data.\n User: Set an alarm.\n AI: {"program":"alarm", "content": {"type":"create", "time":"0:0", "label":"alarm"}}\n User: Set an alarm with label as "wake up".\n AI: {"program":"alarm", "content": {"type":"create", "time":"0:0", "label":"wake up"}}\n User: Set an alarm for 5:00 AM.\n AI: {"program":"alarm", "content": {"type":"create", "time":"5:00", "label":"alarm"}}\n User: Set an alarm for 5:00 PM with label as "wake up".\n AI: {"program":"alarm", "content": {"type":"create", "time":"17:00", "label":"wake up"}}
46
- If user is going to read email or message, please answer belowing json format. {"program": "reademail", "content": ""}
47
  If user is going to send email or message, please answer belowing json format. {"program": "sendemail", "content": ""}
48
  If user is going to compose, write, or create an email, please answer belowing json format. {"program": "writeemail", "content": ""}
49
  User wants to help organization in achieving its goals, which bridges the gap between the user is (present) and where he/she wants to go (future), or that is deciding in advance what to do, how to do when to do it and by whom it is to be done. Also the description ensures in thoughts and actions, work is carried on smoothly without any confusion and misunderstanding. \n And it can be done for the future and the future is full of uncertainties. \n So it looks like to make a decision as well. It helps make rational decisions by choosing the best most profitable alternative which may bring lower cost, adaptable to the organization and situations. \n If user is going to say about planning strategy, or something like that , please answer belowing json format. {"program": "autotask", "content": ""}
 
43
  If user said that open a website using web browsers, please answer belowing json format. The url user is going to open can exist or not. If user doesn\\'t say exact url and want to open some sites, you have to find the best proper url. If user didn\\'t say any url and you can't find proper url, please set website url to "https://www.google.com". {"program": "browser", "url": "website url that user is going to open"}
44
  If users are going to ask something based on the data of website, please answer belowing json format. {"program": "askwebsite", "content": ""}
45
  If user is going to set or create alarm with time and label, please answer belowing json format.\n {"program": "alarm", "content": {"type":"create", "time":"please set time as 24-hours format that user is going to set. If user did not provide any alarm time, set "0:0"", "label":"please set label that user is going to set. If user did not provide any label, set "alarm""}}\n This is example data.\n User: Set an alarm.\n AI: {"program":"alarm", "content": {"type":"create", "time":"0:0", "label":"alarm"}}\n User: Set an alarm with label as "wake up".\n AI: {"program":"alarm", "content": {"type":"create", "time":"0:0", "label":"wake up"}}\n User: Set an alarm for 5:00 AM.\n AI: {"program":"alarm", "content": {"type":"create", "time":"5:00", "label":"alarm"}}\n User: Set an alarm for 5:00 PM with label as "wake up".\n AI: {"program":"alarm", "content": {"type":"create", "time":"17:00", "label":"wake up"}}
46
+ If user is going to read emails, please answer belowing json format. {"program": "if user provided his gmail address and password, set to reademail, if not, set to reademaildetailinfo", "content": {"email": "email address that users sent", pwd: "password that users sent", type: "if user is going to read email inbox, set to inbox, if not, set to draft. if user did not provide where to read email, set to inbox"}}
47
  If user is going to send email or message, please answer belowing json format. {"program": "sendemail", "content": ""}
48
  If user is going to compose, write, or create an email, please answer belowing json format. {"program": "writeemail", "content": ""}
49
  User wants to help organization in achieving its goals, which bridges the gap between the user is (present) and where he/she wants to go (future), or that is deciding in advance what to do, how to do when to do it and by whom it is to be done. Also the description ensures in thoughts and actions, work is carried on smoothly without any confusion and misunderstanding. \n And it can be done for the future and the future is full of uncertainties. \n So it looks like to make a decision as well. It helps make rational decisions by choosing the best most profitable alternative which may bring lower cost, adaptable to the organization and situations. \n If user is going to say about planning strategy, or something like that , please answer belowing json format. {"program": "autotask", "content": ""}
Brain/src/router/email_router.py CHANGED
@@ -4,9 +4,7 @@ from fastapi import APIRouter
4
 
5
  from Brain.src.common.assembler import Assembler
6
  from Brain.src.common.brain_exception import BrainException
7
- from Brain.src.model.requests.request_model import (
8
- EmailReader,
9
- )
10
  from Brain.src.rising_plugin.gmail.email_plugin import EmailPlugin
11
  from Brain.src.firebase.firebase import firebase_admin_with_setting
12
 
@@ -74,4 +72,66 @@ def construct_blueprint_email_api() -> APIRouter:
74
  return assembler.to_response(400, "Failed to read emails", "")
75
  return assembler.to_response(200, "", result)
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  return router
 
4
 
5
  from Brain.src.common.assembler import Assembler
6
  from Brain.src.common.brain_exception import BrainException
7
+ from Brain.src.model.requests.request_model import EmailReader, EmailSender
 
 
8
  from Brain.src.rising_plugin.gmail.email_plugin import EmailPlugin
9
  from Brain.src.firebase.firebase import firebase_admin_with_setting
10
 
 
72
  return assembler.to_response(400, "Failed to read emails", "")
73
  return assembler.to_response(200, "", result)
74
 
75
+ """@generator.request_body(
76
+ {
77
+ "token": "String",
78
+ "uuid": "String",
79
+ "data": {
80
+ "sender": "testsender@gmail.com",
81
+ "pwd": "use app password of your google account",
82
+ "to": "testto@gmail.com",
83
+ "subject": "title of email",
84
+ "body": "email content",
85
+ "to_send": "true or false whether send to inbox or drafts",
86
+ "filename": "test.txt",
87
+ "file_content": "string encoded with base64"
88
+ },
89
+ }
90
+ )
91
+
92
+ @generator.response(
93
+ status_code=200, schema={"message": "message", "result": "result"
94
+ )
95
+
96
+ """
97
+
98
+ @router.post("/send_email")
99
+ def send_email(data: EmailSender):
100
+ # firebase admin init
101
+ try:
102
+ setting, firebase_app = firebase_admin_with_setting(data)
103
+ except BrainException as ex:
104
+ return assembler.to_response(ex.code, ex.message, "")
105
+ try:
106
+ token = setting.token
107
+ uuid = setting.uuid
108
+
109
+ # send email
110
+ email_manager = EmailPlugin()
111
+ result = ""
112
+ if not data.data.filename:
113
+ result += email_manager.send_email(
114
+ sender=data.data.sender,
115
+ pwd=data.data.pwd,
116
+ to=data.data.to,
117
+ subject=data.data.subject,
118
+ body=data.data.body,
119
+ to_send=data.data.to_send,
120
+ )
121
+ else:
122
+ result += email_manager.send_email_with_attachment(
123
+ sender=data.data.sender,
124
+ pwd=data.data.pwd,
125
+ to=data.data.to,
126
+ subject=data.data.subject,
127
+ body=data.data.body,
128
+ to_send=data.data.to_send,
129
+ filename=data.data.filename,
130
+ )
131
+ except Exception as e:
132
+ if isinstance(e, BrainException):
133
+ return e.get_response_exp()
134
+ return assembler.to_response(400, "Failed to read emails", "")
135
+ return assembler.to_response(200, "", result)
136
+
137
  return router