go-twitch/api/chat/send_chat_announcement.go

70 lines
2.0 KiB
Go
Raw Permalink Normal View History

2024-02-28 10:30:20 -05:00
package chat
import (
"context"
"encoding/json"
"fmt"
2024-02-28 10:30:20 -05:00
"io"
"net/http"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
2024-02-28 10:30:20 -05:00
"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)
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.Make(c.baseUrl, "chat/announcements", v), r)
2024-02-28 10:30:20 -05:00
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)
}
2024-02-28 10:30:20 -05:00
return nil
}