2022-10-25 16:47:30 +00:00
|
|
|
package twitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-01-20 11:13:59 +00:00
|
|
|
"net/url"
|
2022-10-25 16:47:30 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SendChatAnnouncement sends an announcement in the specified
|
|
|
|
// channel with the given message. Colors must be blue, green,
|
|
|
|
// orange, purple or primary (empty color = primary)
|
2024-01-01 16:52:18 +00:00
|
|
|
func (c *Client) SendChatAnnouncement(ctx context.Context, channel, color, message string) error {
|
2022-10-25 16:47:30 +00:00
|
|
|
var payload struct {
|
|
|
|
Color string `json:"color,omitempty"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.Color = color
|
|
|
|
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
|
|
|
channelID, err := c.GetIDForUsername(ctx, strings.TrimLeft(channel, "#@"))
|
2022-10-25 16:47:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting channel 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/chat/announcements?broadcaster_id=%s&moderator_id=%s",
|
|
|
|
channelID, botID,
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
"executing request",
|
|
|
|
)
|
|
|
|
}
|
2023-01-20 11:13:59 +00:00
|
|
|
|
|
|
|
// SendShoutout creates a Twitch-native shoutout in the given channel
|
|
|
|
// for the given user. This equals `/shoutout <user>` in the channel.
|
2024-01-01 16:52:18 +00:00
|
|
|
func (c *Client) SendShoutout(ctx context.Context, channel, user string) error {
|
|
|
|
botID, _, err := c.GetAuthorizedUser(ctx)
|
2023-01-20 11:13:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting bot user-id")
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
channelID, err := c.GetIDForUsername(ctx, strings.TrimLeft(channel, "#@"))
|
2023-01-20 11:13:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting channel user-id")
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
userID, err := c.GetIDForUsername(ctx, strings.TrimLeft(user, "#@"))
|
2023-01-20 11:13:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting user user-id")
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(url.Values)
|
|
|
|
params.Set("from_broadcaster_id", channelID)
|
|
|
|
params.Set("moderator_id", botID)
|
|
|
|
params.Set("to_broadcaster_id", userID)
|
|
|
|
|
|
|
|
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,
|
2023-01-20 11:13:59 +00:00
|
|
|
Method: http.MethodPost,
|
|
|
|
OKStatus: http.StatusNoContent,
|
|
|
|
URL: fmt.Sprintf(
|
|
|
|
"https://api.twitch.tv/helix/chat/shoutouts?%s",
|
|
|
|
params.Encode(),
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
"executing request",
|
|
|
|
)
|
|
|
|
}
|