package chat import ( "context" "encoding/json" "fmt" "io" "net/http" "net/url" "github.com/google/go-querystring/query" "go.fifitido.net/twitch/api/types" ) type SendChatAnnouncementParams struct { // The ID of the broadcaster that owns the chat room to send the announcement to. BroadcasterID string `url:"broadcaster_id"` // The ID of a user who has permission to moderate the broadcaster’s chat room, or the broadcaster’s ID if they’re sending the announcement. // This ID must match the user ID in the user access token. ModeratorID string `url:"moderator_id"` } type SendChatAnnouncementRequest struct { // The announcement to make in the broadcaster’s chat room. // Announcements are limited to a maximum of 500 characters; announcements longer than 500 characters are truncated. Message string `json:"message"` // The color used to highlight the announcement. Possible case-sensitive values are: // // If color is set to primary or is not set, the channel’s accent color is used to highlight the announcement // (see Profile Accent Color under profile settings, Channel and Videos, and Brand). Color *types.AccentColor `json:"color"` } // Sends an announcement to the broadcaster’s chat room. // // Requires a user access token that includes the moderator:manage:announcements scope. func (c *Chat) SendChatAnnouncement(ctx context.Context, params *SendChatAnnouncementParams, body *SendChatAnnouncementRequest) error { v, _ := query.Values(params) endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "chat/announcements", RawQuery: v.Encode()}) r, w := io.Pipe() go func() { if err := json.NewEncoder(w).Encode(body); err != nil { w.CloseWithError(err) } else { w.Close() } }() req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), r) if err != nil { return err } res, err := c.client.Do(req) if err != nil { return err } defer res.Body.Close() statusOK := res.StatusCode >= 200 && res.StatusCode < 300 if !statusOK { return fmt.Errorf("failed to send chat announcement (%d)", res.StatusCode) } return nil }