From 1e7e8b6b8f80b106e5e313a35580e97f6502df8b Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Fri, 13 Mar 2020 11:58:18 +0900
Subject: [PATCH] refs #29 Add a argument to specify speakers
---
cmd/event.go | 4 +++-
cmd/rtm.go | 4 +++-
event/server.go | 16 ++++++----------
rage/rage.go | 6 ++++--
rtm/rtm.go | 16 ++++++----------
5 files changed, 22 insertions(+), 24 deletions(-)
diff --git a/cmd/event.go b/cmd/event.go
index 3d380d9..21d8b4e 100644
--- a/cmd/event.go
+++ b/cmd/event.go
@@ -8,6 +8,7 @@ import (
type runEvent struct {
threshold int
period int
+ speakers int
channel string
verbose bool
}
@@ -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")
@@ -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()
}
diff --git a/cmd/rtm.go b/cmd/rtm.go
index 5fd02bc..790f2b3 100644
--- a/cmd/rtm.go
+++ b/cmd/rtm.go
@@ -8,6 +8,7 @@ import (
type runRTM struct {
threshold int
period int
+ speakers int
channel string
verbose bool
}
@@ -23,6 +24,7 @@ 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")
@@ -30,6 +32,6 @@ func runRTMCmd() *cobra.Command {
}
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()
}
diff --git a/event/server.go b/event/server.go
index c54ccc8..685e3e9 100644
--- a/event/server.go
+++ b/event/server.go
@@ -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,
diff --git a/rage/rage.go b/rage/rage.go
index 97c5947..09bcd77 100644
--- a/rage/rage.go
+++ b/rage/rage.go
@@ -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,
@@ -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
}
diff --git a/rtm/rtm.go b/rtm/rtm.go
index 5d4eea0..793e418 100644
--- a/rtm/rtm.go
+++ b/rtm/rtm.go
@@ -9,15 +9,13 @@ 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 {
@@ -25,10 +23,8 @@ func New(threshold, period int, channel string, verbose bool) *RTM {
}
// 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,