musiclink/internal/bot/handler.go
Meta-Repo Bot f602c02f80 feat: Prepare for Nix deployment with integration tests and flake
This commit prepares the musiclink bot for production deployment on NixOS.

Changes:
- Refactored service layer to support Dependency Injection for API URLs.
- Added 'internal/bot/bot_integration_test.go' for mock-based integration testing.
- Added 'flake.nix' and 'flake.lock' for reproducible Nix builds.
- Added 'README.md', 'LICENSE', and '.gitignore'.
- Verified build and tests pass locally.
2026-01-16 21:57:13 +00:00

63 lines
1.3 KiB
Go

package bot
import (
"context"
"log"
"musiclink/internal/detector"
"musiclink/internal/resolver"
)
// Handler processes incoming messages and generates responses.
type Handler struct {
detector *detector.Detector
resolver *resolver.Resolver
}
// NewHandler creates a new message handler.
func NewHandler(apiURL string) *Handler {
return &Handler{
detector: detector.New(),
resolver: resolver.New(apiURL),
}
}
// Handle processes a message and returns a response if music links were found.
func (h *Handler) Handle(ctx context.Context, msg Message) *Message {
// Detect music links in the message
links := h.detector.Detect(msg.Text)
if len(links) == 0 {
return nil
}
log.Printf("Found %d music link(s) in message from %s", len(links), msg.Username)
// Process the first link found
link := links[0]
// Resolve to other services via idonthavespotify API
resolved, err := h.resolver.Resolve(ctx, link.URL)
if err != nil {
log.Printf("Error resolving link: %v", err)
return nil
}
// Only respond if we found links on other services
if len(resolved.Links) <= 1 {
log.Printf("No additional links found for %s", link.URL)
return nil
}
// Format the response
title := ""
if resolved.Track != nil {
title = resolved.Track.Title
}
text := resolver.Format(resolved, title)
return &Message{
Text: text,
Gateway: msg.Gateway,
}
}