package whispers import ( "context" "encoding/json" "fmt" "io" "net/http" "github.com/google/go-querystring/query" "go.fifitido.net/twitch/api/endpoint" ) type SendWhisperParams struct { // The ID of the user sending the whisper. // This user must have a verified phone number. // This ID must match the user ID in the user access token. FromUserID string `json:"from_user_id"` // The ID of the user to receive the whisper. ToUserID string `json:"to_user_id"` } type SendWhisperRequest struct { // The whisper message to send. The message must not be empty. // // The maximum message lengths are: // // 500 characters if the user you're sending the message to hasn't whispered you before. // // 10,000 characters if the user you're sending the message to has whispered you before. // // Messages that exceed the maximum length are truncated. Message string `json:"message"` } // Sends a whisper message to the specified user. // // NOTE: The user sending the whisper must have a verified phone number // (see the Phone Number setting in your Security and Privacy settings). // // NOTE: The API may silently drop whispers that it suspects of violating Twitch policies. // (The API does not indicate that it dropped the whisper; it returns a 204 status code as if it succeeded.) // // Rate Limits: You may whisper to a maximum of 40 unique recipients per day. // Within the per day limit, you may whisper a maximum of 3 whispers per second and a maximum of 100 whispers per minute. // // Requires a user access token that includes the user:manage:whispers scope. func (c *Whispers) SendWhisper(ctx context.Context, params *SendWhisperParams, body *SendWhisperRequest) error { v, _ := query.Values(params) 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, "whispers", v), r) if err != nil { return err } res, err := c.client.Do(req) if err != nil { return err } defer res.Body.Close() statusOK := res.StatusCode >= 200 && res.StatusCode < 300 if !statusOK { return fmt.Errorf("failed to send whisper (%d)", res.StatusCode) } return nil }