forked from essentialkaos/slacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
147 lines (122 loc) · 3.79 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package slack
import (
"context"
"errors"
"fmt"
"log"
"net/url"
"os"
)
// Added as a var so that we can change this for testing purposes
var (
SLACK_API string = "https://slack.com/api/"
SLACK_WEB_API_FORMAT string = "https://%s.slack.com/api/users.admin.%s?t=%s"
)
type SlackResponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
}
type AuthTestResponse struct {
URL string `json:"url"`
Team string `json:"team"`
User string `json:"user"`
TeamID string `json:"team_id"`
UserID string `json:"user_id"`
}
type ClientConfig struct {
BatchPresenceAware bool // Only deliver presence events when requested by subscription
IncludeLocale bool // Set this to true to receive the locale for users and channels
MPIMAware bool // Returns MPIMs to the client in the API response
NoLatest bool // Exclude latest timestamps for channels, groups, mpims, and ims
NoUnreads bool // Skip unread counts for each channel (improves performance)
SimpleLatest bool // Return timestamp only for latest message object of each channel (improves performance)
PresenceSub bool // Set this to true if you plan to subscribe to presence events
token string // Authentication token bearing required scopes
}
type Client struct {
Config *ClientConfig
debug bool
}
// New creates new Client.
func New(token string) *Client {
return &Client{
Config: &ClientConfig{
token: token,
BatchPresenceAware: true,
PresenceSub: true,
},
debug: false,
}
}
type authTestResponseFull struct {
SlackResponse
AuthTestResponse
}
// AuthTest tests if the user is able to do authenticated requests or not
func (api *Client) AuthTest() (response *AuthTestResponse, error error) {
return api.AuthTestContext(context.Background())
}
// AuthTestContext tests if the user is able to do authenticated requests or not with a custom context
func (api *Client) AuthTestContext(ctx context.Context) (response *AuthTestResponse, error error) {
api.Debugf("Challenging auth...")
responseFull := &authTestResponseFull{}
err := post(ctx, "auth.test", url.Values{"token": {api.Config.token}}, responseFull, api.debug)
if err != nil {
api.Debugf("failed to test for auth: %s", err)
return nil, err
}
if !responseFull.Ok {
api.Debugf("auth response was not Ok: %s", responseFull.Error)
return nil, errors.New(responseFull.Error)
}
api.Debugf("Auth challenge was successful with response %+v", responseFull.AuthTestResponse)
return &responseFull.AuthTestResponse, nil
}
// SetDebug switches the api into debug mode
// When in debug mode, it logs various info about what its doing
// If you ever use this in production, don't call SetDebug(true)
func (api *Client) SetDebug(debug bool) {
api.debug = debug
if debug && logger == nil {
SetLogger(log.New(os.Stdout, "nlopes/slack", log.LstdFlags|log.Lshortfile))
}
}
// Debugf print a formatted debug line.
func (api *Client) Debugf(format string, v ...interface{}) {
if api.debug {
logger.Output(2, fmt.Sprintf(format, v...))
}
}
// Debugln print a debug line.
func (api *Client) Debugln(v ...interface{}) {
if api.debug {
logger.Output(2, fmt.Sprintln(v...))
}
}
func (config *ClientConfig) toParams() url.Values {
arguments := url.Values{}
arguments.Add("token", config.token)
if config.BatchPresenceAware {
arguments.Add("batch_presence_aware", "1")
}
if config.IncludeLocale {
arguments.Add("include_locale", "1")
}
if config.MPIMAware {
arguments.Add("mpim_aware", "1")
}
if config.NoLatest {
arguments.Add("no_latest", "1")
arguments.Add("no_unreads", "1")
}
if config.NoUnreads {
arguments.Add("no_unreads", "1")
}
if config.SimpleLatest {
arguments.Add("simple_latest", "1")
}
if config.PresenceSub {
arguments.Add("presence_sub", "1")
}
return arguments
}