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"
)
func main() {
webhookURL := "https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
msg := samhook.Message{
Text: "Hello from samhook!",
Username: "samhook-bot",
}
err := samhook.Send(webhookURL, msg)
if err != 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)
if err != nil {
if webhookErr, ok := err.(*samhook.WebhookError); ok {
if webhookErr.IsNetworkError() {
// Handle network error, can retry
} else if webhookErr.IsAPIError() {
statusCode := webhookErr.GetStatusCode()
if statusCode == 429 {
// Handle rate limiting
}
}
}
}import (
"time"
"github.com/circleyu/samhook"
)
// Use custom timeout
err := samhook.SendWithOptions(webhookURL, msg,
samhook.WithTimeout(30 * time.Second),
)
// Use Context
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := samhook.SendWithContext(ctx, webhookURL, msg)opts := samhook.DefaultRetryOptions
opts.MaxRetries = 5
err := 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.