2024-02-28 10:30:20 -05:00
|
|
|
|
package chat
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-02-28 10:30:20 -05:00
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-querystring/query"
|
2024-03-07 20:52:42 -05:00
|
|
|
|
"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 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)
|
|
|
|
|
|
|
|
|
|
r, w := io.Pipe()
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
if err := json.NewEncoder(w).Encode(body); err != nil {
|
|
|
|
|
w.CloseWithError(err)
|
|
|
|
|
} else {
|
|
|
|
|
w.Close()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
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()
|
|
|
|
|
|
2024-03-07 19:41:05 -05:00
|
|
|
|
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
|
|
|
|
|
}
|