From a5ed4f2af28f6c2bfc083b5e614408c406839bb4 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Mon, 22 Apr 2024 03:40:34 +0530 Subject: [PATCH] Send email to share results of scheduled task --- src/khoj/interface/email/task.html | 41 ++++++++++++++++++++++++++++++ src/khoj/routers/email.py | 21 ++++++++++++++- src/khoj/routers/helpers.py | 9 +++++-- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 src/khoj/interface/email/task.html diff --git a/src/khoj/interface/email/task.html b/src/khoj/interface/email/task.html new file mode 100644 index 00000000..09035092 --- /dev/null +++ b/src/khoj/interface/email/task.html @@ -0,0 +1,41 @@ + + + + Khoj AI - Task + + + + + +
+
+

Merge AI with your brain

+

Hey {{name}}!

+

I've shared the results you'd requested below:

+ +
+
+ +

{{query}}

+
+

{{result}}

+
+
+

You can view, delete and manage your scheduled tasks on the settings page

+
+
+

- Khoj

+ + + + + + + + +
DocsGitHubTwitterLinkedInDiscord
+ + + diff --git a/src/khoj/routers/email.py b/src/khoj/routers/email.py index 86bf67ee..ba28d029 100644 --- a/src/khoj/routers/email.py +++ b/src/khoj/routers/email.py @@ -30,7 +30,7 @@ def is_resend_enabled(): return bool(RESEND_API_KEY) -async def send_welcome_email(name, email): +def send_welcome_email(name, email): if not is_resend_enabled(): logger.debug("Email sending disabled") return @@ -47,3 +47,22 @@ async def send_welcome_email(name, email): "html": html_content, } ) + + +def send_task_email(name, email, query, result): + if not is_resend_enabled(): + logger.debug("Email sending disabled") + return + + template = env.get_template("task.html") + + html_content = template.render(name=name, query=query, result=result) + + resend.Emails.send( + { + "from": "Khoj ", + "to": email, + "subject": f'✨ Your Task Results for "{query}"', + "html": html_content, + } + ) diff --git a/src/khoj/routers/helpers.py b/src/khoj/routers/helpers.py index 0f1d3728..8059733d 100644 --- a/src/khoj/routers/helpers.py +++ b/src/khoj/routers/helpers.py @@ -53,6 +53,7 @@ from khoj.processor.conversation.utils import ( generate_chatml_messages_with_context, save_to_conversation_log, ) +from khoj.routers.email import is_resend_enabled, send_task_email from khoj.routers.storage import upload_image from khoj.utils import state from khoj.utils.config import OfflineChatProcessorModel @@ -894,6 +895,7 @@ def scheduled_chat(executing_query: str, scheduling_query: str, user: KhojUser, return None # Extract the AI response from the chat API response + cleaned_query = scheduling_query.replace("/task", "", 1).strip() if raw_response.headers.get("Content-Type") == "application/json": response_map = raw_response.json() ai_response = response_map.get("response") or response_map.get("image") @@ -901,5 +903,8 @@ def scheduled_chat(executing_query: str, scheduling_query: str, user: KhojUser, ai_response = raw_response.text # Notify user if the AI response is satisfactory - if should_notify(original_query=scheduling_query, executed_query=executing_query, ai_response=ai_response): - return raw_response + if should_notify(original_query=scheduling_query, executed_query=cleaned_query, ai_response=ai_response): + if is_resend_enabled(): + send_task_email(user.get_short_name(), user.email, scheduling_query, ai_response) + else: + return raw_response