go-twitch/api/chat/send_chat_announcement.go

65 lines
1.9 KiB
Go
Raw Normal View History

2024-02-28 10:30:20 -05:00
package chat
import (
"context"
"encoding/json"
"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 broadcasters chat room, or the broadcasters ID if theyre 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 broadcasters 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 channels 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 broadcasters 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()
return nil
}