Skip to content

Commit dccc5d8

Browse files
committed
chore: remove a bunch of global vars
1 parent 369013a commit dccc5d8

File tree

12 files changed

+88
-119
lines changed

12 files changed

+88
-119
lines changed

examples/buttons/buttons.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ import (
1010
"github.com/slack-go/slack"
1111
)
1212

13-
var (
14-
channelID = flag.String("channel", "", "Channel ID (required)")
15-
)
16-
1713
func main() {
14+
channelID := flag.String("channel", "", "Channel ID (required)")
15+
1816
flag.Parse()
1917

2018
// Get token from environment variable

examples/connparams/connparams.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ import (
1111
"github.com/slack-go/slack"
1212
)
1313

14-
var (
15-
channelID = flag.String("channel", "", "Channel ID (required)")
16-
userIDs = flag.String("users", "", "Comma-separated user IDs for presence monitoring (required)")
17-
)
18-
1914
func main() {
15+
channelID := flag.String("channel", "", "Channel ID (required)")
16+
userIDs := flag.String("users", "", "Comma-separated user IDs for presence monitoring (required)")
2017
flag.Parse()
2118

2219
// Get token from environment variable

examples/conversation_history/conversation_history.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import (
99
"github.com/slack-go/slack"
1010
)
1111

12-
var (
13-
channelID = flag.String("channel", "", "Channel ID (required)")
14-
)
15-
1612
func main() {
13+
channelID := flag.String("channel", "", "Channel ID (required)")
14+
1715
flag.Parse()
1816

1917
// Get token from environment variable

examples/conversations_invite/conversations_invite.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import (
99
"github.com/slack-go/slack"
1010
)
1111

12-
var (
13-
channelID = flag.String("channel", "", "Channel ID (required)")
14-
userID = flag.String("user", "", "User ID to invite (required)")
15-
)
16-
1712
func main() {
13+
channelID := flag.String("channel", "", "Channel ID (required)")
14+
userID := flag.String("user", "", "User ID to invite (required)")
15+
1816
flag.Parse()
1917

2018
// Get token from environment variable

examples/messages/messages.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ import (
88
"github.com/slack-go/slack"
99
)
1010

11-
var (
12-
channelID = flag.String("channel", "", "Channel ID (required)")
13-
)
14-
1511
func main() {
12+
channelID := flag.String("channel", "", "Channel ID (required)")
1613
flag.Parse()
1714

1815
// Get token from environment variable

examples/modal/modal.go

Lines changed: 73 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,6 @@ import (
2323
"time"
2424
)
2525

26-
var (
27-
token string
28-
signingSecret string
29-
)
30-
31-
func init() {
32-
// Get token from environment variable
33-
token = os.Getenv("SLACK_BOT_TOKEN")
34-
if token == "" {
35-
fmt.Println("SLACK_BOT_TOKEN environment variable is required")
36-
os.Exit(1)
37-
}
38-
39-
// Get signing secret from environment variable
40-
signingSecret = os.Getenv("SLACK_SIGNING_SECRET")
41-
if signingSecret == "" {
42-
fmt.Println("SLACK_SIGNING_SECRET environment variable is required")
43-
os.Exit(1)
44-
}
45-
}
46-
4726
func generateModalRequest() slack.ModalViewRequest {
4827
// Create a ModalViewRequest with a header and two inputs
4928
titleText := slack.NewTextBlockObject("plain_text", "My App", false, false)
@@ -107,9 +86,14 @@ func updateModal() slack.ModalViewRequest {
10786
return modalRequest
10887
}
10988

110-
// This was taken from the slash example
111-
// https://github.com/slack-go/slack/blob/master/examples/slash/slash.go
11289
func verifySigningSecret(r *http.Request) error {
90+
// Get signing secret from environment variable
91+
signingSecret := os.Getenv("SLACK_SIGNING_SECRET")
92+
if signingSecret == "" {
93+
fmt.Println("SLACK_SIGNING_SECRET environment variable is required")
94+
os.Exit(1)
95+
}
96+
11397
verifier, err := slack.NewSecretsVerifier(r.Header, signingSecret)
11498
if err != nil {
11599
fmt.Println(err.Error())
@@ -133,76 +117,85 @@ func verifySigningSecret(r *http.Request) error {
133117
return nil
134118
}
135119

136-
func handleSlash(w http.ResponseWriter, r *http.Request) {
137-
138-
err := verifySigningSecret(r)
139-
if err != nil {
140-
fmt.Printf(err.Error())
141-
w.WriteHeader(http.StatusUnauthorized)
142-
return
143-
}
144-
145-
s, err := slack.SlashCommandParse(r)
146-
if err != nil {
147-
w.WriteHeader(http.StatusInternalServerError)
148-
fmt.Println(err.Error())
149-
return
150-
}
151-
152-
switch s.Command {
153-
case "/slash":
154-
api := slack.New(token)
155-
modalRequest := generateModalRequest()
156-
_, err = api.OpenView(s.TriggerID, modalRequest)
120+
func handleSlash(token string) func(w http.ResponseWriter, r *http.Request) {
121+
return func(w http.ResponseWriter, r *http.Request) {
122+
err := verifySigningSecret(r)
157123
if err != nil {
158-
fmt.Printf("Error opening view: %s", err)
124+
fmt.Printf(err.Error())
125+
w.WriteHeader(http.StatusUnauthorized)
126+
return
159127
}
160-
default:
161-
w.WriteHeader(http.StatusInternalServerError)
162-
return
163-
}
164-
}
165128

166-
func handleModal(w http.ResponseWriter, r *http.Request) {
129+
s, err := slack.SlashCommandParse(r)
130+
if err != nil {
131+
w.WriteHeader(http.StatusInternalServerError)
132+
fmt.Println(err.Error())
133+
return
134+
}
167135

168-
err := verifySigningSecret(r)
169-
if err != nil {
170-
fmt.Printf(err.Error())
171-
w.WriteHeader(http.StatusUnauthorized)
172-
return
136+
switch s.Command {
137+
case "/slash":
138+
api := slack.New(token)
139+
modalRequest := generateModalRequest()
140+
_, err = api.OpenView(s.TriggerID, modalRequest)
141+
if err != nil {
142+
fmt.Printf("Error opening view: %s", err)
143+
}
144+
default:
145+
w.WriteHeader(http.StatusInternalServerError)
146+
return
147+
}
173148
}
149+
}
174150

175-
var i slack.InteractionCallback
176-
err = json.Unmarshal([]byte(r.FormValue("payload")), &i)
177-
if err != nil {
178-
fmt.Printf(err.Error())
179-
w.WriteHeader(http.StatusUnauthorized)
180-
return
181-
}
151+
func handleModal(token string) func(w http.ResponseWriter, r *http.Request) {
152+
return func(w http.ResponseWriter, r *http.Request) {
153+
err := verifySigningSecret(r)
154+
if err != nil {
155+
fmt.Printf(err.Error())
156+
w.WriteHeader(http.StatusUnauthorized)
157+
return
158+
}
182159

183-
api := slack.New(token)
184-
185-
// update modal sample
186-
switch i.Type {
187-
//update when interaction type is view_submission
188-
case slack.InteractionTypeViewSubmission:
189-
//you can use any modal you want to show to users just like creating modal.
190-
updateModal := updateModal()
191-
// You must set one of external_id or view_id and you can use hash for avoiding race condition.
192-
// More details: https://api.slack.com/surfaces/modals/using#updating_apis
193-
_, err := api.UpdateView(updateModal, "", i.View.Hash, i.View.ID)
194-
// Wait for a few seconds to see result this code is necesarry due to slack server modal is going to be closed after the update
195-
time.Sleep(time.Second * 2)
160+
var i slack.InteractionCallback
161+
err = json.Unmarshal([]byte(r.FormValue("payload")), &i)
196162
if err != nil {
197-
fmt.Printf("Error updating view: %s", err)
198-
w.WriteHeader(http.StatusInternalServerError)
163+
fmt.Printf(err.Error())
164+
w.WriteHeader(http.StatusUnauthorized)
199165
return
200166
}
167+
168+
api := slack.New(token)
169+
170+
// update modal sample
171+
switch i.Type {
172+
// update when interaction type is view_submission
173+
case slack.InteractionTypeViewSubmission:
174+
// you can use any modal you want to show to users just like creating modal.
175+
updateModal := updateModal()
176+
// You must set one of external_id or view_id and you can use hash for avoiding race condition.
177+
// More details: https://api.slack.com/surfaces/modals/using#updating_apis
178+
_, err := api.UpdateView(updateModal, "", i.View.Hash, i.View.ID)
179+
// Wait for a few seconds to see result this code is necesarry due to slack server modal is going to be closed after the update
180+
time.Sleep(time.Second * 2)
181+
if err != nil {
182+
fmt.Printf("Error updating view: %s", err)
183+
w.WriteHeader(http.StatusInternalServerError)
184+
return
185+
}
186+
}
201187
}
202188
}
203189

204190
func main() {
205-
http.HandleFunc("/slash", handleSlash)
206-
http.HandleFunc("/modal", handleModal)
191+
// Get token from environment variable
192+
token := os.Getenv("SLACK_BOT_TOKEN")
193+
if token == "" {
194+
fmt.Println("SLACK_BOT_TOKEN environment variable is required")
195+
os.Exit(1)
196+
}
197+
198+
http.HandleFunc("/slash", handleSlash(token))
199+
http.HandleFunc("/modal", handleModal(token))
207200
http.ListenAndServe(":4390", nil)
208201
}

examples/pins/pins.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
// WARNING: This example is destructive in the sense that it create a channel called testpinning
1212
func main() {
1313
debug := flag.Bool("debug", false, "Show JSON output")
14-
1514
flag.Parse()
1615

1716
// Get token from environment variable
@@ -21,7 +20,7 @@ func main() {
2120
os.Exit(1)
2221
}
2322

24-
api := slack.New(apiToken, slack.OptionDebug(debug))
23+
api := slack.New(apiToken, slack.OptionDebug(*debug))
2524

2625
var (
2726
postAsUserName string

examples/reactions/reactions.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
func main() {
1212
debug := flag.Bool("debug", false, "Show JSON output")
13-
1413
flag.Parse()
1514

1615
// Get token from environment variable
@@ -20,7 +19,7 @@ func main() {
2019
os.Exit(1)
2120
}
2221

23-
api := slack.New(apiToken, slack.OptionDebug(debug))
22+
api := slack.New(apiToken, slack.OptionDebug(*debug))
2423

2524
var (
2625
postAsUserName string

examples/slash/slash.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ func main() {
1919
}
2020

2121
http.HandleFunc("/slash", func(w http.ResponseWriter, r *http.Request) {
22-
2322
verifier, err := slack.NewSecretsVerifier(r.Header, signingSecret)
2423
if err != nil {
2524
w.WriteHeader(http.StatusInternalServerError)

examples/team/team.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ import (
88
"github.com/slack-go/slack"
99
)
1010

11-
var (
12-
userID = flag.String("user", "", "User ID for billing info (optional)")
13-
)
14-
1511
func main() {
12+
userID := flag.String("user", "", "User ID for billing info (optional)")
1613
flag.Parse()
1714

1815
// Get token from environment variable

0 commit comments

Comments
 (0)