123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- 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 SendAlarmMsgByDingTalkRelease(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(utils.APPNAME, msg)
- if err != nil {
- return
- }
- }
- //钉钉机器人
- func SendAlarmMsgByDingTalkDebug(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{
- "8b484441ec606d4c62a8036aa9c5ccd0dd88a995aef1a97b65d51d401c786f45",
- "ae88f1bcbe44e837dd73927f07af84a7f9a7c7be0b04e36089f8bc858ed23bab",
- }
- 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(utils.APPNAME, 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`
- }
- 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
- }
|