notify

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Channel

type Channel struct {
	Slack    []string `yaml:"slack,omitempty"`    // Slack channel IDs
	Pushover []string `yaml:"pushover,omitempty"` // Pushover user keys
	Webhook  []string `yaml:"webhook,omitempty"`  // Webhook URLs
}

Channel specifies which concrete channels to deliver to.

type Config

type Config struct {
	Slack    *SlackConfig    `yaml:"slack,omitempty"`
	Pushover *PushoverConfig `yaml:"pushover,omitempty"`
	Webhook  *WebhookConfig  `yaml:"webhook,omitempty"`
}

Config is the root notification service configuration.

type NotifyService

type NotifyService struct {
	// contains filtered or unexported fields
}

NotifyService 统一通知服务 使用异步发送模式,Send方法将消息放入channel,由消费goroutine实际发送

func NewNotifyService

func NewNotifyService(cfg *Config) (*NotifyService, error)

NewNotifyService 创建通知服务 创建后需要调用Start()启动消费goroutine

func (*NotifyService) Send

func (n *NotifyService) Send(channel *Channel, message string) error

Send 异步发送消息到指定渠道 将消息放入channel,由消费goroutine实际发送,不阻塞调用方

func (*NotifyService) SendWithPriority

func (n *NotifyService) SendWithPriority(channel *Channel, message string, priority int, sound string) error

SendWithPriority 异步发送消息到指定渠道,支持 Pushover 的优先级和声音配置

func (*NotifyService) Start

func (n *NotifyService) Start(ctx context.Context)

Start 启动消费goroutine

func (*NotifyService) Stop

func (n *NotifyService) Stop()

Stop 停止服务,等待所有消息发送完成

type PushoverClient

type PushoverClient struct {
	// contains filtered or unexported fields
}

PushoverClient Pushover API客户端

func NewPushoverClient

func NewPushoverClient(appToken string, retry, expire, maxRetries, retryDelay int) (*PushoverClient, error)

NewPushoverClient 创建Pushover客户端

func (*PushoverClient) SendEmergencyNotification

func (p *PushoverClient) SendEmergencyNotification(userKey, message string) error

SendEmergencyNotification 发送紧急通知(保持向后兼容)

func (*PushoverClient) SendNotification

func (p *PushoverClient) SendNotification(userKey, message string, priority int, sound string) error

SendNotification 发送通知(支持自定义优先级和声音),带指数退避重试

func (*PushoverClient) SendToUsers

func (p *PushoverClient) SendToUsers(userKeys []string, message string, priority int, sound string) error

SendToUsers 向多个Pushover用户发送通知

type PushoverConfig

type PushoverConfig struct {
	Enabled    bool   `yaml:"enabled"`
	AppToken   string `yaml:"app_token"`
	Retry      int    `yaml:"retry"`
	Expire     int    `yaml:"expire"`
	MaxRetries int    `yaml:"max_retries"`
	RetryDelay int    `yaml:"retry_delay"`
}

PushoverConfig Pushover配置

type PushoverRequest

type PushoverRequest struct {
	Token    string `json:"token"`
	User     string `json:"user"`
	Message  string `json:"message"`
	Priority int    `json:"priority"`
	Sound    string `json:"sound"`
	Retry    int    `json:"retry"`
	Expire   int    `json:"expire"`
}

PushoverRequest Pushover API请求

type PushoverResponse

type PushoverResponse struct {
	Status  int      `json:"status"`
	Request string   `json:"request"`
	Errors  []string `json:"errors,omitempty"`
}

PushoverResponse Pushover API响应

type SlackClient

type SlackClient struct {
	// contains filtered or unexported fields
}

SlackClient Slack API客户端

func NewSlackClient

func NewSlackClient(botToken string) (*SlackClient, error)

NewSlackClient 创建Slack客户端

func (*SlackClient) FindUserByName

func (s *SlackClient) FindUserByName(username string) (string, error)

FindUserByName 通过用户名查找用户ID

func (*SlackClient) GetChannelMembers

func (s *SlackClient) GetChannelMembers(channelID string) ([]string, error)

GetChannelMembers 获取频道成员列表(排除bot)

func (*SlackClient) GetUserInfo

func (s *SlackClient) GetUserInfo(userID string) (*UserInfo, error)

GetUserInfo 获取用户信息

func (*SlackClient) PostMessage

func (s *SlackClient) PostMessage(channelID, message string) error

PostMessage 发送消息到指定频道

func (*SlackClient) ReplyToChannel

func (s *SlackClient) ReplyToChannel(responseURL, message, responseType string) error

ReplyToChannel 回复消息到频道

func (*SlackClient) SendToChannels

func (s *SlackClient) SendToChannels(channelIDs []string, message string) error

SendToChannels 向多个Slack频道发送消息

type SlackConfig

type SlackConfig struct {
	Enabled  bool   `yaml:"enabled"`
	BotToken string `yaml:"bot_token"`
}

SlackConfig Slack配置

type UserInfo

type UserInfo struct {
	ID       string
	Name     string
	RealName string
}

UserInfo Slack用户信息

type WebhookClient

type WebhookClient struct {
	// contains filtered or unexported fields
}

WebhookClient sends notifications to generic HTTP webhook endpoints.

func NewWebhookClient

func NewWebhookClient(timeout time.Duration, headers map[string]string) (*WebhookClient, error)

NewWebhookClient creates a WebhookClient with the given timeout and optional custom headers (e.g. Authorization tokens).

func (*WebhookClient) SendToURLs

func (w *WebhookClient) SendToURLs(urls []string, message string) error

SendToURLs posts the message to every URL. It returns an error only when all URLs fail; partial failures are logged but do not block the rest.

Security note (SSRF): Webhook URLs are currently sourced from the config file and controlled by the server administrator, so SSRF risk is low. If webhook URLs are ever exposed via API (user-configurable), the following protections MUST be added:

  • Validate URL scheme (allow only http/https, reject file://, gopher://, etc.)
  • Resolve hostname and block private/reserved IP ranges: 127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 (ip.IsPrivate) 169.254.169.254 (cloud metadata endpoint, ip.IsLinkLocalUnicast) ::1, fc00::/7 (IPv6 loopback/private)
  • Disable HTTP redirects (attacker can redirect to internal IPs)
  • Consider DNS rebinding protection (re-resolve after redirect)

type WebhookConfig

type WebhookConfig struct {
	Enabled bool              `yaml:"enabled"`
	Headers map[string]string `yaml:"headers,omitempty"` // e.g. Authorization: Bearer ...
	Timeout time.Duration     `yaml:"timeout,omitempty"` // HTTP client timeout
}

WebhookConfig configures the generic webhook notification channel.

type WebhookPayload

type WebhookPayload struct {
	Text      string `json:"text"`
	Timestamp string `json:"timestamp"`
}

WebhookPayload is the JSON body posted to each webhook URL.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL