line.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package notifiers
  2. import (
  3. "fmt"
  4. "net/url"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/infra/log"
  7. "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/alerting"
  9. )
  10. func init() {
  11. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  12. Type: "LINE",
  13. Name: "LINE",
  14. Description: "Send notifications to LINE notify",
  15. Factory: NewLINENotifier,
  16. OptionsTemplate: `
  17. <div class="gf-form-group">
  18. <h3 class="page-heading">LINE notify settings</h3>
  19. <div class="gf-form">
  20. <span class="gf-form-label width-14">Token</span>
  21. <input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.token" placeholder="LINE notify token key"></input>
  22. </div>
  23. </div>
  24. `,
  25. })
  26. }
  27. const (
  28. lineNotifyURL string = "https://notify-api.line.me/api/notify"
  29. )
  30. // NewLINENotifier is the constructor for the LINE notifier
  31. func NewLINENotifier(model *models.AlertNotification) (alerting.Notifier, error) {
  32. token := model.Settings.Get("token").MustString()
  33. if token == "" {
  34. return nil, alerting.ValidationError{Reason: "Could not find token in settings"}
  35. }
  36. return &LineNotifier{
  37. NotifierBase: NewNotifierBase(model),
  38. Token: token,
  39. log: log.New("alerting.notifier.line"),
  40. }, nil
  41. }
  42. // LineNotifier is responsible for sending
  43. // alert notifications to LINE.
  44. type LineNotifier struct {
  45. NotifierBase
  46. Token string
  47. log log.Logger
  48. }
  49. // Notify send an alert notification to LINE
  50. func (ln *LineNotifier) Notify(evalContext *alerting.EvalContext) error {
  51. ln.log.Info("Executing line notification", "ruleId", evalContext.Rule.ID, "notification", ln.Name)
  52. var err error
  53. switch evalContext.Rule.State {
  54. case models.AlertStateAlerting:
  55. err = ln.createAlert(evalContext)
  56. }
  57. return err
  58. }
  59. func (ln *LineNotifier) createAlert(evalContext *alerting.EvalContext) error {
  60. ln.log.Info("Creating Line notify", "ruleId", evalContext.Rule.ID, "notification", ln.Name)
  61. ruleURL, err := evalContext.GetRuleURL()
  62. if err != nil {
  63. ln.log.Error("Failed get rule link", "error", err)
  64. return err
  65. }
  66. form := url.Values{}
  67. body := fmt.Sprintf("%s - %s\n%s", evalContext.Rule.Name, ruleURL, evalContext.Rule.Message)
  68. form.Add("message", body)
  69. if evalContext.ImagePublicURL != "" {
  70. form.Add("imageThumbnail", evalContext.ImagePublicURL)
  71. form.Add("imageFullsize", evalContext.ImagePublicURL)
  72. }
  73. cmd := &models.SendWebhookSync{
  74. Url: lineNotifyURL,
  75. HttpMethod: "POST",
  76. HttpHeader: map[string]string{
  77. "Authorization": fmt.Sprintf("Bearer %s", ln.Token),
  78. "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
  79. },
  80. Body: form.Encode(),
  81. }
  82. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  83. ln.log.Error("Failed to send notification to LINE", "error", err, "body", body)
  84. return err
  85. }
  86. return nil
  87. }