Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update go version #2

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .envrc.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export SLACK_BOT_TOKEN=xoxb-xxx
export SLACK_APP_TOKEN=xapp-yyy
6 changes: 3 additions & 3 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: 1.13
go-version: 1.22

- run: make build

Expand All @@ -20,7 +20,7 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: 1.13
go-version: 1.22

- run: make check

Expand All @@ -31,6 +31,6 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: 1.13
go-version: 1.22

- run: make test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
slack-rage
.envrc

# Created by https://www.toptal.com/developers/gitignore/api/go
# Edit at https://www.toptal.com/developers/gitignore?templates=go
Expand Down
101 changes: 101 additions & 0 deletions bolt/bolt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package bolt

import (
"github.com/h3poteto/slack-rage/rage"
"github.com/sirupsen/logrus"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
"log"
"os"
)

type Bolt struct {
channel string
verbose bool
logger *logrus.Logger
detector *rage.Rage
webApi *slack.Client
}

func New(threshold, period, speakers int, channel string, verbose bool) *Bolt {
logger := logrus.New()
if verbose {
logger.SetLevel(logrus.DebugLevel)
}

botToken := os.Getenv("SLACK_BOT_TOKEN")
appToken := os.Getenv("SLACK_APP_TOKEN")
webApi := slack.New(
botToken,
slack.OptionAppLevelToken(appToken),
slack.OptionDebug(verbose),
slack.OptionLog(log.New(os.Stdout, "api: ", log.Lshortfile|log.LstdFlags)),
)
detector := rage.New(threshold, period, speakers, channel, logger, webApi)
return &Bolt{
channel,
verbose,
logger,
detector,
webApi,
}
}

func (b *Bolt) Start() {
socketMode := socketmode.New(
b.webApi,
socketmode.OptionDebug(b.verbose),
socketmode.OptionLog(log.New(os.Stdout, "sm: ", log.Lshortfile|log.LstdFlags)),
)
authTest, authTestErr := b.webApi.AuthTest()
if authTestErr != nil {
b.logger.Errorf("slack bot token is invalid: %v", authTestErr)
os.Exit(1)
}
b.logger.Infof("Bot user ID: %s", authTest.UserID)

go func() {
for envelope := range socketMode.Events {
switch envelope.Type {
case socketmode.EventTypeConnecting:
socketMode.Debugf("Connecting to Slack with Socket Mode...")
case socketmode.EventTypeConnectionError:
socketMode.Debugf("Connection failed: %v", envelope)
case socketmode.EventTypeConnected:
socketMode.Debugf("Connected to Slack with Socket Mode.")
case socketmode.EventTypeEventsAPI:
eventsAPIEvent, ok := envelope.Data.(slackevents.EventsAPIEvent)
if !ok {
socketMode.Debugf("Ignored %+v", envelope)
continue
}
socketMode.Ack(*envelope.Request)

switch eventsAPIEvent.Type {
case slackevents.CallbackEvent:
socketMode.Debugf("CallbackEvent received: %+v", eventsAPIEvent.InnerEvent)
switch event := eventsAPIEvent.InnerEvent.Data.(type) {
case *slackevents.MessageEvent:
err := b.detector.Detect(event.Channel, event.TimeStamp)
if err != nil {
socketMode.Debugf("Detect failed: %v", err)
}
default:
socketMode.Debugf("Skipped: %v", event)
}
default:
socketMode.Debugf("Skipped: %v", eventsAPIEvent.Type)
}
default:
socketMode.Debugf("Skipped: %v", envelope.Type)
}
}
}()

err := socketMode.Run()
if err != nil {
b.logger.Errorf("socketMode.Run() failed: %v", err)
os.Exit(1)
}
}
37 changes: 37 additions & 0 deletions cmd/bolt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"github.com/h3poteto/slack-rage/bolt"
"github.com/spf13/cobra"
)

type runBolt struct {
threshold int
period int
speakers int
channel string
verbose bool
}

func runBoltCmd() *cobra.Command {
r := &runBolt{}
cmd := &cobra.Command{
Use: "bolt",
Short: "Run bot using Bolt",
Run: r.run,
}

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 *runBolt) run(cmd *cobra.Command, args []string) {
b := bolt.New(r.threshold, r.period, r.speakers, r.channel, r.verbose)
b.Start()
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ var RootCmd = &cobra.Command{
func init() {
cobra.OnInitialize()

RootCmd.AddCommand(versionCmd(), runEventCmd(), runRTMCmd())
RootCmd.AddCommand(versionCmd(), runEventCmd(), runRTMCmd(), runBoltCmd())
}
20 changes: 15 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
module github.com/h3poteto/slack-rage

go 1.13
go 1.22

require (
github.com/sirupsen/logrus v1.2.0
github.com/slack-go/slack v0.6.2
github.com/spf13/cobra v0.0.6
github.com/stretchr/testify v1.2.2
github.com/sirupsen/logrus v1.9.3
github.com/slack-go/slack v0.15.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.10.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading