webhook

package
v1.0.0-rc.49 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalid = errors.New("Webhook 配置不对")

ErrInvalid 包着「配置写得不对」的错误(方法、格式、地址、请求头);模板本身的错误包着 tmpl.ErrInvalid。

View Source
var ErrUnknownEvent = errors.New("没有这种事件")

ErrUnknownEvent 是预览时给了不认识的事件类型,或者给的不是通知(命令回复、助手回复到不了 Webhook)

Functions

func NewWebhookChannel

func NewWebhookChannel() notify.Channel

Types

type Config

type Config struct {
	EndpointURL    string  `json:"endpoint_url"`
	TimeoutSeconds int     `json:"timeout_seconds"`
	HMACSecret     string  `json:"hmac_secret,omitempty"`
	Headers        Headers `json:"headers,omitempty"`
	// Method 是请求方法:POST(默认)、PUT、PATCH、GET(GET 不带请求体)
	Method string `json:"method,omitempty"`
	// ContentType 是请求体的格式:json(默认)、text、form、custom(Content-Type 写在 Headers 里)
	ContentType string `json:"content_type,omitempty"`
	// BodyTemplate 是请求体模板(Go 模板,变量见消息模板的事件目录);空着发原来的固定 JSON
	BodyTemplate string `json:"body_template,omitempty"`
	// SuccessTemplate 是按响应判断成败的模板(可选):2xx 以后再用它算一遍,结果是 true 才算送到,
	// 别的结果(false、空、或者自己写的原因)算失败。变量:status、body、json(响应是 JSON 时解出来的值)
	SuccessTemplate string `json:"success_template,omitempty"`
}

Config 是 Webhook 通道的配置。Method、ContentType、BodyTemplate 都空着时发原来的固定 JSON(多一个 event 字段)。 请求体模板里可能写着 token,和其余配置一样只存在加密的 ConfigJSON 里。

type Headers

type Headers map[string]string

Headers 是自定义请求头。旧版编辑页把它存成了 JSON 文本("{\"A\":\"b\"}"),两种写法都认。

func (*Headers) UnmarshalJSON

func (h *Headers) UnmarshalJSON(b []byte) error

type PreviewInput

type PreviewInput struct {
	Type         string  `json:"type"`
	Method       string  `json:"method"`
	EndpointURL  string  `json:"endpoint_url"`
	ContentType  string  `json:"content_type"`
	Headers      Headers `json:"headers"`
	BodyTemplate string  `json:"body_template"`
	// SuccessTemplate 是成功条件;SampleResponse、SampleStatus(默认 200)是拿来试它的一份响应
	SuccessTemplate string `json:"success_template"`
	SampleResponse  string `json:"sample_response"`
	SampleStatus    int    `json:"sample_status"`
}

PreviewInput 是预览的输入,字段和配置里的同名;Type 是用哪种事件的示例数据。

type PreviewResult

type PreviewResult struct {
	Method      string `json:"method"`
	URL         string `json:"url"`
	ContentType string `json:"content_type"`
	Body        string `json:"body"`
	// Success 是按示例响应算出的成败(写了成功条件时才有),SuccessReason 是失败的原因
	Success       *bool  `json:"success,omitempty"`
	SuccessReason string `json:"success_reason,omitempty"`
}

PreviewResult 是按示例数据渲染出来的请求(只渲染、不发送,不含签名)。ContentType 按发送时的规则算。

func Preview

func Preview(in PreviewInput) (*PreviewResult, error)

Preview 按一种事件的示例数据渲染请求。配置不对时返回的错误包着 ErrInvalid 或 tmpl.ErrInvalid。

type WebhookChannel

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

func (*WebhookChannel) CheckConfig

func (w *WebhookChannel) CheckConfig(conf *models.NotificationConf) error

CheckConfig 在保存前检查配置(不发请求):方法、格式、请求头,地址与请求体模板要能用每种事件的示例数据渲染出来, JSON 格式的请求体还要是合法的 JSON。

func (*WebhookChannel) Close

func (w *WebhookChannel) Close(ctx context.Context) error

func (*WebhookChannel) Healthy

func (w *WebhookChannel) Healthy() bool

func (*WebhookChannel) Init

func (*WebhookChannel) OnInbound

func (w *WebhookChannel) OnInbound(handler notify.InboundHandler)

func (*WebhookChannel) Send

func (*WebhookChannel) SupportsInbound

func (w *WebhookChannel) SupportsInbound() bool

func (*WebhookChannel) Type

func (w *WebhookChannel) Type() string

type WebhookPayload

type WebhookPayload struct {
	EventType string                 `json:"event_type"`
	Title     string                 `json:"title"`
	Text      string                 `json:"text"`
	Link      string                 `json:"link,omitempty"`
	Payload   map[string]interface{} `json:"payload,omitempty"`
	Event     *notify.Event          `json:"event,omitempty"`
}

WebhookPayload 是不写请求体模板时发的固定 JSON。Event 是后来加的:原有字段不变。

Jump to

Keyboard shortcuts

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