2024-03-03 15:50:12 -05:00
|
|
|
|
package predictions
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-03-03 15:50:12 -05:00
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-querystring/query"
|
2024-03-07 20:52:42 -05:00
|
|
|
|
"go.fifitido.net/twitch/api/endpoint"
|
2024-03-03 15:50:12 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CreatePredictionRequest struct {
|
|
|
|
|
// The ID of the broadcaster that’s running the prediction. This ID must match the user ID in the user access token.
|
|
|
|
|
BroadcasterID string `json:"broadcaster_id"`
|
|
|
|
|
|
|
|
|
|
// The question that the broadcaster is asking. For example, Will I finish this entire pizza?
|
|
|
|
|
Title string `json:"title"`
|
|
|
|
|
|
|
|
|
|
// The list of possible outcomes that the viewers may choose from. The list must contain a minimum of 2 choices and up to a maximum of 10 choices.
|
|
|
|
|
Outcomes []CreateOutcome `json:"outcomes"`
|
|
|
|
|
|
|
|
|
|
// The length of time (in seconds) that the prediction will run for. The minimum is 30 seconds and the maximum is 1800 seconds (30 minutes).
|
|
|
|
|
PredictionWindow int `json:"prediction_window"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type CreateOutcome struct {
|
|
|
|
|
// The text of one of the outcomes that the viewer may select. The title is limited to a maximum of 25 characters.
|
|
|
|
|
Title string `json:"title"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type CreatePredictionResponse struct {
|
|
|
|
|
// A list that contains the single prediction that you created.
|
|
|
|
|
Data []Prediction `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Creates a Channel Points Prediction.
|
|
|
|
|
//
|
|
|
|
|
// With a Channel Points Prediction, the broadcaster poses a question and viewers try to predict the outcome.
|
|
|
|
|
// The prediction runs as soon as it’s created. The broadcaster may run only one prediction at a time.
|
|
|
|
|
//
|
|
|
|
|
// Requires a user access token that includes the channel:manage:predictions scope.
|
2024-03-07 20:52:42 -05:00
|
|
|
|
func (p *Predictions) CreatePrediction(ctx context.Context, params *CreatePredictionRequest) (*CreatePredictionResponse, error) {
|
2024-03-03 15:50:12 -05:00
|
|
|
|
v, _ := query.Values(params)
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.Make(p.baseUrl, "predictions", v), nil)
|
2024-03-03 15:50:12 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
res, err := p.client.Do(req)
|
2024-03-03 15:50:12 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
2024-03-07 19:41:05 -05:00
|
|
|
|
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
|
|
|
|
if !statusOK {
|
|
|
|
|
return nil, fmt.Errorf("failed to create prediction (%d)", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-03 15:50:12 -05:00
|
|
|
|
var data CreatePredictionResponse
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|