webhook

package
v0.0.185 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2025 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 added in v0.0.173

func GetRegisteredPlatforms() []string

GetRegisteredPlatforms returns a list of all registered platform names.

func RegisterAdapter added in v0.0.173

func RegisterAdapter(platform string, adapter PlatformAdapter)

RegisterAdapter registers a platform adapter.

func RegisterAllAdapters added in v0.0.173

func RegisterAllAdapters()

RegisterAllAdapters registers all platform adapters.

Types

type DefaultAdapter added in v0.0.173

type DefaultAdapter struct{}

DefaultAdapter implements PlatformAdapter for custom/generic webhooks.

func (*DefaultAdapter) FormatMessage added in v0.0.173

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

func (*DefaultAdapter) GetContentType added in v0.0.173

func (d *DefaultAdapter) GetContentType() string

func (*DefaultAdapter) Name added in v0.0.173

func (d *DefaultAdapter) Name() string

func (*DefaultAdapter) SignRequest added in v0.0.173

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

type DingtalkAdapter added in v0.0.173

type DingtalkAdapter struct{}

DingtalkAdapter implements PlatformAdapter for DingTalk webhooks.

func (*DingtalkAdapter) FormatMessage added in v0.0.173

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

func (*DingtalkAdapter) GetContentType added in v0.0.173

func (d *DingtalkAdapter) GetContentType() string

func (*DingtalkAdapter) Name added in v0.0.173

func (d *DingtalkAdapter) Name() string

func (*DingtalkAdapter) SignRequest added in v0.0.173

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

type FeishuAdapter added in v0.0.173

type FeishuAdapter struct{}

FeishuAdapter implements PlatformAdapter for Feishu webhooks.

func (*FeishuAdapter) FormatMessage added in v0.0.173

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

func (*FeishuAdapter) GetContentType added in v0.0.173

func (f *FeishuAdapter) GetContentType() string

func (*FeishuAdapter) Name added in v0.0.173

func (f *FeishuAdapter) Name() string

func (*FeishuAdapter) SignRequest added in v0.0.173

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

type HTTPRequestLog added in v0.0.173

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 added in v0.0.173

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 added in v0.0.173

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

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

func NewLoggedHTTPClient added in v0.0.173

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

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

func (*LoggedHTTPClient) DoWithLogging added in v0.0.173

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

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

type PlatformAdapter added in v0.0.173

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 added in v0.0.173

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 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 added in v0.0.173

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

WebhookClient provides a unified HTTP transport layer for webhook sending.

func NewWebhookClient added in v0.0.173

func NewWebhookClient() *WebhookClient

NewWebhookClient creates a new webhook client with default settings.

func NewWebhookClientWithTimeout added in v0.0.173

func NewWebhookClientWithTimeout(timeout time.Duration) *WebhookClient

NewWebhookClientWithTimeout creates a new webhook client with custom timeout.

func (*WebhookClient) Send added in v0.0.173

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 added in v0.0.173

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 added in v0.0.173

func NewWebhookConfig(receiver *models.WebhookReceiver) *WebhookConfig

NewWebhookConfig creates a new webhook configuration from a WebhookReceiver model.

func (*WebhookConfig) GetDefaultTemplate added in v0.0.173

func (c *WebhookConfig) GetDefaultTemplate() string

GetDefaultTemplate returns the default message template for the platform.

func (*WebhookConfig) GetEffectiveTemplate added in v0.0.173

func (c *WebhookConfig) GetEffectiveTemplate() string

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

func (*WebhookConfig) HasSignature added in v0.0.173

func (c *WebhookConfig) HasSignature() bool

HasSignature returns true if this configuration requires request signing.

func (*WebhookConfig) Validate added in v0.0.173

func (c *WebhookConfig) Validate() error

Validate checks if the configuration is valid.

type WebhookLog added in v0.0.173

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

WebhookLog 完整的webhook发送日志

type WechatAdapter added in v0.0.173

type WechatAdapter struct{}

WechatAdapter implements PlatformAdapter for WeChat Work webhooks.

func (*WechatAdapter) FormatMessage added in v0.0.173

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

func (*WechatAdapter) GetContentType added in v0.0.173

func (w *WechatAdapter) GetContentType() string

func (*WechatAdapter) Name added in v0.0.173

func (w *WechatAdapter) Name() string

func (*WechatAdapter) SignRequest added in v0.0.173

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