Skip to content

Instantly share code, notes, and snippets.

@fain182
Created October 26, 2015 22:25
Show Gist options
  • Save fain182/e6bc61120e8073542925 to your computer and use it in GitHub Desktop.
Save fain182/e6bc61120e8073542925 to your computer and use it in GitHub Desktop.
Which are you most happy slack channels?
package main
import (
"fmt"
"github.com/nlopes/slack"
"strconv"
"strings"
"time"
)
type HappinessIndicator struct {
Name string
Score int
}
func main() {
smiles := []string{":-)", ":smile:", ":-D", ":joy:", ";-)", ":wink:"}
api := slack.New("PUT HERE YOUR SLACK TOKEN")
//api.SetDebug(true)
channels, err := api.GetChannels(true)
if err != nil {
fmt.Printf("%s\n", err)
return
}
for _, channel := range channels {
params := slack.NewHistoryParameters()
params.Count = 1000
beginOfMonth, err := time.Parse("2006-01-02", "2015-10-01")
params.Oldest = strconv.FormatInt(beginOfMonth.Unix(), 10)
history, err := api.GetChannelHistory(channel.ID, params)
if err != nil {
fmt.Printf("%s\n", err)
return
}
if len(history.Messages) == 0 {
continue
}
happiness := 0
for _, message := range history.Messages {
for _, smile := range smiles {
if strings.Contains(message.Text, smile) {
happiness += 1
}
}
}
//fmt.Printf("%d smiles - %s ( %d messages sent ) \n", happiness, channel.Name, len(history.Messages))
percent := (float32(happiness) / float32(len(history.Messages))) * 100.0
fmt.Printf("%.2f %% of happy message on %s (%d messages) \n", percent, channel.Name, len(history.Messages))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment