forked from rodrigo-brito/ninjabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
91 lines (75 loc) · 1.68 KB
/
mail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package notification
import (
"fmt"
"net/smtp"
log "github.com/sirupsen/logrus"
"github.com/buugaaga/ninjabot/model"
)
type Mail struct {
auth smtp.Auth
smtpServerPort int
smtpServerAddress string
to string
from string
}
func (t Mail) Notify(text string) {
serverAddress := fmt.Sprintf(
"%s:%d",
t.smtpServerAddress,
t.smtpServerPort)
message := fmt.Sprintf(
`To: "User" <%s>\nFrom: "NinjaBot" <%s>\n%s`,
t.to,
t.from,
text,
)
err := smtp.SendMail(
serverAddress,
t.auth,
t.from,
[]string{t.to},
[]byte(message))
if err != nil {
log.
WithError(err).
Errorf("notification/mail: couldnt send mail")
}
}
func (t Mail) OnOrder(order model.Order) {
title := ""
switch order.Status {
case model.OrderStatusTypeFilled:
title = fmt.Sprintf("✅ ORDER FILLED - %s", order.Pair)
case model.OrderStatusTypeNew:
title = fmt.Sprintf("🆕 NEW ORDER - %s", order.Pair)
case model.OrderStatusTypeCanceled, model.OrderStatusTypeRejected:
title = fmt.Sprintf("❌ ORDER CANCELED / REJECTED - %s", order.Pair)
}
message := fmt.Sprintf("Subject: %s\nOrder %s", title, order)
t.Notify(message)
}
func (t Mail) OnError(err error) {
message := fmt.Sprintf("Subject: 🛑 ERROR\nError %s", err)
t.Notify(message)
}
type MailParams struct {
SMTPServerPort int
SMTPServerAddress string
To string
From string
Password string
}
func NewMail(params MailParams) Mail {
return Mail{
from: params.From,
to: params.To,
smtpServerPort: params.SMTPServerPort,
smtpServerAddress: params.SMTPServerAddress,
auth: smtp.PlainAuth(
"",
params.From,
params.Password,
params.SMTPServerAddress,
),
}
}