2024-03-03 15:27:37 -05:00
|
|
|
|
package polls
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-03-05 22:29:23 -05:00
|
|
|
|
"io"
|
2024-03-03 15:27:37 -05:00
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
)
|
|
|
|
|
|
2024-03-05 22:29:23 -05:00
|
|
|
|
type CreatePollRequest struct {
|
2024-03-03 15:27:37 -05:00
|
|
|
|
|
|
|
|
|
// The ID of the broadcaster that’s running the poll. This ID must match the user ID in the user access token.
|
2024-03-05 22:29:23 -05:00
|
|
|
|
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.
|
2024-03-05 22:29:23 -05:00
|
|
|
|
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.
|
2024-03-05 22:29:23 -05:00
|
|
|
|
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).
|
2024-03-05 22:29:23 -05:00
|
|
|
|
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.
|
2024-03-05 22:29:23 -05:00
|
|
|
|
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.
|
2024-03-05 22:29:23 -05:00
|
|
|
|
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.
|
2024-03-05 22:29:23 -05:00
|
|
|
|
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 broadcaster’s channel can vote on.
|
|
|
|
|
//
|
|
|
|
|
// The poll begins as soon as it’s created. You may run only one poll at a time.
|
|
|
|
|
//
|
|
|
|
|
// Requires a user access token that includes the channel:manage:polls scope.
|
2024-03-05 22:29:23 -05:00
|
|
|
|
func (p *Polls) CreatePoll(ctx context.Context, body *CreatePollRequest) (*CreatePollResponse, error) {
|
|
|
|
|
endpoint := p.baseUrl.ResolveReference(&url.URL{Path: "polls"})
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-03-05 22:29:23 -05:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), 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()
|
|
|
|
|
|
|
|
|
|
var data CreatePollResponse
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|