Skip to content

Instantly share code, notes, and snippets.

@yukpiz yukpiz/channel.go
Created Oct 11, 2018

Embed
What would you like to do?
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
type Data struct {
Ok bool `json:"ok"`
Channels []Channel `json:"channels"`
}
type Channel struct {
ID string `json:"id"`
Name string `json:"name"`
IsChannel bool `json:"is_channel"`
Created int32 `json:"created"`
IsArchived bool `json:"is_archived"`
IsGeneral bool `json:"is_general"`
Unlinked int32 `json:"unlinked"`
Creator string `json:"creator"`
NameNormalized string `json:"name_normalized"`
IsShared bool `json:"is_shared"`
IsOrgShared bool `json:"is_org_shared"`
IsMember bool `json:"is_member"`
IsPrivate bool `json:"is_private"`
IsMpim bool `json:"is_mpim"`
Members []string `json:"members"`
Topic Topic `json:"topic"`
Purpose Purpose `json:"purpose"`
PreviousNames []string `json:"previous_names"`
NumMembers int32 `json:"num_members"`
}
type Topic struct {
Value string `json:"value"`
Creator string `json:"creator"`
LastSet int32 `json:"last_set"`
}
type Purpose struct {
Value string `json:"value"`
Creator string `json:"creator"`
LastSet int32 `json:"last_set"`
}
func main() {
url := "https://slack.com/api/channels.list?token={YOUR_TOKEN}&exclude_archived=true&pretty=1"
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var data Data
json.Unmarshal(bytes, &data)
c1 := []string{}
c2 := []string{}
for _, c := range data.Channels {
if c.IsPrivate || c.IsArchived {
continue
}
if strings.Index(c.Name, "time") < 0 {
c1 = append(c1, c.Name)
continue
}
c2 = append(c2, c.Name)
}
fmt.Println("=== Times Channels ======")
for _, n := range c2 {
fmt.Printf("#%s\n", n)
}
fmt.Println("")
fmt.Println("=== Public Channels ======")
for _, n := range c1 {
fmt.Printf("#%s\n", n)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.