go-twitch/api/polls/create_poll.go

86 lines
2.7 KiB
Go
Raw Normal View History

2024-03-03 15:27:37 -05:00
package polls
import (
"context"
"encoding/json"
"fmt"
"io"
2024-03-03 15:27:37 -05:00
"net/http"
"go.fifitido.net/twitch/api/endpoint"
2024-03-03 15:27:37 -05:00
)
type CreatePollRequest struct {
2024-03-03 15:27:37 -05:00
// The ID of the broadcaster thats running the poll. This ID must match the user ID in the user access token.
BroadcasterID string `json:"broadcaster_id"`
2024-03-03 15:27:37 -05:00
// The question that viewers will vote on. For example, What game should I play next? The question may contain a maximum of 60 characters.
Title string `json:"title"`
2024-03-03 15:27:37 -05:00
// A list of choices that viewers may choose from. The list must contain a minimum of 2 choices and up to a maximum of 5 choices.
Choices []CreateChoice `json:"choices"`
2024-03-03 15:27:37 -05:00
// The length of time (in seconds) that the poll will run for. The minimum is 15 seconds and the maximum is 1800 seconds (30 minutes).
Duration int `json:"duration"`
2024-03-03 15:27:37 -05:00
// A Boolean value that indicates whether viewers may cast additional votes using Channel Points.
// If true, the viewer may cast more than one vote but each additional vote costs the number of Channel Points specified in channel_points_per_vote.
// The default is false (viewers may cast only one vote). For information about Channel Points, see Channel Points Guide.
ChannelPointsVotingEnabled bool `json:"channel_points_voting_enabled"`
2024-03-03 15:27:37 -05:00
// The number of points that the viewer must spend to cast one additional vote. The minimum is 1 and the maximum is 1000000.
// Set only if ChannelPointsVotingEnabled is true.
ChannelPointsPerVote int `json:"channel_points_per_vote"`
2024-03-03 15:27:37 -05:00
}
type CreateChoice struct {
// One of the choices the viewer may select. The choice may contain a maximum of 25 characters.
Title string `json:"title"`
2024-03-03 15:27:37 -05:00
}
type CreatePollResponse struct {
// A list that contains the single poll that you created.
Data []Poll `json:"data"`
}
// Creates a poll that viewers in the broadcasters channel can vote on.
//
// The poll begins as soon as its created. You may run only one poll at a time.
//
// Requires a user access token that includes the channel:manage:polls scope.
func (p *Polls) CreatePoll(ctx context.Context, body *CreatePollRequest) (*CreatePollResponse, error) {
r, w := io.Pipe()
go func() {
if err := json.NewEncoder(w).Encode(body); err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
}()
2024-03-03 15:27:37 -05:00
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.Make(p.baseUrl, "polls"), r)
2024-03-03 15:27:37 -05:00
if err != nil {
return nil, err
}
res, err := p.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to create poll (%d)", res.StatusCode)
}
2024-03-03 15:27:37 -05:00
var data CreatePollResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}