111 lines
4.1 KiB
Go
111 lines
4.1 KiB
Go
package channelpoints
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
"net/url"
|
||
|
||
"go.fifitido.net/twitch/api/endpoint"
|
||
)
|
||
|
||
type CreateCustomRewardsRequest struct {
|
||
// The custom reward’s title. The title may contain a maximum of 45 characters and it must be unique amongst all of the broadcaster’s custom rewards.
|
||
Title string `json:"title"`
|
||
|
||
// The cost of the reward, in Channel Points.
|
||
// The minimum is 1 point.
|
||
Cost int64 `json:"cost"`
|
||
|
||
// The prompt shown to the viewer when they redeem the reward.
|
||
// Specify a prompt if is_user_input_required is true.
|
||
// The prompt is limited to a maximum of 200 characters.
|
||
Prompt *string `json:"prompt"`
|
||
|
||
// A Boolean value that determines whether the reward is enabled. Viewers see only enabled rewards.
|
||
// The default is true.
|
||
IsEnabled *bool `json:"is_enabled"`
|
||
|
||
// The background color to use for the reward. Specify the color using Hex format (for example, #9147FF).
|
||
BackgroundColor *string `json:"background_color"`
|
||
|
||
// A Boolean value that determines whether the user needs to enter information when redeeming the reward. See the prompt field.
|
||
// The default is false.
|
||
IsUserInputRequired *bool `json:"is_user_input_required"`
|
||
|
||
// A Boolean value that determines whether to limit the maximum number of redemptions allowed per live stream (see the max_per_stream field).
|
||
// The default is false.
|
||
IsMaxPerStreamEnabled *bool `json:"is_max_per_stream_enabled"`
|
||
|
||
// The maximum number of redemptions allowed per live stream. Applied only if is_max_per_stream_enabled is true.
|
||
// The minimum value is 1.
|
||
MaxPerStream *int `json:"max_per_stream"`
|
||
|
||
// A Boolean value that determines whether to limit the maximum number of redemptions allowed per user per stream (see the max_per_user_per_stream field).
|
||
// The default is false.
|
||
IsMaxPerUserPerStreamEnabled *bool `json:"is_max_per_user_per_stream_enabled"`
|
||
|
||
// The maximum number of redemptions allowed per user per stream. Applied only if is_max_per_user_per_stream_enabled is true.
|
||
// The minimum value is 1.
|
||
MaxPerUserPerStream *int `json:"max_per_user_per_stream"`
|
||
|
||
// A Boolean value that determines whether to apply a cooldown period between redemptions (see the global_cooldown_seconds field for the duration of the cooldown period).
|
||
// The default is false.
|
||
IsGlobalCooldownEnabled *bool `json:"is_global_cooldown_enabled"`
|
||
|
||
// The cooldown period, in seconds. Applied only if the is_global_cooldown_enabled field is true.
|
||
// The minimum value is 1; however, the minimum value is 60 for it to be shown in the Twitch UX.
|
||
GlobalCooldownSeconds *int `json:"global_cooldown_seconds"`
|
||
|
||
// A Boolean value that determines whether redemptions should be set to FULFILLED status immediately when a reward is redeemed.
|
||
// If false, status is set to UNFULFILLED and follows the normal request queue process.
|
||
// The default is false.
|
||
ShouldRedemptionsSkipRequestQueue *bool `json:"should_redemptions_skip_request_queue"`
|
||
}
|
||
|
||
type CreateCustomRewardsResponse struct {
|
||
Data []CustomReward `json:"data"`
|
||
}
|
||
|
||
// Creates a Custom Reward in the broadcaster’s channel. The maximum number of custom rewards per channel is 50, which includes both enabled and disabled rewards.
|
||
//
|
||
// Requires a user access token that includes the channel:manage:redemptions scope.
|
||
func (c *ChannelPoints) CreateCustomRewards(ctx context.Context, broadcastID string, body *CreateCustomRewardsRequest) (*CreateCustomRewardsResponse, error) {
|
||
v := url.Values{"broadcaster_id": {broadcastID}}
|
||
|
||
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(c.baseUrl, "channel_points/custom_rewards", v), r)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
res, err := c.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 custom rewards (%d)", res.StatusCode)
|
||
}
|
||
|
||
var data CreateCustomRewardsResponse
|
||
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|