parent
0b1afe6283
commit
cf1ccb5dcf
@ -0,0 +1,59 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"luxshare-daily-report/global"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Bark struct {
|
||||
url string
|
||||
key string
|
||||
}
|
||||
|
||||
var b = Bark{}
|
||||
|
||||
func initBark() {
|
||||
b.url = global.GLO_CONFIG.BarkUrl
|
||||
b.key = global.GLO_CONFIG.BarkKey
|
||||
Register("bark", b)
|
||||
}
|
||||
|
||||
func (m Bark) Send(message Body) {
|
||||
log.Println("[bark] Sending by bark...")
|
||||
var reqBody = Request{
|
||||
DeviceKey: b.key,
|
||||
Title: message.Title,
|
||||
Body: message.Content,
|
||||
Icon: "https://m.luxshare-ict.com/favicon.ico",
|
||||
//Url: "https://github.com/zggsong",
|
||||
}
|
||||
req, _ := json.Marshal(reqBody)
|
||||
resp, err := http.Post(m.url, "application/json; charset=utf-8", bytes.NewReader(req))
|
||||
if err != nil {
|
||||
log.Fatalf("[bark] http post failed: %v\n", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
log.Printf("[bark] Send successful")
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
Body string `json:"body"`
|
||||
DeviceKey string `json:"device_key"`
|
||||
Title string `json:"title"`
|
||||
Badge int `json:"badge"`
|
||||
Category string `json:"category"`
|
||||
Sound string `json:"sound"`
|
||||
Icon string `json:"icon"`
|
||||
Group string `json:"group"`
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Timestamp int `json:"timestamp"`
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jordan-wright/email"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"luxshare-daily-report/global"
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
type Mail struct {
|
||||
Host string `json:"host" yaml:"host"`
|
||||
Protocol string `json:"protocol" yaml:"protocol"`
|
||||
Port int `json:"port" yaml:"port"`
|
||||
Username string `json:"username" yaml:"username"`
|
||||
Password string `json:"password" yaml:"password"`
|
||||
FromName string `json:"from_name" yaml:"from_name"`
|
||||
To []string `json:"to" yaml:"to"`
|
||||
}
|
||||
|
||||
var m Mail
|
||||
|
||||
func initMail() {
|
||||
m = Mail{
|
||||
Host: global.GLO_CONFIG.MailHost,
|
||||
Port: global.GLO_CONFIG.MailPort,
|
||||
Username: global.GLO_CONFIG.MailUser,
|
||||
Password: global.GLO_CONFIG.MailPwd,
|
||||
FromName: global.GLO_CONFIG.MailFromName,
|
||||
To: global.GLO_CONFIG.MailTo,
|
||||
}
|
||||
Register("mail", m)
|
||||
}
|
||||
|
||||
func (m Mail) Send(message Body) {
|
||||
log.Println("[mail] Sending by mail...")
|
||||
e := email.NewEmail()
|
||||
e.From = m.FromName
|
||||
e.To = m.To
|
||||
e.Subject = message.Title
|
||||
e.Text = []byte(message.Content)
|
||||
addr := fmt.Sprintf("%v:%v", m.Host, m.Port)
|
||||
err := e.Send(addr, smtp.PlainAuth("", m.Username, m.Password, m.Host))
|
||||
if err != nil {
|
||||
log.Fatalf("[mail] Send failed: %v\n", err)
|
||||
}
|
||||
log.Printf("[mail] Send successful")
|
||||
}
|
@ -1 +1,33 @@
|
||||
package message
|
||||
|
||||
import "luxshare-daily-report/global"
|
||||
|
||||
var Messages = make(map[string]Message, 0)
|
||||
|
||||
type Message interface {
|
||||
Send(body Body)
|
||||
}
|
||||
|
||||
type Body struct {
|
||||
Title string
|
||||
Content string
|
||||
}
|
||||
|
||||
// GetSupport 获取支持
|
||||
func GetSupport() Message {
|
||||
initBark()
|
||||
initMail()
|
||||
//key := "bark"
|
||||
key := global.GLO_CONFIG.MsgType
|
||||
return Messages[key]
|
||||
}
|
||||
|
||||
// Enabled 是否启用
|
||||
func Enabled() bool {
|
||||
return global.GLO_CONFIG.MsgEnabled
|
||||
}
|
||||
|
||||
// Register 注册
|
||||
func Register(name string, message Message) {
|
||||
Messages[name] = message
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
package support
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"luxshare-daily-report/message"
|
||||
)
|
||||
|
||||
// SendMessageError 发送错误信息
|
||||
func SendMessageError(err error) {
|
||||
var body = message.Body{}
|
||||
body.Title = "LuxShareReportServer"
|
||||
body.Content = fmt.Sprintf("Error occurred: %s", err.Error())
|
||||
send(body)
|
||||
}
|
||||
|
||||
// SendSuccess 发送成功信息
|
||||
func SendSuccess(content string) {
|
||||
body := message.Body{
|
||||
Title: "LuxShareReportServer",
|
||||
Content: content,
|
||||
}
|
||||
send(body)
|
||||
}
|
||||
|
||||
// send 发送
|
||||
func send(body message.Body) {
|
||||
m := message.GetSupport()
|
||||
if m == nil {
|
||||
return
|
||||
}
|
||||
enabled := message.Enabled()
|
||||
if enabled {
|
||||
m.Send(body)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue