English | 繁體中文
A lightweight Go library for sending Slack and Mattermost webhook messages.
- ✅ Lightweight: Minimal external dependencies, uses high-performance JSON library
- ✅ Type-safe: Complete Go type definitions
- ✅ Method chaining: Supports method chaining for better developer experience
- ✅ Flexible input: Supports both struct and Reader input methods
- ✅ Error handling: Detailed error types and classifications
- ✅ Configurable: Supports custom HTTP client and timeout settings
- ✅ Retry mechanism: Optional retry functionality with exponential backoff
go get github.com/circleyu/samhookpackage main
import (
"log""github.com/circleyu/samhook"
)
funcmain() {
webhookURL:="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"msg:= samhook.Message{
Text: "Hello from samhook!",
Username: "samhook-bot",
}
err:=samhook.Send(webhookURL, msg)
iferr!=nil {
log.Fatal(err)
}
}msg:= samhook.Message{
Text: "System Notification",
}
attachment:= samhook.Attachment{
Color: samhook.Good,
Title: "Operation Successful",
Text: "All tasks completed",
}
msg.AddAttachment(attachment)
samhook.Send(webhookURL, msg)err:=samhook.Send(webhookURL, msg)
iferr!=nil {
ifwebhookErr, ok:=err.(*samhook.WebhookError); ok {
ifwebhookErr.IsNetworkError() {
// Handle network error, can retry
} elseifwebhookErr.IsAPIError() {
statusCode:=webhookErr.GetStatusCode()
ifstatusCode==429 {
// Handle rate limiting
}
}
}
}import (
"time""github.com/circleyu/samhook"
)
// Use custom timeouterr:=samhook.SendWithOptions(webhookURL, msg,
samhook.WithTimeout(30*time.Second),
)
// Use Contextctx, cancel:=context.WithTimeout(context.Background(), 10*time.Second)
defercancel()
err:=samhook.SendWithContext(ctx, webhookURL, msg)opts:=samhook.DefaultRetryOptionsopts.MaxRetries=5err:=samhook.SendWithRetry(webhookURL, msg, opts)- API Documentation - Complete API reference
- Architecture Documentation - Project architecture and design
- Usage Examples - Detailed usage examples
samhook/
├── go.mod # Go module definition
├── message.go # Message data structure definitions
├── samhook.go # Core sending functionality
├── error.go # Error type definitions
├── client.go # HTTP client configuration
├── retry.go # Retry mechanism
├── message_test.go # Data structure tests
├── samhook_test.go # Core functionality tests
└── README.md # Project documentation
This project is licensed under the MIT License - see the LICENSE file for details.