func messageHandler(api *slack.Slack, s *SlackService, h *helios.Engine) { for { select { case msg := <-s.slackEvents: switch msg.Data.(type) { case *slack.MessageEvent: channelName := "" event := msg.Data.(*slack.MessageEvent) if channel, ok := s.Channels[event.ChannelId]; ok { channelName = channel.Name } s.Messages <- helios.NewMessage(channelName) // Find only the first mention at the begining of the string re := regexp.MustCompile(`^<@(.*?)>:? (\w+)\s?(.*)?`) match := re.FindStringSubmatch(event.Text) if len(match) < 3 { continue } // Validate that user id exists if u, ok := s.Users[match[1]]; ok { // Commands can only be sent to the defined name and a valid bot botName := h.Config.GetString("slack.botName") if u.Name == botName && u.IsBot { args := []string{} // If we have more than the username and command then process the rest as args if len(match) > 3 { re := regexp.MustCompile(`"[^"]+"|[\w]+`) // Match individual words and quoted strings args = re.FindAllString(match[3], -1) } // Wrap the command information command := Command{ Name: match[2], Args: args, } s.Commands <- helios.NewMessage(command) } } } } } panic("Shouldn't be here") }
func userRoutine(u User, g *GithubService) error { ts := tokenSource{ &oauth2.Token{ AccessToken: u.AccessToken, }, } tc := oauth2.NewClient(oauth2.NoContext, ts) client := github.NewClient(tc) //List Options Page, PerPage opts := github.ListOptions{1, 1} for { events, resp, err := client.Activity.ListEventsPerformedByUser(u.Username, false, &opts) if err != nil { g.EventChan <- helios.NewError("Problem retrieving events for user: %s. Error: %v", u.Username, err) } newEventTime := g.LastEvent.EventTime if len(events) > 0 { newEventTime = *events[0].CreatedAt } // read in last event and compare to new event time g.LastEvent.Lock() dur := g.LastEvent.EventTime.Sub(newEventTime) if dur.Seconds() > 0.0 { g.LastEvent.EventTime = newEventTime g.EventChan <- helios.NewMessage(events[0]) } g.LastEvent.Unlock() // Wait as long as the X-Poll-Interval header says to interval, err := strconv.ParseInt(resp.Header["X-Poll-Interval"][0], 10, 8) if err != nil { // if strconv failed for whatever reason, use the default X-Poll-Interval value of 60 time.Sleep(60 * time.Second) } else { time.Sleep(time.Duration(interval) * time.Second) } } panic("Shouldn't be here") }