Example #1
0
func connectSlackRTM(h *helios.Engine, s *SlackService) {
	err := backoff.Retry(func() error {
		h.Debug("Connecting to slack rtm")
		err := runSlackRTM(h, s)
		if err != nil {
			h.Warn("Failed to start slack rtm. Retrying.", "error", err.Error())
		}
		return err
	}, backoff.NewExponentialBackOff())
	if err != nil {
		s.Messages <- helios.NewError("Slack service error: %v", err)
	}
}
Example #2
0
func providerCallback(h *helios.Engine, g *GithubService) gin.HandlerFunc {
	return func(c *gin.Context) {
		var user User

		// Run user auth using the gothic library
		githubUser, err := gothic.CompleteUserAuth(c.Writer, c.Request)
		if err != nil {
			h.Warn("Failed to create user from callback", "error", err.Error())
		}

		user.Username = githubUser.RawData["login"].(string)
		user.AccessToken = githubUser.AccessToken

		// If the user doesn't exist yet
		if _, ok := g.Users[user.Username]; !ok {
			userFile, err := os.OpenFile("users.csv", os.O_APPEND|os.O_WRONLY, 0644)
			defer userFile.Close()

			_, err = userFile.WriteString(fmt.Sprintf("%s,%s\n", user.Username, user.AccessToken))
			if err != nil {
				h.Error("Failed to write new users to CSV", "error", err.Error())
			}

			g.Users[user.Username] = user
			// startUser(user)

		} else {
			h.Info("User already exists")
		}

		c.JSON(200, user)
	}
}
Example #3
0
func runSlackRTM(h *helios.Engine, s *SlackService) error {
	token := h.Config.GetString("slack.apiKey")
	s.slackEvents = make(chan slack.SlackEvent)
	api := slack.New(token)

	rtm, err := api.StartRTM("", "http://localhost/")
	if err != nil {
		return err
	}

	h.Debug("Connected to slack rtm")

	// Cache all users for mention lookups
	users, err := api.GetUsers()
	if err != nil {
		h.Warn("Failed to get users from slack api")
		return err
	}

	// Map all users ids to user type
	for _, u := range users {
		s.Users[u.Id] = u
	}

	// Cache all channels for lookups
	channels, err := api.GetChannels(true)
	if err != nil {
		h.Warn("Failed to get channels from slack api")
		return err
	}

	// Map all channel ids to channel type
	for _, c := range channels {
		s.Channels[c.Id] = c
	}

	go rtm.HandleIncomingEvents(s.slackEvents)
	go rtm.Keepalive(20 * time.Second)
	go messageHandler(api, s, h)

	return nil
}