예제 #1
0
func main() {
	var (
		apiToken string
		debug    bool
	)

	flag.StringVar(&apiToken, "token", "YOUR_TOKEN_HERE", "Your Slack API Token")
	flag.BoolVar(&debug, "debug", false, "Show JSON output")
	flag.Parse()

	api := slack.New(apiToken)
	if debug {
		api.SetDebug(true)
	}

	// Get all stars for the usr.
	params := slack.NewStarsParameters()
	starredItems, _, err := api.GetStarred(params)
	if err != nil {
		fmt.Printf("Error getting stars: %s\n", err)
		return
	}
	for _, s := range starredItems {
		var desc string
		switch s.Type {
		case slack.TYPE_MESSAGE:
			desc = s.Message.Text
		case slack.TYPE_FILE:
			desc = s.File.Name
		case slack.TYPE_FILE_COMMENT:
			desc = s.File.Name + " - " + s.Comment.Comment
		case slack.TYPE_CHANNEL, slack.TYPE_IM, slack.TYPE_GROUP:
			desc = s.Channel
		}
		fmt.Printf("Starred %s: %s\n", s.Type, desc)
	}
}
예제 #2
0
func main() {
	// Read config.toml
	config, err := toml.LoadFile("config.toml")
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error opening config file. Did you remember to `cp config.toml.example config.toml`?\n", err.Error())
		os.Exit(1)
	}

	apiToken := config.Get("slack-quoter.loader_api_token").(string)
	channelKey := config.Get("slack-quoter.channel_key").(string)
	mongoDBServer := config.Get("slack-quoter.mongodb_server").(string)

	session, err := mgo.Dial(mongoDBServer)
	if err != nil {
		panic(err)
	}
	defer session.Close()
	col := session.DB("slack").C("quotes")

	api := slack.New(apiToken)

	// Get all users.
	users, err := api.GetUsers()
	if err != nil {
		panic(err)
	}

	// Get all starred items.
	starParams := slack.NewStarsParameters()
	for _, user := range users {
		if user.IsBot {
			fmt.Println("BLEEP BLOOP")
			continue
		}
		fmt.Printf("Trying user ID %s with name %s\n", user.ID, user.Name)
		pages := 1
		starParams.User = user.ID
		starParams.Page = 1
		for starParams.Page <= pages {
			stars, paging, err := api.ListStars(starParams)
			if err != nil {
				panic(err)
			}
			for _, star := range stars {
				if star.Type == "message" {
					col.Upsert(star.Message.Msg, star.Message.Msg)
				}
			}
			pages = paging.Pages
			starParams.Page++
		}
	}

	// Get all pins.
	fmt.Println("Grabbing pins....")
	items, _, err := api.ListPins(channelKey)
	if err != nil {
		panic(err)
	}
	for _, item := range items {
		if item.Type == "message" {
			col.Upsert(item.Message.Msg, item.Message.Msg)
		}
	}
}