Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
1.6 kB · 55 lines
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556package discord
import ( "bytes" "context" "encoding/json" "fmt" "net/http" "strings"
"stream.place/streamplace/pkg/aqhttp" "stream.place/streamplace/pkg/integrations/discord/discordtypes" "stream.place/streamplace/pkg/log")
func SendStreamReceived(ctx context.Context, w *discordtypes.Webhook, streamerDID string) error { content := fmt.Sprintf("stream.received %s", streamerDID) for _, rewrite := range w.Rewrite { content = strings.ReplaceAll(content, rewrite.From, rewrite.To) }
payload := discordtypes.Payload{ Content: fmt.Sprintf("%s%s%s", w.Prefix, content, w.Suffix), }
jsonPayload, err := json.Marshal(payload) if err != nil { return fmt.Errorf("failed to marshal payload: %w", err) }
log.Log(ctx, "sending stream.received to discord", "streamerDID", streamerDID, "webhook_url", w.URL)
req, err := http.NewRequestWithContext(ctx, "POST", w.URL, bytes.NewReader(jsonPayload)) if err != nil { return fmt.Errorf("failed to create request: %w", err) } req.Header.Set("Content-Type", "application/json")
resp, err := aqhttp.Do(ctx, req) if err != nil { return fmt.Errorf("failed to send request: %w", err) } defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent { body, err := readResponseBody(resp.Body) if err != nil { return fmt.Errorf("failed to read response body: %w", err) } log.Error(ctx, "stream.received webhook delivery failed", "webhook_url", w.URL, "status_code", resp.StatusCode, "response_body", body) return fmt.Errorf("failed to send request (http %d): %s", resp.StatusCode, body) }
return nil}