mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-08 16:20:02 +00:00
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
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
|
|
func (c *Client) SendWhisper(ctx context.Context, toUser, message string) error {
|
|
var payload struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
payload.Message = message
|
|
|
|
botID, _, err := c.GetAuthorizedUser(ctx)
|
|
if err != nil {
|
|
return errors.Wrap(err, "getting bot user-id")
|
|
}
|
|
|
|
targetID, err := c.GetIDForUsername(ctx, toUser)
|
|
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(
|
|
c.Request(ctx, ClientRequestOpts{
|
|
AuthType: AuthTypeBearerToken,
|
|
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",
|
|
)
|
|
}
|