Example #1
0
func main() {
	flag.Parse()
	if *file == "" {
		check(fmt.Errorf("Please provide the filename to upload"))
	}
	s, err := slack.New(
		slack.SetToken(*token),
		slack.SetErrorLog(log.New(os.Stderr, "ERR:", log.Lshortfile)),
		slack.SetTraceLog(log.New(os.Stderr, "DEBUG:", log.Lshortfile)))
	check(err)
	f, err := os.Open(*file)
	check(err)
	defer f.Close()
	// Translate channel
	channels, err := s.ChannelList(true)
	var channelID string
	for i := range channels.Channels {
		if strings.ToLower(channels.Channels[i].Name) == strings.ToLower(*channel) {
			channelID = channels.Channels[i].ID
			break
		}
	}
	if channelID == "" {
		check(fmt.Errorf("Channel %s not found", *channel))
	}
	resp, err := s.Upload(*title, "", filepath.Base(*file), *comment, []string{channelID}, f)
	check(err)
	fmt.Printf("Response: %v", resp)
}
Example #2
0
func main() {
	flag.Parse()
	s, err := slack.New(
		slack.SetToken(*token),
		slack.SetErrorLog(log.New(os.Stderr, "ERR:", log.Lshortfile)),
		slack.SetTraceLog(log.New(os.Stderr, "DEBUG:", log.Lshortfile)))
	check(err)
	// Translate channel
	channels, err := s.ChannelList(true)
	var channelIDs []string
	channelNames := strings.Split(*channelNamesStr, ",")
	for i := range channelNames {
		for j := range channels.Channels {
			if strings.ToLower(channels.Channels[j].Name) == strings.ToLower(strings.TrimSpace(channelNames[i])) {
				channelIDs = append(channelIDs, channels.Channels[j].ID)
				break
			}
		}
	}
	groups, err := s.GroupList(true)
	check(err)
	for i := range channelNames {
		for j := range groups.Groups {
			if strings.ToLower(groups.Groups[j].Name) == strings.ToLower(strings.TrimSpace(channelNames[i])) {
				channelIDs = append(channelIDs, groups.Groups[j].ID)
				break
			}
		}
	}
	inviteType := slack.InviteeRegular
	if *t == "restricted" {
		inviteType = slack.InviteeRestricted
	} else if *t == "ultra" {
		inviteType = slack.InviteeUltraRestricted
	}
	err = s.InviteToSlack(slack.UserInviteDetails{Email: *email, FirstName: *first, LastName: *last}, channelIDs, inviteType)
	check(err)
}
Example #3
0
File: scli.go Project: dorsha/slack
func main() {
	flag.Parse()
	expandHome()
	if *verbose {
		log.Printf("Using configuration file %s and history file %s\n", *conf, *hist)
	}
	err := Load(*conf)
	if err != nil && *verbose {
		log.Println("Unable load configuration, using defaults")
	}
	if *token != "" {
		Options.Token = *token
	}
	if Options.Token == "" {
		log.Println("Please provide the token from - https://api.slack.com/web")
		os.Exit(1)
	}
	if *channel != "" {
		Options.DefaultChannel = *channel
	}

	// Let's make sure that the token is valid before anything else
	s, err = slack.New(slack.SetErrorLog(log.New(os.Stderr, "", log.Lshortfile)), slack.SetToken(Options.Token))
	check(err)
	if *debug {
		slack.SetTraceLog(log.New(os.Stderr, "", log.Lshortfile))(s)
	}
	test, err := s.AuthTest()
	if err != nil {
		log.Println("Unable to authenticate to Slack: ", err)
	}
	if *verbose {
		log.Printf("Logged in as %s to team %s\n", test.User, test.Team)
	}

	line := liner.NewLiner()
	defer line.Close()

	if f, err := os.Open(*hist); err == nil {
		line.ReadHistory(f)
		f.Close()
	}
	line.SetWordCompleter(completer)
	line.SetTabCompletionStyle(liner.TabPrints)

	var stop []chan bool
	if liner.TerminalSupported() && !line.InputRedirected() {
		stopHistory := make(chan bool)
		go saveHistory(line, stopHistory)
		stop = append(stop, stopHistory)

		in := make(chan *slack.Message)
		info, err = s.RTMStart("", in, nil)
		check(err)
		if !switchChannel(Options.DefaultChannel) {
			fmt.Printf("Default channel %s not found\n", Options.DefaultChannel)
			for i := range info.Channels {
				if info.Channels[i].IsGeneral {
					currChannelID = info.Channels[i].ID
					fmt.Printf("Using %s as initial channel\n", channelName(currChannelID))
					break
				}
			}
		}
		stopReceiving := make(chan bool)
		stop = append(stop, stopReceiving)
		go receiveMessages(line, s, in, stopReceiving)
	} else {
		loadInfo()
	}
	if *shouldLoadFiles {
		loadFiles()
	}

	// The prompt loop
	for {
		if data, err := line.Prompt(channelName(currChannelID) + "> "); err != nil {
			if err != io.EOF && err != liner.ErrNotTerminalOutput {
				log.Print("Error reading line: ", err)
			}
			break
		} else {
			if len(data) == 0 {
				continue
			}
			line.AppendHistory(data)
			if strings.HasPrefix(data, Options.CommandPrefix) {
				shouldExit := handleCommand(data)
				if shouldExit {
					break
				}
			} else {
				go postMessage(data)
			}
		}
	}
	if !liner.TerminalSupported() || line.InputRedirected() {
		// Wait a bit to finish processing all the send messages
		time.Sleep(time.Second * 5)
	}
	cleanup(stop)
}