-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move standalone api methods to own file
- Loading branch information
Showing
3 changed files
with
76 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/bluele/slack" | ||
) | ||
|
||
var ( | ||
api *slack.Slack | ||
msgOpts = &slack.ChatPostMessageOpt{AsUser: true} | ||
) | ||
|
||
func InitAPI(token string) { | ||
api = slack.New(token) | ||
res, err := api.AuthTest() | ||
failOnError(err, "Slack API Error") | ||
output(fmt.Sprintf("connected to %s as %s", res.Team, res.User)) | ||
} | ||
|
||
// Return list of all channels by name | ||
func listChannels() (names []string) { | ||
list, err := api.ChannelsList() | ||
failOnError(err) | ||
for _, c := range list { | ||
names = append(names, c.Name) | ||
} | ||
return names | ||
} | ||
|
||
// Return list of all groups by name | ||
func listGroups() (names []string) { | ||
list, err := api.GroupsList() | ||
failOnError(err) | ||
for _, c := range list { | ||
names = append(names, c.Name) | ||
} | ||
return names | ||
} | ||
|
||
// Return list of all ims by name | ||
func listIms() (names []string) { | ||
users, err := api.UsersList() | ||
failOnError(err) | ||
|
||
list, err := api.ImList() | ||
failOnError(err) | ||
for _, c := range list { | ||
for _, u := range users { | ||
if u.Id == c.User { | ||
names = append(names, u.Name) | ||
continue | ||
} | ||
} | ||
} | ||
return names | ||
} | ||
|
||
// Lookup Slack id for channel, group, or im by name | ||
func lookupSlackID(name string) string { | ||
if channel, err := api.FindChannelByName(name); err == nil { | ||
return channel.Id | ||
} | ||
if group, err := api.FindGroupByName(name); err == nil { | ||
return group.Id | ||
} | ||
if im, err := api.FindImByName(name); err == nil { | ||
return im.Id | ||
} | ||
exitErr(fmt.Errorf("No such channel, group, or im")) | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters