Skip to content

Commit

Permalink
Merge pull request #31 from h3poteto/iss-29
Browse files Browse the repository at this point in the history
closes #29 Add a argument to specify speakers
  • Loading branch information
h3poteto authored Mar 13, 2020
2 parents d4dae79 + 1e7e8b6 commit c40f3b0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
4 changes: 3 additions & 1 deletion cmd/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type runEvent struct {
threshold int
period int
speakers int
channel string
verbose bool
}
Expand All @@ -23,6 +24,7 @@ func runEventCmd() *cobra.Command {
flags := cmd.Flags()
flags.IntVarP(&r.threshold, "threshold", "t", 10, "Threshold for rage judgement.")
flags.IntVarP(&r.period, "period", "p", 1200, "Observation period seconds for rage judgement. This CLI notify when there are more than threshold posts per period.")
flags.IntVarP(&r.speakers, "speakers", "s", 3, "This CLI notify when more speakers are participating.")
flags.StringVarP(&r.channel, "channel", "c", "random", "Notify channel.")
flags.BoolVarP(&r.verbose, "verbose", "v", false, "Enable verbose mode")

Expand All @@ -31,6 +33,6 @@ func runEventCmd() *cobra.Command {
}

func (r *runEvent) run(cmd *cobra.Command, args []string) {
s := event.NewServer(r.threshold, r.period, r.channel, r.verbose)
s := event.NewServer(r.threshold, r.period, r.speakers, r.channel, r.verbose)
s.Serve()
}
4 changes: 3 additions & 1 deletion cmd/rtm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type runRTM struct {
threshold int
period int
speakers int
channel string
verbose bool
}
Expand All @@ -23,13 +24,14 @@ func runRTMCmd() *cobra.Command {
flags := cmd.Flags()
flags.IntVarP(&r.threshold, "threshold", "t", 10, "Threshold for rage judgement.")
flags.IntVarP(&r.period, "period", "p", 1200, "Observation period seconds for rage judgement. This CLI notify when there are more than threshold posts per period.")
flags.IntVarP(&r.speakers, "speakers", "s", 3, "This CLI notify when more speakers are participating.")
flags.StringVarP(&r.channel, "channel", "c", "random", "Notify channel.")
flags.BoolVarP(&r.verbose, "verbose", "v", false, "Enable verbose mode")

return cmd
}

func (r *runRTM) run(cmd *cobra.Command, args []string) {
s := rtm.New(r.threshold, r.period, r.channel, r.verbose)
s := rtm.New(r.threshold, r.period, r.speakers, r.channel, r.verbose)
s.Start()
}
16 changes: 6 additions & 10 deletions event/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,20 @@ import (
var notifyHistory = map[string]time.Time{}

type Server struct {
threshold int
period int
channel string
token string
logger *logrus.Logger
detector *rage.Rage
channel string
token string
logger *logrus.Logger
detector *rage.Rage
}

func NewServer(threshold, period int, channel string, verbose bool) *Server {
func NewServer(threshold, period, speakers int, channel string, verbose bool) *Server {
token := os.Getenv("SLACK_TOKEN")
logger := logrus.New()
if verbose {
logger.SetLevel(logrus.DebugLevel)
}
detector := rage.New(threshold, period, channel, logger, token)
detector := rage.New(threshold, period, speakers, channel, logger, token)
return &Server{
threshold,
period,
channel,
token,
logger,
Expand Down
6 changes: 4 additions & 2 deletions rage/rage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ func keys(m map[string]bool) []string {
type Rage struct {
threshold int
period int
speakers int
channel string
logger *logrus.Logger
slackClient *slack.Client
notifyHistory map[string]time.Time
}

func New(threshold, period int, channel string, logger *logrus.Logger, token string) *Rage {
func New(threshold, period, speakers int, channel string, logger *logrus.Logger, token string) *Rage {
notifyHistory := map[string]time.Time{}
slackClient := slack.New(token)
return &Rage{
threshold,
period,
speakers,
channel,
logger,
slackClient,
Expand Down Expand Up @@ -98,7 +100,7 @@ func (r *Rage) Detect(messageChannelID string, messageTimestamp string) error {
}

r.logger.Infof("%d speakers in the conversation", len(speakers))
if len(speakers) < 2 {
if len(speakers) < r.speakers {
return nil
}

Expand Down
16 changes: 6 additions & 10 deletions rtm/rtm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,22 @@ import (
)

type RTM struct {
threshold int
period int
channel string
token string
logger *logrus.Logger
detector *rage.Rage
channel string
token string
logger *logrus.Logger
detector *rage.Rage
}

func New(threshold, period int, channel string, verbose bool) *RTM {
func New(threshold, period, speakers int, channel string, verbose bool) *RTM {
token := os.Getenv("SLACK_TOKEN")
logger := logrus.New()
if verbose {
logger.SetLevel(logrus.DebugLevel)
}
// We have to create classic slack app using RTM.
// Classic slack app require OAuth token to call REST API separately from bot token.
detector := rage.New(threshold, period, channel, logger, os.Getenv("OAUTH_TOKEN"))
detector := rage.New(threshold, period, speakers, channel, logger, os.Getenv("OAUTH_TOKEN"))
return &RTM{
threshold,
period,
channel,
token,
logger,
Expand Down

0 comments on commit c40f3b0

Please sign in to comment.