threema.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package notifiers
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strings"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/infra/log"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/alerting"
  10. )
  11. var (
  12. threemaGwBaseURL = "https://msgapi.threema.ch/%s"
  13. )
  14. func init() {
  15. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  16. Type: "threema",
  17. Name: "Threema Gateway",
  18. Description: "Sends notifications to Threema using the Threema Gateway",
  19. Factory: NewThreemaNotifier,
  20. OptionsTemplate: `
  21. <h3 class="page-heading">Threema Gateway settings</h3>
  22. <p>
  23. Notifications can be configured for any Threema Gateway ID of type
  24. "Basic". End-to-End IDs are not currently supported.
  25. </p>
  26. <p>
  27. The Threema Gateway ID can be set up at
  28. <a href="https://gateway.threema.ch/" target="_blank" rel="noopener noreferrer">https://gateway.threema.ch/</a>.
  29. </p>
  30. <div class="gf-form">
  31. <span class="gf-form-label width-14">Gateway ID</span>
  32. <input type="text" required maxlength="8" pattern="\*[0-9A-Z]{7}"
  33. class="gf-form-input max-width-14"
  34. ng-model="ctrl.model.settings.gateway_id"
  35. placeholder="*3MAGWID">
  36. </input>
  37. <info-popover mode="right-normal">
  38. Your 8 character Threema Gateway ID (starting with a *)
  39. </info-popover>
  40. </div>
  41. <div class="gf-form">
  42. <span class="gf-form-label width-14">Recipient ID</span>
  43. <input type="text" required maxlength="8" pattern="[0-9A-Z]{8}"
  44. class="gf-form-input max-width-14"
  45. ng-model="ctrl.model.settings.recipient_id"
  46. placeholder="YOUR3MID">
  47. </input>
  48. <info-popover mode="right-normal">
  49. The 8 character Threema ID that should receive the alerts
  50. </info-popover>
  51. </div>
  52. <div class="gf-form">
  53. <span class="gf-form-label width-14">API Secret</span>
  54. <input type="text" required
  55. class="gf-form-input max-width-24"
  56. ng-model="ctrl.model.settings.api_secret">
  57. </input>
  58. <info-popover mode="right-normal">
  59. Your Threema Gateway API secret
  60. </info-popover>
  61. </div>
  62. `,
  63. })
  64. }
  65. type ThreemaNotifier struct {
  66. NotifierBase
  67. GatewayID string
  68. RecipientID string
  69. APISecret string
  70. log log.Logger
  71. }
  72. func NewThreemaNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  73. if model.Settings == nil {
  74. return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
  75. }
  76. gatewayID := model.Settings.Get("gateway_id").MustString()
  77. recipientID := model.Settings.Get("recipient_id").MustString()
  78. apiSecret := model.Settings.Get("api_secret").MustString()
  79. // Validation
  80. if gatewayID == "" {
  81. return nil, alerting.ValidationError{Reason: "Could not find Threema Gateway ID in settings"}
  82. }
  83. if !strings.HasPrefix(gatewayID, "*") {
  84. return nil, alerting.ValidationError{Reason: "Invalid Threema Gateway ID: Must start with a *"}
  85. }
  86. if len(gatewayID) != 8 {
  87. return nil, alerting.ValidationError{Reason: "Invalid Threema Gateway ID: Must be 8 characters long"}
  88. }
  89. if recipientID == "" {
  90. return nil, alerting.ValidationError{Reason: "Could not find Threema Recipient ID in settings"}
  91. }
  92. if len(recipientID) != 8 {
  93. return nil, alerting.ValidationError{Reason: "Invalid Threema Recipient ID: Must be 8 characters long"}
  94. }
  95. if apiSecret == "" {
  96. return nil, alerting.ValidationError{Reason: "Could not find Threema API secret in settings"}
  97. }
  98. return &ThreemaNotifier{
  99. NotifierBase: NewNotifierBase(model),
  100. GatewayID: gatewayID,
  101. RecipientID: recipientID,
  102. APISecret: apiSecret,
  103. log: log.New("alerting.notifier.threema"),
  104. }, nil
  105. }
  106. func (notifier *ThreemaNotifier) Notify(evalContext *alerting.EvalContext) error {
  107. notifier.log.Info("Sending alert notification from", "threema_id", notifier.GatewayID)
  108. notifier.log.Info("Sending alert notification to", "threema_id", notifier.RecipientID)
  109. // Set up basic API request data
  110. data := url.Values{}
  111. data.Set("from", notifier.GatewayID)
  112. data.Set("to", notifier.RecipientID)
  113. data.Set("secret", notifier.APISecret)
  114. // Determine emoji
  115. stateEmoji := ""
  116. switch evalContext.Rule.State {
  117. case m.AlertStateOK:
  118. stateEmoji = "\u2705 " // White Heavy Check Mark
  119. case m.AlertStateNoData:
  120. stateEmoji = "\u2753 " // Black Question Mark Ornament
  121. case m.AlertStateAlerting:
  122. stateEmoji = "\u26A0 " // Warning sign
  123. }
  124. // Build message
  125. message := fmt.Sprintf("%s%s\n\n*State:* %s\n*Message:* %s\n",
  126. stateEmoji, evalContext.GetNotificationTitle(),
  127. evalContext.Rule.Name, evalContext.Rule.Message)
  128. ruleURL, err := evalContext.GetRuleUrl()
  129. if err == nil {
  130. message = message + fmt.Sprintf("*URL:* %s\n", ruleURL)
  131. }
  132. if evalContext.ImagePublicUrl != "" {
  133. message = message + fmt.Sprintf("*Image:* %s\n", evalContext.ImagePublicUrl)
  134. }
  135. data.Set("text", message)
  136. // Prepare and send request
  137. url := fmt.Sprintf(threemaGwBaseURL, "send_simple")
  138. body := data.Encode()
  139. headers := map[string]string{
  140. "Content-Type": "application/x-www-form-urlencoded",
  141. }
  142. cmd := &m.SendWebhookSync{
  143. Url: url,
  144. Body: body,
  145. HttpMethod: "POST",
  146. HttpHeader: headers,
  147. }
  148. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  149. notifier.log.Error("Failed to send webhook", "error", err, "webhook", notifier.Name)
  150. return err
  151. }
  152. return nil
  153. }