go-twitch/api/polls/create_poll.go

86 lines
2.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package polls
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"go.fifitido.net/twitch/api/endpoint"
)
type CreatePollRequest struct {
// 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"`
// 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"`
// 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"`
// 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"`
// 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"`
// 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"`
}
type CreateChoice struct {
// One of the choices the viewer may select. The choice may contain a maximum of 25 characters.
Title string `json:"title"`
}
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()
}
}()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.Make(p.baseUrl, "polls"), r)
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)
}
var data CreatePollResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}