alarm_msg.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package services
  2. import (
  3. "github.com/blinkbean/dingtalk"
  4. "gopkg.in/gomail.v2"
  5. "hongze/hongze_robot/utils"
  6. "strings"
  7. "time"
  8. )
  9. //projectName-项目名称
  10. //runMode-运行模式
  11. //msgBody-消息内容
  12. //level:消息基本,1:提示消息,2:警告消息,3:严重错误信息,默认为1 提示消息
  13. //钉钉机器人
  14. func SendAlarmMsgByDingTalkRelease(projectName, runMode, msgBody string, level int) {
  15. if level <= 0 {
  16. level = 1
  17. }
  18. time.Sleep(time.Second)
  19. var err error
  20. defer func() {
  21. if err != nil {
  22. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "SendSms Err:"+err.Error(), utils.EmailSendToUsers)
  23. }
  24. }()
  25. var dingToken = []string{}
  26. dingToken = []string{
  27. "c0ff5de65a46c8a340c9846dcae6f182782247dbd910f4fcfa0758e12c28a030",
  28. "84780b28c4cd6f2ca3edd650e068cd56a4ab99fc78003bdd0983c35b573b3e75",
  29. "dbdb7f0650e920740c952bfc29d9217d8dbbfc3d949dc8bd25ad8ed744ca1b40",
  30. }
  31. smsDateTime := time.Now().Format(utils.FormatDateTime)
  32. var fontColor string
  33. if level == 1 {
  34. fontColor = `#008000`
  35. } else if level == 2 {
  36. fontColor = `#FF8C00`
  37. } else {
  38. fontColor = `#FF0000`
  39. }
  40. title := projectName + "【" + runMode + "】"
  41. if runMode == "" {
  42. title = projectName
  43. }
  44. msg := []string{
  45. "### " + title,
  46. "---",
  47. "- <font color=" + fontColor + ">" + msgBody + "</font>",
  48. "- " + smsDateTime,
  49. }
  50. cli := dingtalk.InitDingTalk(dingToken, "报警")
  51. //cli.SendTextMessage("content", dingtalk.WithAtAll())
  52. err = cli.SendMarkDownMessageBySlice(utils.APPNAME, msg)
  53. if err != nil {
  54. return
  55. }
  56. }
  57. //钉钉机器人
  58. func SendAlarmMsgByDingTalkDebug(projectName, runMode, msgBody string, level int) {
  59. if level <= 0 {
  60. level = 1
  61. }
  62. time.Sleep(time.Second)
  63. var err error
  64. defer func() {
  65. if err != nil {
  66. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "SendSms Err:"+err.Error(), utils.EmailSendToUsers)
  67. }
  68. }()
  69. var dingToken = []string{}
  70. dingToken = []string{
  71. "8b484441ec606d4c62a8036aa9c5ccd0dd88a995aef1a97b65d51d401c786f45",
  72. "ae88f1bcbe44e837dd73927f07af84a7f9a7c7be0b04e36089f8bc858ed23bab",
  73. }
  74. smsDateTime := time.Now().Format(utils.FormatDateTime)
  75. var fontColor string
  76. if level == 1 {
  77. fontColor = `#008000`
  78. } else if level == 2 {
  79. fontColor = `#FF8C00`
  80. } else {
  81. fontColor = `#FF0000`
  82. }
  83. title := projectName + "【" + runMode + "】"
  84. if runMode == "" {
  85. title = projectName
  86. }
  87. msg := []string{
  88. "### " + title,
  89. "---",
  90. "- <font color=" + fontColor + ">" + msgBody + "</font>",
  91. "- " + smsDateTime,
  92. }
  93. cli := dingtalk.InitDingTalk(dingToken, "报警")
  94. //cli.SendTextMessage("content", dingtalk.WithAtAll())
  95. err = cli.SendMarkDownMessageBySlice(utils.APPNAME, msg)
  96. if err != nil {
  97. return
  98. }
  99. }
  100. var (
  101. emails = []struct {
  102. address string
  103. authCode string
  104. }{
  105. {
  106. address: "qunao007@qq.com",
  107. authCode: "cyhjgffhaobteihd",
  108. },
  109. {
  110. address: "2370889154@qq.com",
  111. authCode: "pfujkfwyqaprdjgb",
  112. },
  113. }
  114. )
  115. //邮件
  116. func SendAlarmMsgByEmail(projectName, runMode, msgBody, touser string, level int) bool {
  117. var arr []string
  118. sub := strings.Index(touser, ";")
  119. if sub >= 0 {
  120. spArr := strings.Split(touser, ";")
  121. for _, v := range spArr {
  122. arr = append(arr, v)
  123. }
  124. } else {
  125. arr = append(arr, touser)
  126. }
  127. rand := utils.GetRandInt(1, len(emails)+1)
  128. emailObj := emails[rand-1]
  129. emailAddress := emailObj.address
  130. emailAuthCode := emailObj.authCode
  131. var fontColor string
  132. if level == 1 {
  133. fontColor = `#008000`
  134. } else if level == 2 {
  135. fontColor = `#FF8C00`
  136. } else {
  137. fontColor = `#FF0000`
  138. }
  139. body := `<!DOCTYPE html>
  140. <html lang="en">
  141. <head>
  142. <meta charset="UTF-8">
  143. <title>Title</title>
  144. </head>
  145. <body>
  146. <div style="color: ` + fontColor + `">
  147. ` + msgBody + `
  148. </div>
  149. </body>
  150. </html>`
  151. title := projectName + "【" + runMode + "】"
  152. m := gomail.NewMessage()
  153. m.SetHeader("From", emailAddress)
  154. m.SetHeader("To", arr...)
  155. m.SetHeader("Subject", title+" "+utils.GetRandStringNoSpecialChar(10))
  156. m.SetBody("text/html", body)
  157. d := gomail.NewDialer("smtp.qq.com", 587, emailAddress, emailAuthCode)
  158. if err := d.DialAndSend(m); err != nil {
  159. return false
  160. }
  161. return true
  162. }