mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-14 02:52:42 +00:00
53 lines
1.2 KiB
Go
53 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(toUser, message string) error {
|
|
var payload struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
payload.Message = message
|
|
|
|
botID, _, err := c.GetAuthorizedUser()
|
|
if err != nil {
|
|
return errors.Wrap(err, "getting bot user-id")
|
|
}
|
|
|
|
targetID, err := c.GetIDForUsername(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(clientRequestOpts{
|
|
AuthType: authTypeBearerToken,
|
|
Context: context.Background(),
|
|
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",
|
|
)
|
|
}
|