telegram.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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: NewOpsGenieNotifier,
  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("%s\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. bodyJSON.Set("text", message)
  81. url := fmt.Sprintf(telegeramApiUrl, this.BotToken, "sendMessage")
  82. body, _ := bodyJSON.MarshalJSON()
  83. cmd := &m.SendWebhookSync{
  84. Url: url,
  85. Body: string(body),
  86. HttpMethod: "POST",
  87. }
  88. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  89. this.log.Error("Failed to send webhook", "error", err, "webhook", this.Name)
  90. return err
  91. }
  92. return nil
  93. }