/
chat.go
92 lines (79 loc) · 2.32 KB
/
chat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package twitch
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"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)
func (c *Client) SendChatAnnouncement(ctx context.Context, channel, color, message string) error {
var payload struct {
Color string `json:"color,omitempty"`
Message string `json:"message"`
}
payload.Color = color
payload.Message = message
botID, _, err := c.GetAuthorizedUser(ctx)
if err != nil {
return errors.Wrap(err, "getting bot user-id")
}
channelID, err := c.GetIDForUsername(ctx, strings.TrimLeft(channel, "#@"))
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(
c.Request(ctx, ClientRequestOpts{
AuthType: AuthTypeBearerToken,
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",
)
}
// SendShoutout creates a Twitch-native shoutout in the given channel
// for the given user. This equals `/shoutout <user>` in the channel.
func (c *Client) SendShoutout(ctx context.Context, channel, user string) error {
botID, _, err := c.GetAuthorizedUser(ctx)
if err != nil {
return errors.Wrap(err, "getting bot user-id")
}
channelID, err := c.GetIDForUsername(ctx, strings.TrimLeft(channel, "#@"))
if err != nil {
return errors.Wrap(err, "getting channel user-id")
}
userID, err := c.GetIDForUsername(ctx, strings.TrimLeft(user, "#@"))
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(
c.Request(ctx, ClientRequestOpts{
AuthType: AuthTypeBearerToken,
Method: http.MethodPost,
OKStatus: http.StatusNoContent,
URL: fmt.Sprintf(
"https://api.twitch.tv/helix/chat/shoutouts?%s",
params.Encode(),
),
}),
"executing request",
)
}