threema.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "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. // ThreemaNotifier is responsible for sending
  66. // alert notifications to Threema.
  67. type ThreemaNotifier struct {
  68. NotifierBase
  69. GatewayID string
  70. RecipientID string
  71. APISecret string
  72. log log.Logger
  73. }
  74. // NewThreemaNotifier is the constructor for the Threema notifer
  75. func NewThreemaNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
  76. if model.Settings == nil {
  77. return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
  78. }
  79. gatewayID := model.Settings.Get("gateway_id").MustString()
  80. recipientID := model.Settings.Get("recipient_id").MustString()
  81. apiSecret := model.Settings.Get("api_secret").MustString()
  82. // Validation
  83. if gatewayID == "" {
  84. return nil, alerting.ValidationError{Reason: "Could not find Threema Gateway ID in settings"}
  85. }
  86. if !strings.HasPrefix(gatewayID, "*") {
  87. return nil, alerting.ValidationError{Reason: "Invalid Threema Gateway ID: Must start with a *"}
  88. }
  89. if len(gatewayID) != 8 {
  90. return nil, alerting.ValidationError{Reason: "Invalid Threema Gateway ID: Must be 8 characters long"}
  91. }
  92. if recipientID == "" {
  93. return nil, alerting.ValidationError{Reason: "Could not find Threema Recipient ID in settings"}
  94. }
  95. if len(recipientID) != 8 {
  96. return nil, alerting.ValidationError{Reason: "Invalid Threema Recipient ID: Must be 8 characters long"}
  97. }
  98. if apiSecret == "" {
  99. return nil, alerting.ValidationError{Reason: "Could not find Threema API secret in settings"}
  100. }
  101. return &ThreemaNotifier{
  102. NotifierBase: NewNotifierBase(model),
  103. GatewayID: gatewayID,
  104. RecipientID: recipientID,
  105. APISecret: apiSecret,
  106. log: log.New("alerting.notifier.threema"),
  107. }, nil
  108. }
  109. // Notify send an alert notification to Threema
  110. func (notifier *ThreemaNotifier) Notify(evalContext *alerting.EvalContext) error {
  111. notifier.log.Info("Sending alert notification from", "threema_id", notifier.GatewayID)
  112. notifier.log.Info("Sending alert notification to", "threema_id", notifier.RecipientID)
  113. // Set up basic API request data
  114. data := url.Values{}
  115. data.Set("from", notifier.GatewayID)
  116. data.Set("to", notifier.RecipientID)
  117. data.Set("secret", notifier.APISecret)
  118. // Determine emoji
  119. stateEmoji := ""
  120. switch evalContext.Rule.State {
  121. case models.AlertStateOK:
  122. stateEmoji = "\u2705 " // White Heavy Check Mark
  123. case models.AlertStateNoData:
  124. stateEmoji = "\u2753 " // Black Question Mark Ornament
  125. case models.AlertStateAlerting:
  126. stateEmoji = "\u26A0 " // Warning sign
  127. }
  128. // Build message
  129. message := fmt.Sprintf("%s%s\n\n*State:* %s\n*Message:* %s\n",
  130. stateEmoji, evalContext.GetNotificationTitle(),
  131. evalContext.Rule.Name, evalContext.Rule.Message)
  132. ruleURL, err := evalContext.GetRuleURL()
  133. if err == nil {
  134. message = message + fmt.Sprintf("*URL:* %s\n", ruleURL)
  135. }
  136. if evalContext.ImagePublicURL != "" {
  137. message = message + fmt.Sprintf("*Image:* %s\n", evalContext.ImagePublicURL)
  138. }
  139. data.Set("text", message)
  140. // Prepare and send request
  141. url := fmt.Sprintf(threemaGwBaseURL, "send_simple")
  142. body := data.Encode()
  143. headers := map[string]string{
  144. "Content-Type": "application/x-www-form-urlencoded",
  145. }
  146. cmd := &models.SendWebhookSync{
  147. Url: url,
  148. Body: body,
  149. HttpMethod: "POST",
  150. HttpHeader: headers,
  151. }
  152. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  153. notifier.log.Error("Failed to send webhook", "error", err, "webhook", notifier.Name)
  154. return err
  155. }
  156. return nil
  157. }