Documentation
¶
Index ¶
- Constants
- Variables
- func Send(url string, msg Message) error
- func SendReader(url string, r io.Reader) error
- func SendWithContext(ctx context.Context, url string, msg Message, opts ...ClientOption) error
- func SendWithOptions(url string, msg Message, opts ...ClientOption) error
- func SendWithRetry(url string, msg Message, opts RetryOptions, clientOpts ...ClientOption) error
- func SetLogger(logger Logger)
- func SetLoggerWriter(w io.Writer)
- func ValidateWebhookURL(webhookURL string) error
- type Attachment
- type ClientOption
- type ExponentialBackoff
- type Field
- type Logger
- type Message
- type RetryOptions
- type WebhookError
- func (e *WebhookError) DetailedMessage() string
- func (e *WebhookError) Error() string
- func (e *WebhookError) GetErrorCode() string
- func (e *WebhookError) GetResponseBody() string
- func (e *WebhookError) GetStatusCode() int
- func (e *WebhookError) IsAPIError() bool
- func (e *WebhookError) IsNetworkError() bool
- func (e *WebhookError) IsSerializationError() bool
- func (e *WebhookError) Unwrap() error
Constants ¶
const ( ErrorTypeNetwork = "network" ErrorTypeSerialization = "serialization" ErrorTypeAPI = "api" ErrorTypeUnknown = "unknown" )
錯誤類型常數
const ( ErrorCodeNetworkTimeout = "NETWORK_TIMEOUT" ErrorCodeNetworkConnection = "NETWORK_CONNECTION" ErrorCodeNetworkDNS = "NETWORK_DNS" ErrorCodeSerializationJSON = "SERIALIZATION_JSON" ErrorCodeAPIForbidden = "API_FORBIDDEN" ErrorCodeAPINotFound = "API_NOT_FOUND" ErrorCodeAPIRateLimit = "API_RATE_LIMIT" ErrorCodeAPIServerError = "API_SERVER_ERROR" )
錯誤代碼常數
const Danger string = "#FF0000"
Danger is a predefined color for a dangerous condition (red)
const DefaultTimeout = 10 * time.Second
DefaultTimeout 預設超時時間
const Good string = "#00FF00"
Good is a predefined color for a normal information (green)
const Warning string = "#FFBB00"
Warning is a predefined color for a warning (yellow)
Variables ¶
var DefaultRetryOptions = RetryOptions{ MaxRetries: 3, Interval: 1 * time.Second, Backoff: &ExponentialBackoff{ InitialInterval: 1 * time.Second, MaxInterval: 30 * time.Second, Multiplier: 2.0, Jitter: true, }, }
DefaultRetryOptions 預設重試選項
Functions ¶
func SendWithContext ¶
SendWithContext 使用 Context 發送訊息
func SendWithOptions ¶
func SendWithOptions(url string, msg Message, opts ...ClientOption) error
SendWithOptions 使用選項發送訊息
func SendWithRetry ¶
func SendWithRetry(url string, msg Message, opts RetryOptions, clientOpts ...ClientOption) error
SendWithRetry 帶重試的發送,支援自訂客戶端配置
func ValidateWebhookURL ¶
ValidateWebhookURL 驗證 webhook URL 格式和協議
Types ¶
type Attachment ¶
type Attachment struct {
Fallback string `json:"fallback,omitempty"`
Color string `json:"color,omitempty"`
Pretext string `json:"pretext,omitempty"`
AuthorName string `json:"author_name,omitempty"`
AuthorLink string `json:"author_link,omitempty"`
AuthorIcon string `json:"author_icon,omitempty"`
Title string `json:"title,omitempty"`
TitleLink string `json:"title_link,omitempty"`
Text string `json:"text,omitempty"`
ImageURL string `json:"image_url,omitempty"`
Fields []Field `json:"fields,omitempty"`
ThumbURL string `json:"thumb_url,omitempty"`
}
Attachment attachment主體
type ClientOption ¶
ClientOption 客戶端選項
type ExponentialBackoff ¶
type ExponentialBackoff struct {
InitialInterval time.Duration
MaxInterval time.Duration
Multiplier float64
Jitter bool
}
ExponentialBackoff 指數退避
func (*ExponentialBackoff) NextInterval ¶
func (eb *ExponentialBackoff) NextInterval(attempt int) time.Duration
NextInterval 計算下一次重試間隔
type Field ¶
type Field struct {
Title string `json:"title,omitempty"`
Value string `json:"value,omitempty"`
Short bool `json:"short,omitempty"`
}
Field field主體
type Message ¶
type Message struct {
Parse string `json:"parse,omitempty"`
Username string `json:"username,omitempty"`
IconURL string `json:"icon_url,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
Channel string `json:"channel,omitempty"`
Text string `json:"text,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
}
Message message主體
func (*Message) AddAttachment ¶
func (m *Message) AddAttachment(attachment Attachment) *Message
AddAttachment 添加一個attachment
func (*Message) AddAttachments ¶
func (m *Message) AddAttachments(attachments []Attachment) *Message
AddAttachments 添加多個attachment
type RetryOptions ¶
type RetryOptions struct {
MaxRetries int
Interval time.Duration
Backoff *ExponentialBackoff
}
RetryOptions 重試選項
type WebhookError ¶
type WebhookError struct {
// Type 錯誤類型
Type string
// StatusCode HTTP 狀態碼(如果是 API 錯誤)
StatusCode int
// Message 錯誤訊息
Message string
// ResponseBody API 回應體(如果是 API 錯誤)
ResponseBody string
// Err 原始錯誤
Err error
// URL webhook URL(用於上下文)
URL string
// ErrorCode 具體的錯誤代碼(用於更細緻的分類)
ErrorCode string
}
WebhookError 表示 webhook 操作中的錯誤
func NewAPIError ¶
func NewAPIError(url string, statusCode int, responseBody string) *WebhookError
NewAPIError 創建 API 錯誤
func NewNetworkError ¶
func NewNetworkError(url string, err error) *WebhookError
NewNetworkError 創建網路錯誤,自動分類錯誤類型
func NewSerializationError ¶
func NewSerializationError(err error) *WebhookError
NewSerializationError 創建序列化錯誤
func (*WebhookError) DetailedMessage ¶
func (e *WebhookError) DetailedMessage() string
DetailedMessage 返回詳細的錯誤訊息(多行格式)
func (*WebhookError) GetErrorCode ¶
func (e *WebhookError) GetErrorCode() string
GetErrorCode 返回錯誤代碼
func (*WebhookError) GetResponseBody ¶
func (e *WebhookError) GetResponseBody() string
GetResponseBody 返回 API 回應體(如果是 API 錯誤)
func (*WebhookError) GetStatusCode ¶
func (e *WebhookError) GetStatusCode() int
GetStatusCode 返回 HTTP 狀態碼(如果是 API 錯誤)
func (*WebhookError) IsNetworkError ¶
func (e *WebhookError) IsNetworkError() bool
IsNetworkError 判斷是否為網路錯誤
func (*WebhookError) IsSerializationError ¶
func (e *WebhookError) IsSerializationError() bool
IsSerializationError 判斷是否為序列化錯誤