2024-02-27 22:13:57 -05:00
package channelpoints
import (
2024-02-27 23:03:40 -05:00
"context"
2024-02-27 22:13:57 -05:00
"encoding/json"
2024-03-07 19:41:05 -05:00
"fmt"
2024-02-27 22:13:57 -05:00
"io"
2024-02-27 23:03:40 -05:00
"net/http"
2024-02-27 22:13:57 -05:00
"net/url"
)
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.
2024-02-27 23:03:40 -05:00
func ( c * ChannelPoints ) CreateCustomRewards ( ctx context . Context , broadcastID string , body * CreateCustomRewardsRequest ) ( * CreateCustomRewardsResponse , error ) {
2024-02-27 22:13:57 -05:00
endpoint := c . baseUrl . ResolveReference ( & url . URL { Path : "channel_points/custom_rewards" , RawQuery : "broadcaster_id=" + broadcastID } )
r , w := io . Pipe ( )
go func ( ) {
2024-02-27 23:03:40 -05:00
if err := json . NewEncoder ( w ) . Encode ( body ) ; err != nil {
2024-02-27 22:13:57 -05:00
w . CloseWithError ( err )
} else {
w . Close ( )
}
} ( )
2024-02-27 23:03:40 -05:00
req , err := http . NewRequestWithContext ( ctx , http . MethodPost , endpoint . String ( ) , r )
2024-02-27 22:13:57 -05:00
if err != nil {
return nil , err
}
2024-02-27 23:03:40 -05:00
res , err := c . client . Do ( req )
if err != nil {
return nil , err
}
defer res . Body . Close ( )
2024-02-27 22:13:57 -05:00
2024-03-07 19:41:05 -05:00
statusOK := res . StatusCode >= 200 && res . StatusCode < 300
if ! statusOK {
return nil , fmt . Errorf ( "failed to create custom rewards (%d)" , res . StatusCode )
}
2024-02-27 22:13:57 -05:00
var data CreateCustomRewardsResponse
2024-02-27 23:03:40 -05:00
if err := json . NewDecoder ( res . Body ) . Decode ( & data ) ; err != nil {
2024-02-27 22:13:57 -05:00
return nil , err
}
return & data , nil
}