@@ -97,6 +97,11 @@ func main() {
9797 userIDs = append (userIDs , userID )
9898 }
9999
100+ if len (userIDs ) == 0 {
101+ fmt .Println ("\n No users found - aborting" )
102+ return
103+ }
104+
100105 // get all channels
101106 channelNameToIDMap , err := getChannels (apiToken , private , debug )
102107 if err != nil {
@@ -130,11 +135,20 @@ func main() {
130135}
131136
132137func getUserID (apiToken , userEmail string ) (string , error ) {
138+ httpClient := & http.Client {}
133139
134140 // lookup user by email
135- resp , err := http .Get ( usersLookupByEmailURL + fmt .Sprintf ("?token=%s& email=%s" , apiToken , userEmail ))
141+ req , err := http .NewRequest ( http . MethodGet , fmt .Sprintf (usersLookupByEmailURL + "? email=%s" , userEmail ), nil )
136142 if err != nil {
137- panic (err )
143+ return "" , err
144+ }
145+
146+ req .Header .Add ("Content-Type" , "application/x-www-form-urlencoded" )
147+ req .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , apiToken ))
148+
149+ resp , err := httpClient .Do (req )
150+ if err != nil {
151+ return "" , err
138152 }
139153 defer resp .Body .Close ()
140154
@@ -170,12 +184,21 @@ func getChannels(apiToken string, private bool, debug bool) (map[string]string,
170184
171185 nameToID := make (map [string ]string )
172186
187+ httpClient := & http.Client {}
173188 var nextCursor string
174189 for {
175190 // query list of channels
176- resp , err := http .Get (conversationsListURL + fmt .Sprintf ("?token=%s&cursor=%s&exclude_archived=true&limit=200&types=%s" , apiToken , nextCursor , channelType ))
191+ req , err := http .NewRequest (http .MethodGet , fmt .Sprintf (conversationsListURL + "?cursor=%s&exclude_archived=true&limit=200&types=%s" , nextCursor , channelType ), nil )
192+ if err != nil {
193+ return nil , err
194+ }
195+
196+ req .Header .Add ("Content-Type" , "application/x-www-form-urlencoded" )
197+ req .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , apiToken ))
198+
199+ resp , err := httpClient .Do (req )
177200 if err != nil {
178- panic ( err )
201+ return nil , err
179202 }
180203 defer resp .Body .Close ()
181204
@@ -228,15 +251,15 @@ func inviteUsersToChannel(apiToken string, userIDs []string, channelID string) e
228251 return err
229252 }
230253
231- request , err := http .NewRequest (http .MethodPost , conversationsInviteURL , bytes .NewReader (reqBody ))
254+ req , err := http .NewRequest (http .MethodPost , conversationsInviteURL , bytes .NewReader (reqBody ))
232255 if err != nil {
233256 return err
234257 }
235258
236- request .Header .Add ("Content-Type" , "application/json" )
237- request .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , apiToken ))
259+ req .Header .Add ("Content-Type" , "application/json" )
260+ req .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , apiToken ))
238261
239- resp , err := httpClient .Do (request )
262+ resp , err := httpClient .Do (req )
240263 if err != nil {
241264 return err
242265 }
0 commit comments