/
slack.go
54 lines (46 loc) · 1.46 KB
/
slack.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
package testkit
import (
"log"
"os"
"testing"
"github.com/slack-go/slack"
"github.com/stretchr/testify/require"
)
type SlackMessaging interface {
SendMessage(t *testing.T, message string)
}
type SlackMessagingClient struct {
slackClient *slack.Client
channel string
}
func NewSlackMessagingClient(token, channel string) *SlackMessagingClient {
return &SlackMessagingClient{
slackClient: slack.New(
token, slack.OptionLog(
log.New(os.Stdout, "testkit-slack: ", log.Lshortfile|log.LstdFlags),
),
),
channel: channel,
}
}
func (c *SlackMessagingClient) SendMessage(t *testing.T, message string) {
t.Helper()
_, _, err := c.slackClient.PostMessage(
c.channel,
slack.MsgOptionText(message, false),
)
// If you get "missing_scope" erorr here, you need to add the "chat:write" scope to your bot token.
// Click "Add an OAuth Scope" in the "OAuth & Permissions" page of your app settings,
// add the "chat:write" scope, and reinstall the app to your workspace.
//
// If you get "invalid_auth" error here, you need to reinstall the app to your workspace.
// Go to the "OAuth & Permissions" page of your app settings, and click "Reinstall App".
//
// If you get "channel_not_found" error here, you need to invite your bot to the channel.
// Go to the channel, and type "/add" and select "Add apps to this channel",
// and select your bot app.
if err != nil {
t.Logf("unable to send message to channel %q: %v", c.channel, err)
}
require.NoError(t, err)
}