feat: add new func to send bark and add send to mail

main
zggsong 2 years ago
parent 0b1afe6283
commit cf1ccb5dcf

@ -19,7 +19,7 @@ import (
func InitialConfig() model.Config { func InitialConfig() model.Config {
workPath, _ := os.Executable() workPath, _ := os.Executable()
filePath := path.Dir(workPath) filePath := path.Dir(workPath)
filePath = filepath.Join(filePath, "/config/.yml") filePath = filepath.Join(filePath, "/config/config.yml")
viper.SetConfigName("config") viper.SetConfigName("config")
viper.SetConfigType("yml") viper.SetConfigType("yml")
viper.AddConfigPath("./config") viper.AddConfigPath("./config")
@ -41,9 +41,19 @@ func InitialConfig() model.Config {
func getConfig() model.Config { func getConfig() model.Config {
var config model.Config var config model.Config
config.Quality = viper.GetString("img.quality") config.Quality = viper.GetString("images.quality")
config.UserName = viper.GetString("info.username") config.UserName = viper.GetString("user.username")
config.PassWord = viper.GetString("info.password") config.PassWord = viper.GetString("user.password")
config.MsgEnabled = viper.GetBool("message.enabled")
config.MsgType = viper.GetString("message.type")
config.BarkUrl = viper.GetString("message.bark.url") config.BarkUrl = viper.GetString("message.bark.url")
config.BarkKey = viper.GetString("message.bark.key")
config.MailHost = viper.GetString("message.mail.host")
config.MailProtocol = viper.GetString("message.mail.protocol")
config.MailPort = viper.GetInt("message.mail.port")
config.MailUser = viper.GetString("message.mail.username")
config.MailPwd = viper.GetString("message.mail.password")
config.MailFromName = viper.GetString("message.mail.from_name")
config.MailTo = viper.GetStringSlice("message.mail.to")
return config return config
} }

@ -4,6 +4,7 @@ go 1.18
require ( require (
github.com/fsnotify/fsnotify v1.5.4 github.com/fsnotify/fsnotify v1.5.4
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
github.com/sirupsen/logrus v1.9.0 github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.13.0 github.com/spf13/viper v1.13.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0 gopkg.in/natefinch/lumberjack.v2 v2.0.0

@ -123,6 +123,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=

@ -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 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
}

@ -75,8 +75,18 @@ type BarkResp struct {
// @Description: config model // @Description: config model
// //
type Config struct { type Config struct {
Quality string Quality string
UserName string UserName string
PassWord string PassWord string
BarkUrl string MsgEnabled bool
MsgType string
BarkUrl string
BarkKey string
MailHost string
MailProtocol string
MailPort int
MailUser string
MailPwd string
MailFromName string
MailTo []string
} }

@ -3,6 +3,7 @@ package serve
import ( import (
"bytes" "bytes"
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"image" "image"
@ -14,7 +15,7 @@ import (
"io/ioutil" "io/ioutil"
"luxshare-daily-report/global" "luxshare-daily-report/global"
"luxshare-daily-report/serve/core" "luxshare-daily-report/serve/core"
"luxshare-daily-report/util" "luxshare-daily-report/support"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@ -82,7 +83,7 @@ https://github.com/zggsong`))
global.GLO_RECV_CHAN <- m global.GLO_RECV_CHAN <- m
} }
w.Write([]byte("Successful...")) //这个写入到w的是输出到客户端的 w.Write([]byte("服务端接收成功...")) //这个写入到w的是输出到客户端的
} }
// //
@ -95,9 +96,8 @@ func DeclarationService(files map[string]string) {
//ticket := "Q9okHMY42Fk7kzLA3rvPTCbUShhX3zqlbaT97CDjUbxql0NH0AAqKYw+XfSjwoytijuuHXOc7vNY9GePZoIZSg==" //ticket := "Q9okHMY42Fk7kzLA3rvPTCbUShhX3zqlbaT97CDjUbxql0NH0AAqKYw+XfSjwoytijuuHXOc7vNY9GePZoIZSg=="
ticket, err := core.Login(global.GLO_CONFIG.UserName, global.GLO_CONFIG.PassWord) ticket, err := core.Login(global.GLO_CONFIG.UserName, global.GLO_CONFIG.PassWord)
if ticket == "" || err != nil { if ticket == "" || err != nil {
errMsg := fmt.Sprintf("ticket: %v, err:%v", ticket, err.Error()) log.Printf("ticket: %v, err:%v", ticket, err.Error())
log.Printf(errMsg) support.SendMessageError(err)
util.Send(errMsg)
return return
} }
@ -111,10 +111,16 @@ func DeclarationService(files map[string]string) {
m["jkm"] = resJkm m["jkm"] = resJkm
//imagesLinks := []string{"https://p.luxshare-ict.com/KSLANTO/EpidemicSys/20221016/html5_2a05b9ab03844033a63b2c2ac06556ab.jpg", "https://p.luxshare-ict.com/KSLANTO/EpidemicSys/20221016/html5_7e64fd5c643c49299571583163623f7b.jpg"} //imagesLinks := []string{"https://p.luxshare-ict.com/KSLANTO/EpidemicSys/20221016/html5_2a05b9ab03844033a63b2c2ac06556ab.jpg", "https://p.luxshare-ict.com/KSLANTO/EpidemicSys/20221016/html5_7e64fd5c643c49299571583163623f7b.jpg"}
imagesLinks, err := core.Upload2Azure(ticket, m) imagesLinks, err := core.Upload2Azure(ticket, m)
//log.Printf("[DEBUG] images links: %s", imagesLinks) //log.Printf("[DEBUG] get images links: %s", imagesLinks)
if err != nil { if err != nil {
log.Printf(err.Error()) log.Printf(err.Error())
util.Send(err.Error()) support.SendMessageError(err)
return
}
if imagesLinks == nil {
err = errors.New("[ERROR] Get no images links")
log.Printf(err.Error())
support.SendMessageError(err)
return return
} }
@ -123,7 +129,7 @@ func DeclarationService(files map[string]string) {
err = core.EpidemicRegistration(ticket, imagesLinks) err = core.EpidemicRegistration(ticket, imagesLinks)
if err != nil && i > 1 { if err != nil && i > 1 {
log.Printf("重试3次失败%v", err.Error()) log.Printf("重试3次失败%v", err.Error())
util.Send(err.Error()) support.SendMessageError(err)
return return
} else { } else {
break break
@ -139,7 +145,7 @@ func DeclarationService(files map[string]string) {
err = core.RefreshDoor(ticket) err = core.RefreshDoor(ticket)
if err != nil && i > 1 { if err != nil && i > 1 {
log.Printf("重试3次失败%v", err.Error()) log.Printf("重试3次失败%v", err.Error())
util.Send(err.Error()) support.SendMessageError(err)
return return
} else { } else {
break break
@ -147,7 +153,7 @@ func DeclarationService(files map[string]string) {
} }
log.Printf("[INFO] 刷新门禁成功") log.Printf("[INFO] 刷新门禁成功")
util.Send("[INFO] 每日申报+刷新门禁成功") support.SendSuccess("【成功】每日申报、刷新门禁")
} }
// //

@ -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…
Cancel
Save