Thanks to visit codestin.com
Credit goes to github.com

Skip to content

circleyu/samhook

Repository files navigation

samhook

English | 繁體中文

A lightweight Go library for sending Slack and Mattermost webhook messages.

Features

  • 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

Installation

go get github.com/circleyu/samhook

Quick Start

Basic Usage

package 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)
    }
}

Using Attachments

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)

Error Handling

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
            }
        }
    }
}

Using Custom Client

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)

Using Retry Mechanism

opts := samhook.DefaultRetryOptions
opts.MaxRetries = 5

err := samhook.SendWithRetry(webhookURL, msg, opts)

Documentation

Project Structure

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

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A lightweight Go library for sending Slack and Mattermost webhook messages.

Resources

License

Stars

Watchers

Forks

Languages