Skip to content

Modern Docker Management, Designed for Everyone

2.0 KB
Raw Blame
package notifications

import (
	"context"
	"errors"
	"fmt"
	"net/url"
	"strings"

	shoutrrrTypes "github.com/nicholas-fedor/shoutrrr/pkg/types"
)

// BuildGoogleChatURL converts GoogleChatConfig to Shoutrrr URL format by
// swapping the incoming webhook URL's scheme to googlechat while preserving
// the host, path and the key/token query parameters.
func BuildGoogleChatURL(config GoogleChatConfig) (string, error) {
	if config.WebhookURL == "" {
		return "", errors.New("google chat webhook URL is empty")
	}

	parsed, err := url.Parse(config.WebhookURL)
	if err != nil {
		return "", fmt.Errorf("invalid google chat webhook URL: %w", err)
	}

	// Accept a bare host without scheme, mirroring the generic webhook's URL
	// normalisation.
	if parsed.Host == "" && !strings.Contains(config.WebhookURL, "://") {
		normalized := strings.TrimPrefix(config.WebhookURL, "//")
		parsed, err = url.Parse("https://" + normalized)
		if err != nil {
			return "", fmt.Errorf("invalid google chat webhook URL: %w", err)
		}
	}

	if parsed.Host == "" {
		return "", errors.New("invalid google chat webhook URL: missing host")
	}

	switch strings.ToLower(parsed.Scheme) {
	case "http", "https":
	default:
		return "", fmt.Errorf("invalid google chat webhook URL scheme: %s", parsed.Scheme)
	}

	shoutrrrURL := &url.URL{
		Scheme:   "googlechat",
		Host:     parsed.Host,
		Path:     parsed.Path,
		RawQuery: parsed.Query().Encode(),
	}

	return shoutrrrURL.String(), nil
}

// SendGoogleChat sends a message via Shoutrrr's Google Chat service. The
// service posts plain text only ({"text": ...}) and ignores params, so the
// title is expected to already be part of the message body.
func SendGoogleChat(ctx context.Context, config GoogleChatConfig, message string) error {
	shoutrrrURL, err := BuildGoogleChatURL(config)
	if err != nil {
		return fmt.Errorf("failed to build shoutrrr Google Chat URL: %w", err)
	}

	return sendShoutrrrInternal("Google Chat", shoutrrrURL, message, &shoutrrrTypes.Params{})
}