telegram.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package notifiers
  2. import (
  3. "fmt"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. "github.com/grafana/grafana/pkg/log"
  7. "github.com/grafana/grafana/pkg/metrics"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/alerting"
  10. )
  11. var (
  12. telegeramApiUrl string = "https://api.telegram.org/bot%s/%s"
  13. )
  14. func init() {
  15. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  16. Type: "telegram",
  17. Name: "Telegram",
  18. Description: "Sends notifications to Telegram",
  19. Factory: NewTelegramNotifier,
  20. OptionsTemplate: `
  21. <h3 class="page-heading">Telegram API settings</h3>
  22. <div class="gf-form">
  23. <span class="gf-form-label width-9">BOT API Token</span>
  24. <input type="text" required
  25. class="gf-form-input"
  26. ng-model="ctrl.model.settings.bottoken"
  27. placeholder="Telegram BOT API Token"></input>
  28. </div>
  29. <div class="gf-form">
  30. <span class="gf-form-label width-9">Chat ID</span>
  31. <input type="text" required
  32. class="gf-form-input"
  33. ng-model="ctrl.model.settings.chatid"
  34. data-placement="right">
  35. </input>
  36. <info-popover mode="right-absolute">
  37. Integer Telegram Chat Identifier
  38. </info-popover>
  39. </div>
  40. `,
  41. })
  42. }
  43. type TelegramNotifier struct {
  44. NotifierBase
  45. BotToken string
  46. ChatID string
  47. log log.Logger
  48. }
  49. func NewTelegramNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  50. if model.Settings == nil {
  51. return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
  52. }
  53. botToken := model.Settings.Get("bottoken").MustString()
  54. chatId := model.Settings.Get("chatid").MustString()
  55. if botToken == "" {
  56. return nil, alerting.ValidationError{Reason: "Could not find Bot Token in settings"}
  57. }
  58. if chatId == "" {
  59. return nil, alerting.ValidationError{Reason: "Could not find Chat Id in settings"}
  60. }
  61. return &TelegramNotifier{
  62. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  63. BotToken: botToken,
  64. ChatID: chatId,
  65. log: log.New("alerting.notifier.telegram"),
  66. }, nil
  67. }
  68. func (this *TelegramNotifier) Notify(evalContext *alerting.EvalContext) error {
  69. this.log.Info("Sending alert notification to", "bot_token", this.BotToken)
  70. this.log.Info("Sending alert notification to", "chat_id", this.ChatID)
  71. metrics.M_Alerting_Notification_Sent_Telegram.Inc(1)
  72. bodyJSON := simplejson.New()
  73. bodyJSON.Set("chat_id", this.ChatID)
  74. bodyJSON.Set("parse_mode", "html")
  75. message := fmt.Sprintf("<b>%s</b>\nState: %s\nMessage: %s\n", evalContext.GetNotificationTitle(), evalContext.Rule.Name, evalContext.Rule.Message)
  76. ruleUrl, err := evalContext.GetRuleUrl()
  77. if err == nil {
  78. message = message + fmt.Sprintf("URL: %s\n", ruleUrl)
  79. }
  80. if evalContext.ImagePublicUrl != "" {
  81. message = message + fmt.Sprintf("Image: %s\n", evalContext.ImagePublicUrl)
  82. }
  83. metrics := ""
  84. fieldLimitCount := 4
  85. for index, evt := range evalContext.EvalMatches {
  86. metrics += fmt.Sprintf("\n%s: %s", evt.Metric, evt.Value)
  87. if index > fieldLimitCount {
  88. break
  89. }
  90. }
  91. if metrics != "" {
  92. message = message + fmt.Sprintf("\n<i>Metrics:</i>%s", metrics)
  93. }
  94. bodyJSON.Set("text", message)
  95. url := fmt.Sprintf(telegeramApiUrl, this.BotToken, "sendMessage")
  96. body, _ := bodyJSON.MarshalJSON()
  97. cmd := &m.SendWebhookSync{
  98. Url: url,
  99. Body: string(body),
  100. HttpMethod: "POST",
  101. }
  102. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  103. this.log.Error("Failed to send webhook", "error", err, "webhook", this.Name)
  104. return err
  105. }
  106. return nil
  107. }