예제 #1
0
파일: users.go 프로젝트: c2h5oh/slack
func main() {
	api := slack.New("YOUR_TOKEN_HERE")
	user, err := api.GetUserInfo("U023BECGF")
	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)
}
예제 #2
0
파일: slack.go 프로젝트: xenolog/janus
func (s *Slack) Connect() error {
	//todo: check for alredy connected
	s.Api = slacklib.New(s.janusConfig.Slack.Api_token)
	s.Api.SetDebug(true)
	s.Rtm = s.Api.NewRTM()
	// start RTM main loop
	go mainSlack.Rtm.ManageConnection()

	return nil
}
예제 #3
0
파일: groups.go 프로젝트: c2h5oh/slack
func main() {
	api := slack.New("YOUR_TOKEN_HERE")
	// If you set debugging, it will log all requests to the console
	// Useful when encountering issues
	// api.SetDebug(true)
	groups, err := api.GetGroups(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)
	}
}
예제 #4
0
파일: main.go 프로젝트: madebymany/tilly
func main() {
	var err error

	slackToken := os.Getenv("SLACK_TOKEN")
	if slackToken == "" {
		log.Fatalln("You must provide a SLACK_TOKEN environment variable")
	}

	if day := time.Now().Weekday(); os.Getenv("TILLY_WEEKDAY_ONLY") != "" && (day < time.Monday || day > time.Friday) {
		log.Fatalln("Exiting; it's the weekend and I'm set to only run on a weekday")
	}

	client := slack.New(slackToken)

	auth, err := client.AuthTest()
	if err != nil {
		log.Fatalf("Couldn't log in: %s", err)
	}
	authClient := &AuthedSlack{Client: client, UserId: auth.UserId}

	slackWS := authClient.NewRTM()
	userManager := NewUserManager(authClient)
	eventReceiver := NewEventReceiver(slackWS, userManager, auth.UserId)
	go eventReceiver.Start()

	chs, err := authClient.GetChannels(true)
	if err != nil {
		log.Fatalf("Couldn't get channels: %s", err)
	}

	exitWaitGroup := new(sync.WaitGroup)

	for _, ch := range chs {
		if ch.IsGeneral || !ch.IsMember {
			continue
		}

		s := NewStandup(authClient, ch, userManager, exitWaitGroup)
		go s.Run()
	}

	exitWaitGroup.Wait()
}
예제 #5
0
파일: files.go 프로젝트: c2h5oh/slack
func main() {
	api := slack.New("YOUR_TOKEN_HERE")
	params := slack.FileUploadParameters{
		Title: "Batman Example",
		//Filetype: "txt",
		File: "example.txt",
		//Content:  "Nan Nan Nan Nan Nan Nan Nan Nan Batman",
	}
	file, err := api.UploadFile(params)
	if err != nil {
		fmt.Printf("%s\n", err)
		return
	}
	fmt.Printf("Name: %s, Url: %s\n", file.Name, file.URL)

	err = api.DeleteFile(file.Id)
	if err != nil {
		fmt.Printf("%s\n", err)
		return
	}
	fmt.Printf("File %s deleted successfully.\n", file.Name)
}
예제 #6
0
파일: websocket.go 프로젝트: c2h5oh/slack
func main() {
	api := slack.New("YOUR TOKEN HERE")
	api.SetDebug(true)

	rtm := api.NewRTM()
	go rtm.ManageConnection()

	for {
		select {
		case msg := <-rtm.IncomingEvents:
			fmt.Print("Event Received: ")
			switch ev := msg.Data.(type) {
			case *slack.HelloEvent:
				// Ignore hello

			case *slack.ConnectedEvent:
				fmt.Println("Infos:", ev.Info)
				fmt.Println("Connection counter:", ev.ConnectionCount)
				rtm.SendMessage(rtm.NewOutgoingMessage("Hello world", "#general"))

			case *slack.MessageEvent:
				fmt.Printf("Message: %v\n", ev)

			case *slack.PresenceChangeEvent:
				fmt.Printf("Presence Change: %v\n", ev)

			case *slack.LatencyReport:
				fmt.Printf("Current latency: %v\n", ev.Value)

			case *slack.SlackWSError:
				fmt.Printf("Error: %s\n", ev.Error())

			default:
				// Ignore other events..
				//fmt.Printf("Unexpected: %v\n", msg.Data)
			}
		}
	}
}
예제 #7
0
파일: auth.go 프로젝트: plotly/slick
func (mw *OAuthMiddleware) doOAuth2Roundtrip(w http.ResponseWriter, r *http.Request) (*slack.User, error) {
	code := r.FormValue("code")

	token, err := mw.oauthCfg.Exchange(oauth2.NoContext, code)
	if err != nil {
		log.Println("OAuth2: ", err)
		return nil, fmt.Errorf("Error processing token.")
	}

	client := slack.New(token.AccessToken)

	resp, err := client.AuthTest()
	if err != nil {
		return nil, fmt.Errorf("User unauthenticated: %s", err)
	}

	expectedURL := fmt.Sprintf("https://%s.slack.com/", mw.bot.Config.TeamDomain)
	if resp.URL != expectedURL {
		return nil, fmt.Errorf("Authenticated for wrong domain: %q != %q", resp.URL, expectedURL)
	}

	return mw.bot.GetUser(resp.UserId), nil
}
예제 #8
0
파일: messages.go 프로젝트: c2h5oh/slack
func main() {
	api := slack.New("YOUR_TOKEN_HERE")
	params := slack.PostMessageParameters{}
	attachment := slack.Attachment{
		Pretext: "some pretext",
		Text:    "some text",
		// Uncomment the following part to send a field too
		/*
			Fields: []slack.AttachmentField{
				slack.AttachmentField{
					Title: "a",
					Value: "no",
				},
			},
		*/
	}
	params.Attachments = []slack.Attachment{attachment}
	channelId, timestamp, err := api.PostMessage("CHANNEL_ID", "Some text", params)
	if err != nil {
		fmt.Printf("%s\n", err)
		return
	}
	fmt.Printf("Message successfully sent to channel %s at %s", channelId, timestamp)
}
예제 #9
0
파일: bot.go 프로젝트: plotly/slick
func (bot *Bot) Run() {
	bot.loadBaseConfig()

	// Write PID
	err := bot.writePID()
	if err != nil {
		log.Fatal("Couldn't write PID file:", err)
	}

	db, err := leveldb.OpenFile(bot.LevelDBConfig.Path, nil)
	if err != nil {
		log.Fatal("Could not initialize Leveldb key/value store")
	}
	defer func() {
		log.Fatal("Database is closing")
		db.Close()
	}()
	bot.DB = db

	// Init all plugins
	enabledPlugins := make([]string, 0)
	for _, plugin := range registeredPlugins {
		pluginType := reflect.TypeOf(plugin)
		if pluginType.Kind() == reflect.Ptr {
			pluginType = pluginType.Elem()
		}
		typeList := make([]string, 0)
		if _, ok := plugin.(PluginInitializer); ok {
			typeList = append(typeList, "Plugin")
		}
		if _, ok := plugin.(WebServer); ok {
			typeList = append(typeList, "WebServer")
		}
		if _, ok := plugin.(WebServerAuth); ok {
			typeList = append(typeList, "WebServerAuth")
		}
		if _, ok := plugin.(WebPlugin); ok {
			typeList = append(typeList, "WebPlugin")
		}

		log.Printf("Plugin %s implements %s", pluginType.String(),
			strings.Join(typeList, ", "))
		enabledPlugins = append(enabledPlugins, strings.Replace(pluginType.String(), ".", "_", -1))
	}

	initChatPlugins(bot)
	initWebServer(bot, enabledPlugins)
	initWebPlugins(bot)

	if bot.WebServer != nil {
		go bot.WebServer.RunServer()
	}

	bot.Slack = slack.New(bot.Config.ApiToken)
	bot.Slack.SetDebug(bot.Config.Debug)

	rtm := bot.Slack.NewRTM()
	bot.rtm = rtm

	bot.setupHandlers()

	bot.rtm.ManageConnection()
}