func main() { ParcelSnatcher := map[string]ProcessingMethod{ "slack": SnatchSlackParcels, } pirateConf, err := confman.LoadJson(fmt.Sprint(confman.GetThisFolder(), "/db.conf")) if err != nil { fatal("ParcelPirate Conf Failed to Load", err) } // todo: include support for other database types Jug = polyjug.New("sqlite3", pirateConf["targetDatabase"].(string)) supportedParcelTypes := map[string]int{ // := pirateConf["parcelTypes"].(map[string]int) "slack": 1, //"lync":2, //"smtp":3, //"sms":4, //"voice":5, //"flowdock":6, } for parcelTypeName, parcelTypeId := range supportedParcelTypes { parcels, err := ParcelSnatcher[parcelTypeName]() if err != nil { fatal(fmt.Sprintf("ParcelProcessor %s failed", parcelTypeName), err) } if len(parcels) > 0 { fmt.Printf("snatched %v parcels from %s\n", len(parcels), parcelTypeName) receivedMessages, err := CreateReceivedMessages(&parcels, parcelTypeId) if err != nil { fatal("Unable to convert []RawApiMessages to []jugger.ReceivedMessage", err) } fmt.Printf("burying %s booty!\n", parcelTypeName) err = SaveNewReceivedMessages(&receivedMessages) if err != nil { fatal("Unable to save new []jugger.ReceivedMessage in database", err) } } else { fmt.Printf("No parcels found while sailing near %s\n", parcelTypeName) } } fmt.Println("done") }
func SnatchSlackParcels() ([]RawApiMessage, error) { //todo: defautlt to db lookup, then failback to config file. if neither exist. exit. // "SELECT key, value FROM ConfigTable WHERE key IN('token', 'channel', 'lastRunTime') AND OwnerName = '%s'" confMap, err := confman.LoadJson(confman.GetThisFolder() + "/slack.conf") if err != nil { return nil, err } chanSlice := make([]string, len(confMap["channels"].([]interface{}))) for i, cha := range confMap["channels"].([]interface{}) { chanSlice[i] = cha.(string) } slackConf := slack.SlackConfig{ Token: confMap["token"].(string), Channels: chanSlice, LastRunTime: confMap["lastRunTime"].(string), } slack.Configure(slackConf.Token) channelIds := slackConf.Channels if len(channelIds) == 0 { channelIds, err = slack.GetChannelIds() if err != nil { return nil, err } } allMessages := make([]RawApiMessage, 0) oldestTime := slackConf.LastRunTime for _, channelId := range channelIds { // this return type needs to have generic keys across all types. // use slack as the model citizen (design a struct or interface that deals with this) slackMessages, err := slack.GetChannelMessagesSinceLastRun(channelId, oldestTime) if err != nil { return nil, err } channelMessages := make([]RawApiMessage, 0) for _, mess := range slackMessages { if _, ok := mess["user"]; ok { //ensure that only real user messages are stored. (slack bot messages do not have a user property) channelMessages = append(channelMessages, RawApiMessage{ channel: channelId, //this will be the current channel in this for loop user: mess["user"].(string), text: mess["text"].(string), ts: mess["ts"].(string), }) } } if len(channelMessages) > 0 { allMessages = append(allMessages, channelMessages...) messageEpoch, _ := strconv.ParseUint(channelMessages[0].ts[:10], 10, 64) configEpoch, _ := strconv.ParseUint(slackConf.LastRunTime[:10], 10, 64) if messageEpoch > configEpoch { slackConf.LastRunTime = channelMessages[0].ts } } } if len(allMessages) == 0 { slackConf.LastRunTime = fmt.Sprintf("%v.000000", time.Now().UTC().Unix()) } // todo: error check this guy... confman.SaveJson(confman.GetThisFolder()+"/slack.conf", confman.StructToMap(slackConf)) return allMessages, nil }