core

package
v0.26.6 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPlatform = errors.New("invalid or empty platform")
	ErrInvalidURL      = errors.New("invalid or empty target URL")
	ErrSenderNotFound  = errors.New("sender not found for platform")
	ErrSendFailed      = errors.New("failed to send webhook")
	ErrInvalidConfig   = errors.New("invalid webhook configuration")
)

Common webhook errors

Functions

func GetRegisteredPlatforms

func GetRegisteredPlatforms() []string

GetRegisteredPlatforms returns a list of all registered platform names.

func RegisterAdapter

func RegisterAdapter(platform string, adapter PlatformAdapter)

RegisterAdapter registers a platform adapter.

func RegisterAllAdapters

func RegisterAllAdapters()

RegisterAllAdapters registers all platform adapters.

Types

type DefaultAdapter

type DefaultAdapter struct{}

DefaultAdapter implements PlatformAdapter for custom/generic webhooks.

func (*DefaultAdapter) FormatMessage

func (d *DefaultAdapter) FormatMessage(msg, raw string, config *WebhookConfig) ([]byte, error)

func (*DefaultAdapter) GetContentType

func (d *DefaultAdapter) GetContentType() string

func (*DefaultAdapter) Name

func (d *DefaultAdapter) Name() string

func (*DefaultAdapter) SignRequest

func (d *DefaultAdapter) SignRequest(baseURL string, body []byte, secret string) (string, error)

type DingtalkAdapter

type DingtalkAdapter struct{}

DingtalkAdapter implements PlatformAdapter for DingTalk webhooks.

func (*DingtalkAdapter) FormatMessage

func (d *DingtalkAdapter) FormatMessage(msg, raw string, config *WebhookConfig) ([]byte, error)

func (*DingtalkAdapter) GetContentType

func (d *DingtalkAdapter) GetContentType() string

func (*DingtalkAdapter) Name

func (d *DingtalkAdapter) Name() string

func (*DingtalkAdapter) SignRequest

func (d *DingtalkAdapter) SignRequest(baseURL string, body []byte, secret string) (string, error)

type FeishuAdapter

type FeishuAdapter struct{}

FeishuAdapter implements PlatformAdapter for Feishu webhooks.

func (*FeishuAdapter) FormatMessage

func (f *FeishuAdapter) FormatMessage(msg, raw string, config *WebhookConfig) ([]byte, error)

func (*FeishuAdapter) GetContentType

func (f *FeishuAdapter) GetContentType() string

func (*FeishuAdapter) Name

func (f *FeishuAdapter) Name() string

func (*FeishuAdapter) SignRequest

func (f *FeishuAdapter) SignRequest(baseURL string, body []byte, secret string) (string, error)

type HTTPRequestLog

type HTTPRequestLog struct {
	Timestamp   time.Time         `json:"timestamp"`
	Method      string            `json:"method"`
	URL         string            `json:"url"`
	Headers     map[string]string `json:"headers"`
	Body        string            `json:"body"`
	BodySize    int               `json:"body_size"`
	WebhookName string            `json:"webhook_name"`
	ReceiverID  string            `json:"receiver_id,omitempty"`
}

HTTPRequestLog 记录HTTP请求的详细信息

type HTTPResponseLog

type HTTPResponseLog struct {
	Timestamp    time.Time         `json:"timestamp"`
	StatusCode   int               `json:"status_code"`
	Status       string            `json:"status"`
	Headers      map[string]string `json:"headers"`
	Body         string            `json:"body"`
	BodySize     int               `json:"body_size"`
	Duration     time.Duration     `json:"duration"`
	Success      bool              `json:"success"`
	ErrorMessage string            `json:"error_message,omitempty"`
}

HTTPResponseLog 记录HTTP响应的详细信息

type LoggedHTTPClient

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

LoggedHTTPClient 带日志记录功能的HTTP客户端包装器

func NewLoggedHTTPClient

func NewLoggedHTTPClient(timeout time.Duration, webhookId uint, webhookName, receiverID string) *LoggedHTTPClient

NewLoggedHTTPClient 创建一个新的带日志记录的HTTP客户端

func (*LoggedHTTPClient) DoWithLogging

func (c *LoggedHTTPClient) DoWithLogging(req *http.Request) (*http.Response, *WebhookLog, error)

DoWithLogging 执行HTTP请求并记录详细日志

type PlatformAdapter

type PlatformAdapter interface {
	// Name returns the platform name (e.g., "dingtalk", "feishu", "wechat")
	Name() string

	// GetContentType returns the content type for HTTP requests
	GetContentType() string

	// FormatMessage formats the message according to platform requirements
	FormatMessage(msg, raw string, config *WebhookConfig) ([]byte, error)

	// SignRequest signs the request if required by the platform
	SignRequest(baseURL string, body []byte, secret string) (string, error)
}

PlatformAdapter defines the interface for platform-specific webhook adapters.

func GetAdapter

func GetAdapter(platform string) (PlatformAdapter, error)

GetAdapter retrieves a platform adapter by name.

type SendResult

type SendResult struct {
	Status     string `json:"status"`
	StatusCode int    `json:"status_code"`
	RespBody   string `json:"resp_body"`
	Error      error  `json:"-"`
}

SendResult represents the result of a webhook send operation.

func PushMsgToAllTargetByIDs

func PushMsgToAllTargetByIDs(msg string, raw string, receiverIDs []string) []*SendResult

PushMsgToAllTargetByIDs sends a message to multiple webhook receivers.

func PushMsgToAllTargets

func PushMsgToAllTargets(msg string, raw string, receivers []*models.WebhookReceiver) []*SendResult

PushMsgToAllTargets sends a message to multiple webhook receivers.

func PushMsgToSingleTarget

func PushMsgToSingleTarget(msg string, raw string, receiver *models.WebhookReceiver) *SendResult

PushMsgToSingleTarget sends a message to a single webhook receiver using the new architecture.

type WebhookClient

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

WebhookClient provides a unified HTTP transport layer for webhook sending.

func NewWebhookClient

func NewWebhookClient() *WebhookClient

NewWebhookClient creates a new webhook client with default settings.

func NewWebhookClientWithTimeout

func NewWebhookClientWithTimeout(timeout time.Duration) *WebhookClient

NewWebhookClientWithTimeout creates a new webhook client with custom timeout.

func (*WebhookClient) Send

func (c *WebhookClient) Send(ctx context.Context, msg, raw string, config *WebhookConfig) (*SendResult, error)

Send sends a webhook message using the specified configuration and platform adapter.

type WebhookConfig

type WebhookConfig struct {
	WebhookId    uint   // WebhookID of the webhook configuration
	WebhookName  string // WebhookName of the webhook configuration
	Platform     string // Platform identifier (feishu, dingtalk, wechat, default)
	TargetURL    string // The webhook endpoint URL
	BodyTemplate string // Message body template (optional, platform defaults will be used if empty)
	SignSecret   string // Secret for signing requests (platform-specific)
}

WebhookConfig represents the configuration for a webhook endpoint. This replaces the old Channel concept with clearer naming and responsibilities.

func NewWebhookConfig

func NewWebhookConfig(receiver *models.WebhookReceiver) *WebhookConfig

NewWebhookConfig creates a new webhook configuration from a WebhookReceiver model.

func (*WebhookConfig) GetDefaultTemplate

func (c *WebhookConfig) GetDefaultTemplate() string

GetDefaultTemplate returns the default message template for the platform.

func (*WebhookConfig) GetEffectiveTemplate

func (c *WebhookConfig) GetEffectiveTemplate() string

GetEffectiveTemplate returns the template to use, falling back to default if none specified.

func (*WebhookConfig) HasSignature

func (c *WebhookConfig) HasSignature() bool

HasSignature returns true if this configuration requires request signing.

func (*WebhookConfig) Validate

func (c *WebhookConfig) Validate() error

Validate checks if the configuration is valid.

type WebhookLog

type WebhookLog struct {
	Request  HTTPRequestLog  `json:"request"`
	Response HTTPResponseLog `json:"response"`
	Summary  string          `json:"summary"`
}

WebhookLog 完整的webhook发送日志

type WechatAdapter

type WechatAdapter struct{}

WechatAdapter implements PlatformAdapter for WeChat Work webhooks.

func (*WechatAdapter) FormatMessage

func (w *WechatAdapter) FormatMessage(msg, raw string, config *WebhookConfig) ([]byte, error)

func (*WechatAdapter) GetContentType

func (w *WechatAdapter) GetContentType() string

func (*WechatAdapter) Name

func (w *WechatAdapter) Name() string

func (*WechatAdapter) SignRequest

func (w *WechatAdapter) SignRequest(baseURL string, body []byte, secret string) (string, error)

Jump to

Keyboard shortcuts

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