2022-10-25 16:47:30 +00:00
|
|
|
package twitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SendWhisper sends a whisper from the bot to the specified user.
|
|
|
|
//
|
|
|
|
// For details about message limits see the official documentation:
|
|
|
|
// https://dev.twitch.tv/docs/api/reference#send-whisper
|
2024-01-01 16:52:18 +00:00
|
|
|
func (c *Client) SendWhisper(ctx context.Context, toUser, message string) error {
|
2022-10-25 16:47:30 +00:00
|
|
|
var payload struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.Message = message
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
botID, _, err := c.GetAuthorizedUser(ctx)
|
2022-10-25 16:47:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting bot user-id")
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
targetID, err := c.GetIDForUsername(ctx, toUser)
|
2022-10-25 16:47:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting target user-id")
|
|
|
|
}
|
|
|
|
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
if err = json.NewEncoder(body).Encode(payload); err != nil {
|
|
|
|
return errors.Wrap(err, "encoding payload")
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Wrap(
|
2024-01-01 16:52:18 +00:00
|
|
|
c.Request(ctx, ClientRequestOpts{
|
2023-07-01 14:48:21 +00:00
|
|
|
AuthType: AuthTypeBearerToken,
|
2022-10-25 16:47:30 +00:00
|
|
|
Method: http.MethodPost,
|
|
|
|
OKStatus: http.StatusNoContent,
|
|
|
|
Body: body,
|
|
|
|
URL: fmt.Sprintf(
|
|
|
|
"https://api.twitch.tv/helix/whispers?from_user_id=%s&to_user_id=%s",
|
|
|
|
botID, targetID,
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
"executing whisper request",
|
|
|
|
)
|
|
|
|
}
|