Send email to share results of scheduled task

This commit is contained in:
Debanjum Singh Solanky 2024-04-22 03:40:34 +05:30
parent 69775b6d6e
commit a5ed4f2af2
3 changed files with 68 additions and 3 deletions

View file

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html>
<head>
<title>Khoj AI - Task</title>
</head>
<body>
<body style="font-family: 'Verdana', sans-serif; font-weight: 400; font-style: normal; padding: 0; text-align: left; width: 600px; margin: 20px auto;">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<a class="logo" href="https://khoj.dev" target="_blank" style="text-decoration: none; text-decoration: underline dotted;">
<img src="https://khoj.dev/khoj-logo-sideways-500.png" alt="Khoj Logo" style="width: 100px;">
</a>
<div class="calls-to-action" style="margin-top: 20px;">
<div>
<h1 style="color: #333; font-size: large; font-weight: bold; margin: 0; line-height: 1.5; background-color: #fee285; padding: 8px; box-shadow: 6px 6px rgba(0, 0, 0, 1.5);">Merge AI with your brain</h1>
<p style="color: #333; font-size: medium; margin-top: 20px; padding: 0; line-height: 1.5;">Hey {{name}}! </p>
<p style="color: #333; font-size: medium; margin-top: 20px; padding: 0; line-height: 1.5;">I've shared the results you'd requested below:</p>
<div style="display: grid; grid-template-columns: 1fr 1fr; grid-gap: 12px; margin-top: 20px;">
<div style="border: 1px solid black; border-radius: 8px; padding: 8px; box-shadow: 6px 6px rgba(0, 0, 0, 1.0); margin-top: 20px;">
<a href="https://app.khoj.dev/config#tasks" style="text-decoration: none; text-decoration: underline dotted;">
<h3 style="color: #333; font-size: large; margin: 0; padding: 0; line-height: 2.0; background-color: #b8f1c7; padding: 8px; ">{{query}}</h3>
</a>
<p style="color: #333; font-size: medium; margin-top: 20px; padding: 0; line-height: 1.5;">{{result}}</p>
</div>
</div>
<p style="color: #333; font-size: medium; margin-top: 20px; padding: 0; line-height: 1.5;">You can view, delete and manage your scheduled tasks on <a href="https://app.khoj.dev/configure#tasks">the settings page</a></p>
</div>
</div>
<p style="color: #333; font-size: large; margin-top: 20px; padding: 0; line-height: 1.5;">- Khoj</p>
<table style="width: 100%; margin-top: 20px;">
<tr>
<td style="text-align: center;"><a href="https://docs.khoj.dev" target="_blank" style="padding: 8px; color: #333; background-color: #fee285; border-radius: 8px; box-shadow: 6px 6px rgba(0, 0, 0, 1.0);">Docs</a></td>
<td style="text-align: center;"><a href="https://github.com/khoj-ai/khoj" target="_blank" style="padding: 8px; color: #333; background-color: #fee285; border-radius: 8px; box-shadow: 6px 6px rgba(0, 0, 0, 1.0);">GitHub</a></td>
<td style="text-align: center;"><a href="https://twitter.com/khoj_ai" target="_blank" style="padding: 8px; color: #333; background-color: #fee285; border-radius: 8px; box-shadow: 6px 6px rgba(0, 0, 0, 1.0);">Twitter</a></td>
<td style="text-align: center;"><a href="https://www.linkedin.com/company/khoj-ai" target="_blank" style="padding: 8px; color: #333; background-color: #fee285; border-radius: 8px; box-shadow: 6px 6px rgba(0, 0, 0, 1.0);">LinkedIn</a></td>
<td style="text-align: center;"><a href="https://discord.gg/BDgyabRM6e" target="_blank" style="padding: 8px; color: #333; background-color: #fee285; border-radius: 8px; box-shadow: 6px 6px rgba(0, 0, 0, 1.0);">Discord</a></td>
</tr>
</table>
</body>
</html>

View file

@ -30,7 +30,7 @@ def is_resend_enabled():
return bool(RESEND_API_KEY) return bool(RESEND_API_KEY)
async def send_welcome_email(name, email): def send_welcome_email(name, email):
if not is_resend_enabled(): if not is_resend_enabled():
logger.debug("Email sending disabled") logger.debug("Email sending disabled")
return return
@ -47,3 +47,22 @@ async def send_welcome_email(name, email):
"html": html_content, "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 <khoj@khoj.dev>",
"to": email,
"subject": f'✨ Your Task Results for "{query}"',
"html": html_content,
}
)

View file

@ -53,6 +53,7 @@ from khoj.processor.conversation.utils import (
generate_chatml_messages_with_context, generate_chatml_messages_with_context,
save_to_conversation_log, save_to_conversation_log,
) )
from khoj.routers.email import is_resend_enabled, send_task_email
from khoj.routers.storage import upload_image from khoj.routers.storage import upload_image
from khoj.utils import state from khoj.utils import state
from khoj.utils.config import OfflineChatProcessorModel from khoj.utils.config import OfflineChatProcessorModel
@ -894,6 +895,7 @@ def scheduled_chat(executing_query: str, scheduling_query: str, user: KhojUser,
return None return None
# Extract the AI response from the chat API response # 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": if raw_response.headers.get("Content-Type") == "application/json":
response_map = raw_response.json() response_map = raw_response.json()
ai_response = response_map.get("response") or response_map.get("image") 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 ai_response = raw_response.text
# Notify user if the AI response is satisfactory # Notify user if the AI response is satisfactory
if should_notify(original_query=scheduling_query, executed_query=executing_query, ai_response=ai_response): if should_notify(original_query=scheduling_query, executed_query=cleaned_query, ai_response=ai_response):
return raw_response if is_resend_enabled():
send_task_email(user.get_short_name(), user.email, scheduling_query, ai_response)
else:
return raw_response