Skip to content

Commit

Permalink
add email debt notification (#4820)
Browse files Browse the repository at this point in the history
* fix email is nil

* add email notification
  • Loading branch information
bxy4543 authored Jun 28, 2024
1 parent a1442f9 commit d11c5fd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 42 deletions.
93 changes: 51 additions & 42 deletions controllers/account/controllers/debt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"strings"
"time"

"github.com/alibabacloud-go/tea/tea"

"github.com/volcengine/volc-sdk-golang/service/vms"

"github.com/labring/sealos/controllers/pkg/pay"
Expand All @@ -43,8 +45,6 @@ import (

userv1 "github.com/labring/sealos/controllers/user/api/v1"

"github.com/alibabacloud-go/tea/tea"

"github.com/labring/sealos/controllers/pkg/database"

client2 "github.com/alibabacloud-go/dysmsapi-20170525/v3/client"
Expand Down Expand Up @@ -432,31 +432,35 @@ const (
trueStatus = "true"
)

var NoticeTemplateEN map[int]string

var TitleTemplateZH = map[int]string{
WarningNotice: "欠费告警",
ApproachingDeletionNotice: "资源暂停告警",
ImminentDeletionNotice: "资源释放告警",
FinalDeletionNotice: "资源已释放告警",
}

var TitleTemplateEN = map[int]string{
WarningNotice: "Debt Warning",
ApproachingDeletionNotice: "Resource Suspension Warning",
ImminentDeletionNotice: "Resource Release Warning",
FinalDeletionNotice: "Resource Release Warning",
}
var (
TitleTemplateZH = map[int]string{
WarningNotice: "欠费告警",
ApproachingDeletionNotice: "资源暂停告警",
ImminentDeletionNotice: "资源释放告警",
FinalDeletionNotice: "资源已释放告警",
}
TitleTemplateEN = map[int]string{
WarningNotice: "Debt Warning",
ApproachingDeletionNotice: "Resource Suspension Warning",
ImminentDeletionNotice: "Resource Release Warning",
FinalDeletionNotice: "Resource Release Warning",
}
)

var NoticeTemplateZH map[int]string
var (
EmailTemplateEN map[int]string
EmailTemplateZH map[int]string
NoticeTemplateEN map[int]string
NoticeTemplateZH map[int]string
)

var (
forbidTimes = []string{"00:00-10:00", "20:00-24:00"}
UTCPlus8 = time.FixedZone("UTC+8", 8*3600)
)

func (r *DebtReconciler) sendSMSNotice(user string, oweAmount int64, noticeType int) error {
if r.SmsConfig == nil {
if r.SmsConfig == nil && r.VmsConfig == nil && r.smtpConfig == nil {
return nil
}
outh, err := r.AccountV2.GetUserOauthProvider(&pkgtypes.UserQueryOpts{Owner: user})
Expand All @@ -471,33 +475,32 @@ func (r *DebtReconciler) sendSMSNotice(user string, oweAmount int64, noticeType
email = outh[i].ProviderID
}
}
if phone == "" && email == "" {
r.Logger.Info("user phone && email is not set, skip sms notification", "user", user)
return nil
}
oweamount := strconv.FormatInt(int64(math.Abs(math.Ceil(float64(oweAmount)/1_000_000))), 10)
err = utils.SendSms(r.SmsConfig.Client, &client2.SendSmsRequest{
PhoneNumbers: tea.String(phone),
SignName: tea.String(r.SmsConfig.SmsSignName),
TemplateCode: tea.String(r.SmsConfig.SmsCode[noticeType]),
// |ownAmount/1_000_000|
TemplateParam: tea.String("{\"user_id\":\"" + user + "\",\"oweamount\":\"" + oweamount + "\"}"),
})
if err != nil {
return fmt.Errorf("failed to send sms notice: %w", err)
}
if noticeType == WarningNotice {
err = utils.SendVms(phone, r.VmsConfig.TemplateCode[noticeType], r.VmsConfig.NumberPoll, GetSendVmsTimeInUTCPlus8(time.Now()), forbidTimes)
if err != nil {
return fmt.Errorf("failed to send vms notice: %w", err)
if phone != "" {
if r.SmsConfig != nil && r.SmsConfig.SmsCode[noticeType] != "" {
oweamount := strconv.FormatInt(int64(math.Abs(math.Ceil(float64(oweAmount)/1_000_000))), 10)
err = utils.SendSms(r.SmsConfig.Client, &client2.SendSmsRequest{
PhoneNumbers: tea.String(phone),
SignName: tea.String(r.SmsConfig.SmsSignName),
TemplateCode: tea.String(r.SmsConfig.SmsCode[noticeType]),
// |ownAmount/1_000_000|
TemplateParam: tea.String("{\"user_id\":\"" + user + "\",\"oweamount\":\"" + oweamount + "\"}"),
})
if err != nil {
return fmt.Errorf("failed to send sms notice: %w", err)
}
}
if r.smtpConfig != nil {
err = r.smtpConfig.SendEmail(NoticeTemplateZH[noticeType], email)
if r.VmsConfig != nil && noticeType == WarningNotice && r.VmsConfig.TemplateCode[noticeType] != "" {
err = utils.SendVms(phone, r.VmsConfig.TemplateCode[noticeType], r.VmsConfig.NumberPoll, GetSendVmsTimeInUTCPlus8(time.Now()), forbidTimes)
if err != nil {
return fmt.Errorf("failed to send email notice: %w", err)
return fmt.Errorf("failed to send vms notice: %w", err)
}
}
}
if r.smtpConfig != nil && email != "" {
if err = r.smtpConfig.SendEmail(EmailTemplateZH[noticeType]+"\n"+EmailTemplateEN[noticeType], email); err != nil {
return fmt.Errorf("failed to send email notice: %w", err)
}
}
return nil
}

Expand Down Expand Up @@ -690,7 +693,7 @@ func (r *DebtReconciler) setupSMTPConfig() error {
ServerPort: serverPort,
FromEmail: os.Getenv(SMTPFromEnv),
Passwd: os.Getenv(SMTPPasswordEnv),
EmailTitle: SMTPTitleEnv,
EmailTitle: os.Getenv(SMTPTitleEnv),
}
return nil
}
Expand Down Expand Up @@ -755,6 +758,12 @@ func setDefaultDebtPeriodWaitSecond() {
ImminentDeletionNotice: fmt.Sprintf("Your container instance resources have been suspended, and the system will completely release the resources after %2.f hours, which cannot be recovered. Please recharge in time to avoid affecting your normal use.", math.Ceil(float64(DebtConfig[accountv1.FinalDeletionPeriod])/3600)),
FinalDeletionNotice: "The system will completely release all your resources at any time. Please recharge in time to avoid affecting your normal use.",
}
domain := os.Getenv("DOMAIN")
EmailTemplateEN, EmailTemplateZH = make(map[int]string), make(map[int]string)
for _, i := range []int{WarningNotice, ApproachingDeletionNotice, ImminentDeletionNotice, FinalDeletionNotice} {
EmailTemplateEN[i] = TitleTemplateEN[i] + ":" + NoticeTemplateEN[i] + "(" + domain + ")"
EmailTemplateZH[i] = TitleTemplateZH[i] + ":" + NoticeTemplateZH[i] + "(" + domain + ")"
}
}

type UserOwnerPredicate struct {
Expand Down
1 change: 1 addition & 0 deletions controllers/go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvSc
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/gobuffalo/flect v0.2.3 h1:f/ZukRnSNA/DUpSNDadko7Qc0PhGvsew35p/2tu+CRY=
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.6 h1:mkgN1ofwASrYnJ5W6U/BxG15eXXXjirgZc7CLqkcaro=
github.com/godbus/dbus/v5 v5.0.6/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
Expand Down

0 comments on commit d11c5fd

Please sign in to comment.