|
12 | 12 | ) |
13 | 13 | import os |
14 | 14 | from apscheduler.schedulers.background import BackgroundScheduler |
15 | | -scheduler = BackgroundScheduler() |
16 | 15 | router = APIRouter(prefix="/api/v1") |
17 | 16 |
|
18 | 17 | @router.post( |
@@ -48,12 +47,16 @@ async def exchange_code_for_token( |
48 | 47 | Exchanges an authorization code for an access token. |
49 | 48 | """ |
50 | 49 | try: |
| 50 | + from src.main import code_app |
| 51 | + scheduler = BackgroundScheduler() |
51 | 52 | response = await nylas_crud.login_user(request.token, session) |
52 | 53 |
|
53 | 54 | if response: |
54 | | - ensure_future(send_welcome_email(response["user"]["email"])) |
55 | | - ensure_future(async_send_algorithm_email(response["user"]["email"])) |
56 | | - scheduler.add_job(send_algorithm_email, 'interval', hours=24, args=(response["user"]["email"],)) |
| 55 | + # send a welcome email in the background |
| 56 | + ensure_future(nylas_crud.send_welcome_email(response["user"]["email"])) |
| 57 | + # send an algorithm email in the background |
| 58 | + ensure_future(code_app.state.openai.async_send_algorithm_email(response["user"]["email"], "python")) |
| 59 | + scheduler.add_job(code_app.state.openai.send_algorithm_email, 'interval', hours=24, args=(response["user"]["email"], "python")) |
57 | 60 | scheduler.start() |
58 | 61 | return response |
59 | 62 | return { |
@@ -121,15 +124,16 @@ def send_email( |
121 | 124 | draft = code_app.state.nylas.drafts.create() |
122 | 125 | draft['subject'] = request_body.subject |
123 | 126 | draft['to'] = [{"email": item.email} for item in request_body.to] |
124 | | - draft['cc'] = [{'email': request_body.cc}] |
125 | | - draft['bcc'] = [{'email': request_body.bcc}] |
| 127 | + if request_body.cc: |
| 128 | + draft['cc'] = [{'email': request_body.cc}] |
| 129 | + if request_body.bcc: |
| 130 | + draft['bcc'] = [{'email': request_body.bcc}] |
126 | 131 | draft['body'] = request_body.message |
127 | 132 | draft['from'] = [{'email': current_user.email}] |
| 133 | + print(draft) |
128 | 134 | message = draft.send() |
129 | 135 | return message |
130 | 136 |
|
131 | | - #fR2jJBhahgu44V9KDmWYN2d3YDZPDz |
132 | | - |
133 | 137 | @router.get( |
134 | 138 | "/nylas/read-labels", |
135 | 139 | response_model=List[Dict[str, Any]], |
@@ -209,140 +213,53 @@ def create_label( |
209 | 213 | return {"message": "Emails' folders updated successfully"} |
210 | 214 |
|
211 | 215 |
|
212 | | -async def send_welcome_email(to): |
213 | | - from src.main import code_app |
214 | | - initial_token = code_app.state.nylas.access_token |
215 | | - code_app.state.nylas.access_token = settings().NYLAS_SYSTEM_TOKEN |
216 | | - draft = code_app.state.nylas.drafts.create() |
217 | | - with open(os.getcwd() + "/static/welcome_email.html", "r", encoding="utf-8") as file: |
218 | | - html_content = file.read() |
219 | | - draft['subject'] = "Welcome to Code Inbox 🚀" |
220 | | - draft['to'] = [{"email": to}] |
221 | | - draft['body'] = html_content |
222 | | - draft['from'] = [{'email': code_app.state.nylas.account.email_address}] |
223 | | - # TODO: draft.send_raw |
224 | | - message = draft.send() |
225 | | - code_app.state.nylas.access_token = initial_token |
226 | | - |
227 | | - |
228 | | -def send_algorithm_email(to): |
229 | | - from src.main import code_app |
230 | | - import openai |
231 | | - initial_token = code_app.state.nylas.access_token |
232 | | - code_app.state.nylas.access_token = settings().NYLAS_SYSTEM_TOKEN |
233 | | - draft = code_app.state.nylas.drafts.create() |
234 | | - prompt = """ |
235 | | - **Task Prompt:** |
236 | | -
|
237 | | - As an algorithm expert, your task is to generate a comprehensive algorithm tutorial. The tutorial should cover a specific algorithmic topic of your choice (e.g., sorting algorithms, search algorithms, dynamic programming, graph algorithms, etc.) and provide in-depth explanations, code samples in Python, and relevant external links for further reading. |
238 | | -
|
239 | | - **Instructions:** |
240 | | -
|
241 | | - 1. Choose an algorithmic topic that you are knowledgeable about or interested in. |
242 | | - 2. Create a tutorial that covers the selected topic in detail. |
243 | | - 3. Your tutorial should be structured as an HTML page and include the following sections: |
244 | | -
|
245 | | - - Title: A clear and informative title for the tutorial. |
246 | | - - Introduction: Briefly introduce the algorithmic topic you will be covering and explain its importance or relevance. |
247 | | - - Overview: Provide an overview of the key concepts and principles related to the algorithm. |
248 | | - - Detailed Explanations: Break down the algorithm into its components and explain each step or concept thoroughly. Use clear and concise language. |
249 | | - - Python Code Samples: Include Python code examples that illustrate how the algorithm works. Ensure that the code is well-commented and easy to understand. |
250 | | - - Visualizations (optional): If applicable, include visual representations or diagrams to aid in understanding. |
251 | | - - Complexity Analysis: Discuss the time and space complexity of the algorithm and analyze its efficiency. |
252 | | - - Applications: Describe real-world applications or scenarios where the algorithm is commonly used. |
253 | | - - External Links: Provide links to external resources, research papers, or additional reading materials for those who want to explore the topic further. |
254 | | - - Conclusion: Summarize the key takeaways from the tutorial and reiterate the significance of the algorithm. |
255 | | -
|
256 | | - 4. Ensure that your HTML page is well-structured, with appropriate headings, paragraphs, and code formatting. |
257 | | - 5. Use hyperlinks to connect sections, references, and external links. |
258 | | - 6. Make use of proper HTML tags for formatting and styling, such as headings, lists, and code blocks. |
259 | | - 7. Proofread and edit your tutorial for clarity, accuracy, and completeness. |
260 | | -
|
261 | | - **Note:** Feel free to choose any algorithmic topic that you are comfortable with. Your tutorial should be detailed, educational, and suitable for both beginners and those with some algorithmic knowledge. |
| 216 | +@router.post( |
| 217 | + "/nylas/reply-email", |
| 218 | + response_model=Dict[str, Any], |
| 219 | + status_code=200, |
| 220 | + name="nylas:reply-email", |
| 221 | +) |
| 222 | +def reply_email( |
| 223 | + request_body: nylas_schemas.ReplyEmailSchema, |
| 224 | + current_user: users_schemas.UserObjectSchema = Depends( |
| 225 | + dependencies.get_current_user |
| 226 | + )) -> Dict[str, Any]: |
262 | 227 | """ |
263 | | - params = { |
264 | | - "model": "gpt-3.5-turbo", |
265 | | - "temperature": 0, |
266 | | - "max_tokens": 128, |
267 | | - "top_p": 1, |
268 | | - "frequency_penalty": 0, |
269 | | - "presence_penalty": 0.6, |
270 | | - "messages": [ |
271 | | - { |
272 | | - "role": "system", |
273 | | - "content": prompt, |
274 | | - } |
275 | | - ], |
276 | | - } |
277 | | - openai.api_key = settings().OPENAI_API_KEY |
278 | | - response = openai.ChatCompletion.create(**params) |
279 | | - html_content = response["choices"][0]["message"]["content"] |
280 | | - draft['subject'] = "Your Daily Dose of Code Inbox" |
281 | | - draft['to'] = [{"email": to}] |
282 | | - draft['body'] = html_content |
283 | | - draft['from'] = [{'email': code_app.state.nylas.account.email_address}] |
| 228 | + Sends a reply on behalf of the user using their access token. |
| 229 | + """ |
| 230 | + from src.main import ( |
| 231 | + code_app, |
| 232 | + ) |
| 233 | + thread = code_app.state.nylas.threads.get(request_body.thread_id) |
| 234 | + draft = thread.create_reply() |
| 235 | + draft.body = request_body.body |
| 236 | + draft.cc = thread.cc |
| 237 | + draft.bcc = thread.bcc |
| 238 | + # a hack to remove the current user from the to list cause thread has no from_ attribute |
| 239 | + for participant in thread.participants: |
| 240 | + if participant.get("email") == current_user.email: |
| 241 | + thread.participants.remove(participant) |
| 242 | + draft.to = thread.participants |
| 243 | + # draft.from_ = [{'email': current_user.email}] |
284 | 244 | message = draft.send() |
285 | | - code_app.state.nylas.access_token = initial_token |
286 | | - openai.api_key = "" |
287 | | - |
288 | | - |
289 | | -async def async_send_algorithm_email(to): |
290 | | - from src.main import code_app |
291 | | - import openai |
292 | | - initial_token = code_app.state.nylas.access_token |
293 | | - code_app.state.nylas.access_token = settings().NYLAS_SYSTEM_TOKEN |
294 | | - draft = code_app.state.nylas.drafts.create() |
295 | | - prompt = """ |
296 | | - **Task Prompt:** |
297 | | -
|
298 | | - As an algorithm expert, your task is to generate a comprehensive algorithm tutorial. The tutorial should cover a specific algorithmic topic of your choice (e.g., sorting algorithms, search algorithms, dynamic programming, graph algorithms, etc.) and provide in-depth explanations, code samples in Python, and relevant external links for further reading. |
299 | | -
|
300 | | - **Instructions:** |
301 | | -
|
302 | | - 1. Choose an algorithmic topic that you are knowledgeable about or interested in. |
303 | | - 2. Create a tutorial that covers the selected topic in detail. |
304 | | - 3. Your tutorial should be structured as an HTML page and include the following sections: |
305 | | -
|
306 | | - - Title: A clear and informative title for the tutorial. |
307 | | - - Introduction: Briefly introduce the algorithmic topic you will be covering and explain its importance or relevance. |
308 | | - - Overview: Provide an overview of the key concepts and principles related to the algorithm. |
309 | | - - Detailed Explanations: Break down the algorithm into its components and explain each step or concept thoroughly. Use clear and concise language. |
310 | | - - Python Code Samples: Include Python code examples that illustrate how the algorithm works. Ensure that the code is well-commented and easy to understand. |
311 | | - - Visualizations (optional): If applicable, include visual representations or diagrams to aid in understanding. |
312 | | - - Complexity Analysis: Discuss the time and space complexity of the algorithm and analyze its efficiency. |
313 | | - - Applications: Describe real-world applications or scenarios where the algorithm is commonly used. |
314 | | - - External Links: Provide links to external resources, research papers, or additional reading materials for those who want to explore the topic further. |
315 | | - - Conclusion: Summarize the key takeaways from the tutorial and reiterate the significance of the algorithm. |
316 | | -
|
317 | | - 4. Ensure that your HTML page is well-structured, with appropriate headings, paragraphs, and code formatting. |
318 | | - 5. Use hyperlinks to connect sections, references, and external links. |
319 | | - 6. Make use of proper HTML tags for formatting and styling, such as headings, lists, and code blocks. |
320 | | - 7. Proofread and edit your tutorial for clarity, accuracy, and completeness. |
| 245 | + return message |
321 | 246 |
|
322 | | - **Note:** Feel free to choose any algorithmic topic that you are comfortable with. Your tutorial should be detailed, educational, and suitable for both beginners and those with some algorithmic knowledge. |
| 247 | +@router.get( |
| 248 | + "/nylas/contacts", |
| 249 | + response_model=None, |
| 250 | + status_code=200, |
| 251 | + name="nylas:contacts", |
| 252 | +) |
| 253 | +def read_contacts( |
| 254 | + current_user: users_schemas.UserObjectSchema = Depends( |
| 255 | + dependencies.get_current_user |
| 256 | + )) -> Dict[str, Any]: |
323 | 257 | """ |
324 | | - params = { |
325 | | - "model": "gpt-3.5-turbo", |
326 | | - "temperature": 0, |
327 | | - "max_tokens": 512, |
328 | | - "top_p": 1, |
329 | | - "frequency_penalty": 0, |
330 | | - "presence_penalty": 0.6, |
331 | | - "messages": [ |
332 | | - { |
333 | | - "role": "system", |
334 | | - "content": prompt, |
335 | | - } |
336 | | - ], |
337 | | - } |
338 | | - openai.api_key = settings().OPENAI_API_KEY |
339 | | - response = openai.ChatCompletion.create(**params) |
340 | | - html_content = response["choices"][0]["message"]["content"] |
341 | | - draft['subject'] = "Your Daily Dose of Code Inbox" |
342 | | - draft['to'] = [{"email": to}] |
343 | | - draft['body'] = html_content |
344 | | - draft['from'] = [{'email': code_app.state.nylas.account.email_address}] |
345 | | - message = draft.send() |
346 | | - code_app.state.nylas.access_token = initial_token |
347 | | - openai.api_key = "" |
| 258 | + Read all contacts on behalf of the user using their access token. |
| 259 | + """ |
| 260 | + from src.main import ( |
| 261 | + code_app, |
| 262 | + ) |
| 263 | + # todo |
| 264 | + return [] |
348 | 265 |
|
0 commit comments