예제 #1
0
func TestConfLoad() {
	fmt.Println("\n................................................\n")

	confMap, err := confman.LoadJson("C:\\work\\git\\project_juggernaut\\bin\\slack.conf")

	fmt.Println("Conf Map", confMap, err)

	chans := make([]string, len(confMap["channels"].([]interface{})))

	for i, cha := range confMap["channels"].([]interface{}) {
		chans[i] = cha.(string)
	}

	slackConf := slack.SlackConfig{
		Token:       confMap["token"].(string),
		Channels:    chans,
		LastRunTime: confMap["lastRunTime"].(string),
	}

	fmt.Println("chan length", len(slackConf.Channels))

	channels := slackConf.Channels

	raw, err := slack.GetChannelIds(slackConf.Token)

	fmt.Println("raw chans", raw, err)

	if len(channels) == 0 {
		channels, err = slack.GetChannelIds(slackConf.Token)
	}

	fmt.Println("slackConf", slackConf)

	fmt.Println("channels", channels, err)
}
예제 #2
0
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
}