Example #1
0
func get_channel(rtm *slack.RTM, lookfor string) (string, error) {
	has_hash := strings.HasPrefix(strings.Trim(lookfor, " "), "#")
	if has_hash {
		lookfor = strings.TrimPrefix(strings.Trim(lookfor, " "), "#")
	}
	t := time.Now()
	ts := t.Format("Mon Jan 2 15:04:05 -0700 MST 2006")

	// Try public channels first
	l, err := rtm.GetChannels(false)
	if err != nil {
		fmt.Printf("[%s] ERROR Channel list error: %s\n", ts, err)
	}
	for _, v := range l {
		if v.Name == lookfor {
			return v.ID, nil
		}
	}

	// Now private channels, or groups in slack terms
	g, err := rtm.GetGroups(false)
	if err != nil {
		fmt.Printf("[%s] ERROR Channel list error: %s\n", ts, err)
	}
	for _, v := range g {
		if v.Name == lookfor {
			return v.ID, nil
		}
	}

	return "", errors.New("No channel found with this name")
}