go-twitch/api/polls/create_poll.go

73 lines
2.5 KiB
Go
Raw 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"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
)
type CreatePollParams struct {
// The ID of the broadcaster thats running the poll. This ID must match the user ID in the user access token.
BroadcasterID string `url:"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 `url:"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 `url:"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 `url:"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 `url:"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 `url:"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 `url:"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, params *CreatePollParams) (*CreatePollResponse, error) {
v, _ := query.Values(params)
endpoint := p.baseUrl.ResolveReference(&url.URL{Path: "polls", RawQuery: v.Encode()})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), nil)
if err != nil {
return nil, err
}
res, err := p.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var data CreatePollResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}