mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
205 lines
6.1 KiB
Go
205 lines
6.1 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2024 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
package object
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/ai/conf"
|
|
"github.com/hanzoai/ai/i18n"
|
|
iam "github.com/hanzoai/ai/internal/iam"
|
|
"github.com/hanzoai/ai/util"
|
|
)
|
|
|
|
func (message *Message) SendEmail(lang string, orgName ...string) error {
|
|
iamOrganization := conf.GetConfigString("IAM_ORG")
|
|
if len(orgName) > 0 && orgName[0] != "" {
|
|
iamOrganization = orgName[0]
|
|
}
|
|
organization, err := iam.GetOrganization(iamOrganization)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if organization == nil {
|
|
return fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "object:IAM organization: [%s] doesn't exist"), iamOrganization))
|
|
}
|
|
sender := organization.DisplayName
|
|
iamApplication := conf.GetConfigString("IAM_APP_NAME")
|
|
application, err := iam.GetApplication(iamApplication)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if application == nil {
|
|
return fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "object:IAM application: [%s] doesn't exist"), iamApplication))
|
|
}
|
|
title := application.DisplayName
|
|
logoUrl := conf.GetConfigString("logoUrl")
|
|
user, err := iam.GetUser(message.User)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if user == nil {
|
|
return fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "object:IAM user: [%s] doesn't exist"), message.User))
|
|
}
|
|
username := user.Name
|
|
receiverEmail := user.Email
|
|
questionMessage, err := GetMessage(util.GetId("admin", message.ReplyTo))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// The mail quotes the question this answers, so there is nothing to send when
|
|
// the question has been deleted.
|
|
if questionMessage == nil {
|
|
return fmt.Errorf("%s", i18n.Translate(lang, "general:The message does not exist"))
|
|
}
|
|
question := questionMessage.Text
|
|
content := fmt.Sprintf(`<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Hanzo Cloud Message Update</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; }
|
|
.email-container { width: 600px; margin: 0 auto; }
|
|
.header { text-align: center; }
|
|
.code { font-size: 24px; margin: 20px 0; text-align: center; }
|
|
.footer { font-size: 12px; text-align: center; margin-top: 50px; }
|
|
.footer a { color: #000; text-decoration: none; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="email-container">
|
|
<div class="header">
|
|
<h3>%s</h3>
|
|
<img src="%s" alt="Hanzo Cloud Logo" width="300">
|
|
</div>
|
|
<p>Hi <strong>%s</strong>, your AI reply has been updated by the administrator! </p>
|
|
<p>Question:</p>
|
|
<div class="code">
|
|
%s
|
|
</div>
|
|
<p>Original answer:</p>
|
|
<div class="code">
|
|
%s
|
|
</div>
|
|
<p>Updated answer:</p>
|
|
<div class="code">
|
|
%s
|
|
</div>
|
|
<p>Thanks</p>
|
|
<p>%s</p>
|
|
<hr>
|
|
<div class="footer">
|
|
<p>Copyright © 2025 Hanzo AI Inc.</p>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
`, title, logoUrl, username, question, message.Text, message.Comment, title)
|
|
err = iam.SendEmail(title, content, sender, receiverEmail)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (message *Message) SendErrorEmail(errorText string, lang string, orgName ...string) error {
|
|
adminUser, err := iam.GetUser("admin")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if adminUser == nil {
|
|
return fmt.Errorf("%s", i18n.Translate(lang, "object:SendErrorEmail() error, the receiver user: \")admin\" doesn't exist"))
|
|
}
|
|
receiverEmail := adminUser.Email
|
|
if !strings.HasPrefix(receiverEmail, "51") {
|
|
return nil
|
|
}
|
|
iamOrganization := conf.GetConfigString("IAM_ORG")
|
|
if len(orgName) > 0 && orgName[0] != "" {
|
|
iamOrganization = orgName[0]
|
|
}
|
|
organization, err := iam.GetOrganization(iamOrganization)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if organization == nil {
|
|
return fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "object:IAM organization: [%s] doesn't exist"), iamOrganization))
|
|
}
|
|
sender := organization.DisplayName
|
|
user, err := iam.GetUser(message.User)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
username := user.Name
|
|
title := fmt.Sprintf("AI-Error: %s - %s - %s - %s", sender, username, message.Chat, message.Name)
|
|
logoUrl := conf.GetConfigString("logoUrl")
|
|
questionMessage, err := GetMessage(util.GetId("admin", message.ReplyTo))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if questionMessage == nil {
|
|
return fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "object:Question message: [%s] doesn't exist"), message.ReplyTo))
|
|
}
|
|
question := questionMessage.Text
|
|
content := fmt.Sprintf(`<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Hanzo Cloud Message Error</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; }
|
|
.email-container { width: 600px; margin: 0 auto; }
|
|
.header { text-align: center; }
|
|
.code { font-size: 24px; margin: 20px 0; text-align: center; }
|
|
.footer { font-size: 12px; text-align: center; margin-top: 50px; }
|
|
.footer a { color: #000; text-decoration: none; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="email-container">
|
|
<div class="header">
|
|
<h3>%s</h3>
|
|
<img src="%s" alt="Hanzo Cloud Logo" width="300">
|
|
</div>
|
|
<p>The message for user: <strong>%s</strong> has encountered error! </p>
|
|
<p>Question:</p>
|
|
<div class="code">
|
|
%s
|
|
</div>
|
|
<p>Error text:</p>
|
|
<div class="code">
|
|
%s
|
|
</div>
|
|
<p>Thanks</p>
|
|
<p>%s</p>
|
|
<hr>
|
|
<div class="footer">
|
|
<p>Copyright © 2025 Hanzo AI Inc.</p>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
`, title, logoUrl, username, question, errorText, sender)
|
|
err = iam.SendEmail(title, content, sender, receiverEmail)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|