From 9c89850eaab7512e94fdda4765f678e618774fcd Mon Sep 17 00:00:00 2001 From: Omid Marfavi <21163286+marfavi@users.noreply.github.com> Date: Tue, 12 Nov 2024 17:58:19 +0100 Subject: [PATCH 1/2] add basic functions --- get_all_groups.go | 22 ++++++++++++++++++++++ get_user.go | 27 +++++++++++++++++++++++++++ main.go | 3 +++ slack_token.go | 7 +++++++ 4 files changed, 59 insertions(+) create mode 100644 get_all_groups.go create mode 100644 get_user.go create mode 100644 main.go create mode 100644 slack_token.go diff --git a/get_all_groups.go b/get_all_groups.go new file mode 100644 index 0000000..f00739c --- /dev/null +++ b/get_all_groups.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + + "github.com/slack-go/slack" +) + +func getAllUserGroups() { + api := slack.New(slack_token()) + // If you set debugging, it will log all requests to the console + // Useful when encountering issues + // slack.New(slack_token(), slack.OptionDebug(true)) + groups, err := api.GetUserGroups(slack.GetUserGroupsOptionIncludeUsers(false)) + if err != nil { + fmt.Printf("%s\n", err) + return + } + for _, group := range groups { + fmt.Printf("ID: %s, Name: %s\n", group.ID, group.Name) + } +} diff --git a/get_user.go b/get_user.go new file mode 100644 index 0000000..e171625 --- /dev/null +++ b/get_user.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + + "github.com/slack-go/slack" +) + +func getUserById(userId string) { + api := slack.New(slack_token()) + user, err := api.GetUserInfo(userId) + if err != nil { + fmt.Printf("%s\n", err) + return + } + fmt.Printf("ID: %s, Fullname: %s, Email: %s\n", user.ID, user.Profile.RealName, user.Profile.Email) +} + +func getUserByEmail(email string) { + api := slack.New(slack_token()) + user, err := api.GetUserByEmail(email) + if err != nil { + fmt.Printf("%s\n", err) + return + } + fmt.Printf("ID: %s, Fullname: %s, Email: %s\n", user.ID, user.Profile.RealName, user.Profile.Email) +} \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..38dd16d --- /dev/null +++ b/main.go @@ -0,0 +1,3 @@ +package main + +func main() {} diff --git a/slack_token.go b/slack_token.go new file mode 100644 index 0000000..d3b11ce --- /dev/null +++ b/slack_token.go @@ -0,0 +1,7 @@ +package main + +import "os" + +func slack_token() string { + return os.Getenv("SLACK_TOKEN") +} \ No newline at end of file From f34d1bc28864890457ad3b8428700b4478c31105 Mon Sep 17 00:00:00 2001 From: Omid Marfavi <21163286+marfavi@users.noreply.github.com> Date: Tue, 12 Nov 2024 18:29:58 +0100 Subject: [PATCH 2/2] minor improvements --- get_user.go | 12 ++++++------ slack_token.go | 7 +++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/get_user.go b/get_user.go index e171625..3e7185e 100644 --- a/get_user.go +++ b/get_user.go @@ -6,22 +6,22 @@ import ( "github.com/slack-go/slack" ) -func getUserById(userId string) { +func getUserById(userId string) *slack.User { api := slack.New(slack_token()) user, err := api.GetUserInfo(userId) if err != nil { fmt.Printf("%s\n", err) - return + panic(err) } - fmt.Printf("ID: %s, Fullname: %s, Email: %s\n", user.ID, user.Profile.RealName, user.Profile.Email) + return user } -func getUserByEmail(email string) { +func getUserByEmail(email string) *slack.User { api := slack.New(slack_token()) user, err := api.GetUserByEmail(email) if err != nil { fmt.Printf("%s\n", err) - return + panic(err) } - fmt.Printf("ID: %s, Fullname: %s, Email: %s\n", user.ID, user.Profile.RealName, user.Profile.Email) + return user } \ No newline at end of file diff --git a/slack_token.go b/slack_token.go index d3b11ce..e3369fd 100644 --- a/slack_token.go +++ b/slack_token.go @@ -3,5 +3,8 @@ package main import "os" func slack_token() string { - return os.Getenv("SLACK_TOKEN") -} \ No newline at end of file + if token, ok := os.LookupEnv("SLACK_TOKEN"); ok { + return token + } + panic("SLACK_TOKEN environment variable not set") +}