Beispiel #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)
}
Beispiel #2
0
// auth receives the callback from Slack, validates and displays the user information
func auth(w http.ResponseWriter, r *http.Request) {
	state := r.FormValue("state")
	code := r.FormValue("code")
	errStr := r.FormValue("error")
	if errStr != "" {
		writeError(w, 401, errStr)
		return
	}
	if state == "" || code == "" {
		writeError(w, 400, "Missing state or code")
		return
	}
	if state != globalState.auth {
		writeError(w, 403, "State does not match")
		return
	}
	// As an example, we allow only 5 min between requests
	if time.Since(globalState.ts) > 5*time.Minute {
		writeError(w, 403, "State is too old")
		return
	}
	token, err := slack.OAuthAccess(*clientID, *clientSecret, code, "")
	if err != nil {
		writeError(w, 401, err.Error())
		return
	}
	s, err := slack.New(slack.SetToken(token.AccessToken))
	if err != nil {
		writeError(w, 500, err.Error())
		return
	}
	// Get our own user id
	test, err := s.AuthTest()
	if err != nil {
		writeError(w, 500, err.Error())
		return
	}
	w.Write([]byte(fmt.Sprintf("OAuth successful for team %s and user %s", test.Team, test.User)))
}
Beispiel #3
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)
}
Beispiel #4
0
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)
}