|
@@ -0,0 +1,133 @@
|
|
|
+package services
|
|
|
+
|
|
|
+import (
|
|
|
+ "github.com/blinkbean/dingtalk"
|
|
|
+ "gopkg.in/gomail.v2"
|
|
|
+ "hongze/hongze_robot/utils"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+//projectName-项目名称
|
|
|
+//runMode-运行模式
|
|
|
+//msgBody-消息内容
|
|
|
+//level:消息基本,1:提示消息,2:警告消息,3:严重错误信息,默认为1 提示消息
|
|
|
+
|
|
|
+//钉钉机器人
|
|
|
+func SendAlarmMsgByDingTalk(projectName, runMode, msgBody string, level int) {
|
|
|
+ if level <= 0 {
|
|
|
+ level = 1
|
|
|
+ }
|
|
|
+ time.Sleep(time.Second)
|
|
|
+ var err error
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "SendSms Err:"+err.Error(), utils.EmailSendToUsers)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ var dingToken = []string{}
|
|
|
+
|
|
|
+ dingToken = []string{
|
|
|
+ "c0ff5de65a46c8a340c9846dcae6f182782247dbd910f4fcfa0758e12c28a030",
|
|
|
+ "84780b28c4cd6f2ca3edd650e068cd56a4ab99fc78003bdd0983c35b573b3e75",
|
|
|
+ "dbdb7f0650e920740c952bfc29d9217d8dbbfc3d949dc8bd25ad8ed744ca1b40",
|
|
|
+ }
|
|
|
+
|
|
|
+ smsDateTime := time.Now().Format(utils.FormatDateTime)
|
|
|
+ var fontColor string
|
|
|
+ if level == 1 {
|
|
|
+ fontColor = `#008000`
|
|
|
+ } else if level == 2 {
|
|
|
+ fontColor = `#FF8C00`
|
|
|
+ } else {
|
|
|
+ fontColor = `#FF0000`
|
|
|
+ }
|
|
|
+ title := projectName + "【" + runMode + "】"
|
|
|
+ if runMode == "" {
|
|
|
+ title = projectName
|
|
|
+ }
|
|
|
+ msg := []string{
|
|
|
+ "### " + title,
|
|
|
+ "---",
|
|
|
+ "- <font color=" + fontColor + ">" + msgBody + "</font>",
|
|
|
+ "- " + smsDateTime,
|
|
|
+ }
|
|
|
+ cli := dingtalk.InitDingTalk(dingToken, "报警")
|
|
|
+ //cli.SendTextMessage("content", dingtalk.WithAtAll())
|
|
|
+ err = cli.SendMarkDownMessageBySlice("华瑞信息", msg)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+var (
|
|
|
+ emails = []struct {
|
|
|
+ address string
|
|
|
+ authCode string
|
|
|
+ }{
|
|
|
+ {
|
|
|
+ address: "qunao007@qq.com",
|
|
|
+ authCode: "cyhjgffhaobteihd",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ address: "2370889154@qq.com",
|
|
|
+ authCode: "pfujkfwyqaprdjgb",
|
|
|
+ },
|
|
|
+ }
|
|
|
+)
|
|
|
+
|
|
|
+//邮件
|
|
|
+func SendAlarmMsgByEmail(projectName, runMode, msgBody, touser string, level int) bool {
|
|
|
+ var arr []string
|
|
|
+ sub := strings.Index(touser, ";")
|
|
|
+ if sub >= 0 {
|
|
|
+ spArr := strings.Split(touser, ";")
|
|
|
+ for _, v := range spArr {
|
|
|
+ arr = append(arr, v)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ arr = append(arr, touser)
|
|
|
+ }
|
|
|
+ rand := utils.GetRandInt(1, len(emails)+1)
|
|
|
+ emailObj := emails[rand-1]
|
|
|
+
|
|
|
+ emailAddress := emailObj.address
|
|
|
+ emailAuthCode := emailObj.authCode
|
|
|
+
|
|
|
+ var fontColor string
|
|
|
+ if level == 1 {
|
|
|
+ fontColor = `#008000`
|
|
|
+ } else if level == 2 {
|
|
|
+ fontColor = `#FF8C00`
|
|
|
+ } else {
|
|
|
+ fontColor = `#FF0000`
|
|
|
+ }
|
|
|
+
|
|
|
+ msgBody = "<div>" + "</div>"
|
|
|
+
|
|
|
+ body := `<!DOCTYPE html>
|
|
|
+ <html lang="en">
|
|
|
+ <head>
|
|
|
+ <meta charset="UTF-8">
|
|
|
+ <title>Title</title>
|
|
|
+ </head>
|
|
|
+ <body>
|
|
|
+ <div style="color: ` + fontColor + `">
|
|
|
+ ` + msgBody + `
|
|
|
+ </div>
|
|
|
+ </body>
|
|
|
+ </html>`
|
|
|
+
|
|
|
+ title := projectName + "【" + runMode + "】"
|
|
|
+ m := gomail.NewMessage()
|
|
|
+ m.SetHeader("From", emailAddress)
|
|
|
+ m.SetHeader("To", arr...)
|
|
|
+ m.SetHeader("Subject", title+" "+utils.GetRandStringNoSpecialChar(10))
|
|
|
+ m.SetBody("text/html", body)
|
|
|
+ d := gomail.NewDialer("smtp.qq.com", 587, emailAddress, emailAuthCode)
|
|
|
+ if err := d.DialAndSend(m); err != nil {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|