Skip to content

Commit

Permalink
fix: Use log package for logging
Browse files Browse the repository at this point in the history
Using the `log` package ensures that timestamps are emitted with our log
entries, and the format matches the logging behavior of the slack-go
library when debugging is enabled.

Certain log entries, such as ignoring unsupported events, are now gated
by whether or not debugging has been enabled.

Closes #131
  • Loading branch information
arusso committed Apr 5, 2023
1 parent 6d778fb commit 48e84a3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
4 changes: 2 additions & 2 deletions message_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func getChannel(slacker *Slacker, channelID string) *slack.Channel {
IncludeLocale: false,
IncludeNumMembers: false})
if err != nil {
fmt.Printf("unable to get channel info for %s: %v\n", channelID, err)
slacker.logf("unable to get channel info for %s: %v\n", channelID, err)
return nil
}
return channel
Expand All @@ -141,7 +141,7 @@ func getUserProfile(slacker *Slacker, userID string) *slack.UserProfile {

user, err := slacker.apiClient.GetUserInfo(userID)
if err != nil {
fmt.Printf("unable to get user info for %s: %v\n", userID, err)
slacker.logf("unable to get user info for %s: %v\n", userID, err)
return nil
}
return &user.Profile
Expand Down
3 changes: 2 additions & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slacker

import (
"fmt"
"log"

"github.com/slack-go/slack"
)
Expand Down Expand Up @@ -43,7 +44,7 @@ func (r *response) ReportError(err error, options ...ReportErrorOption) {

_, _, err = apiClient.PostMessage(event.ChannelID, opts...)
if err != nil {
fmt.Printf("failed posting message: %v\n", err)
log.Printf("failed posting message: %v\n", err)
}
}

Expand Down
37 changes: 25 additions & 12 deletions slacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"
"strings"

"github.com/robfig/cron"
Expand Down Expand Up @@ -60,6 +61,7 @@ func NewClient(botToken, appToken string, options ...ClientOption) *Slacker {
errUnauthorized: errUnauthorized,
botInteractionMode: defaults.BotMode,
sanitizeEventText: defaultCleanEventInput,
debug: defaults.Debug,
}
return slacker
}
Expand Down Expand Up @@ -90,6 +92,7 @@ type Slacker struct {
appID string
botInteractionMode BotInteractionMode
sanitizeEventText func(string) string
debug bool
}

// BotCommands returns Bot Commands
Expand Down Expand Up @@ -219,26 +222,26 @@ func (s *Slacker) Listen(ctx context.Context) error {

switch socketEvent.Type {
case socketmode.EventTypeConnecting:
fmt.Println("Connecting to Slack with Socket Mode.")
s.logf("Connecting to Slack with Socket Mode.")
if s.initHandler == nil {
continue
}
go s.initHandler()

case socketmode.EventTypeConnectionError:
fmt.Println("Connection failed. Retrying later...")
s.logf("Connection failed. Retrying later...")

case socketmode.EventTypeConnected:
fmt.Println("Connected to Slack with Socket Mode.")
s.logf("Connected to Slack with Socket Mode.")

case socketmode.EventTypeHello:
s.appID = socketEvent.Request.ConnectionInfo.AppID
fmt.Printf("Connected as App ID %v\n", s.appID)
s.logf("Connected as App ID %v\n", s.appID)

case socketmode.EventTypeEventsAPI:
event, ok := socketEvent.Data.(slackevents.EventsAPIEvent)
if !ok {
fmt.Printf("Ignored %+v\n", socketEvent)
s.debugf("Ignored %+v\n", socketEvent)
continue
}

Expand All @@ -250,7 +253,7 @@ func (s *Slacker) Listen(ctx context.Context) error {
if s.defaultInnerEventHandler != nil {
s.defaultInnerEventHandler(ctx, event.InnerEvent.Data, socketEvent.Request)
} else {
fmt.Printf("unsupported inner event: %+v\n", event.InnerEvent.Type)
s.debugf("unsupported inner event: %+v\n", event.InnerEvent.Type)
}
}

Expand All @@ -259,7 +262,7 @@ func (s *Slacker) Listen(ctx context.Context) error {
case socketmode.EventTypeSlashCommand:
callback, ok := socketEvent.Data.(slack.SlashCommand)
if !ok {
fmt.Printf("Ignored %+v\n", socketEvent)
s.debugf("Ignored %+v\n", socketEvent)
continue
}
s.socketModeClient.Ack(*socketEvent.Request)
Expand All @@ -268,7 +271,7 @@ func (s *Slacker) Listen(ctx context.Context) error {
case socketmode.EventTypeInteractive:
callback, ok := socketEvent.Data.(slack.InteractionCallback)
if !ok {
fmt.Printf("Ignored %+v\n", socketEvent)
s.debugf("Ignored %+v\n", socketEvent)
continue
}

Expand Down Expand Up @@ -418,18 +421,18 @@ func (s *Slacker) handleMessageEvent(ctx context.Context, event interface{}, req
bot, err := s.apiClient.GetBotInfo(messageEvent.BotID)
if err != nil {
if err.Error() == "missing_scope" {
fmt.Println("unable to determine if bot response is from me -- please add users:read scope to your app")
s.logf("unable to determine if bot response is from me -- please add users:read scope to your app\n")
} else {
fmt.Printf("unable to get bot that sent message information: %v\n", err)
s.debugf("unable to get bot that sent message information: %v\n", err)
}
return
}
if bot.AppID == s.appID {
fmt.Printf("Ignoring event that originated from my App ID: %v\n", bot.AppID)
s.debugf("Ignoring event that originated from my App ID: %v\n", bot.AppID)
return
}
case BotInteractionModeIgnoreAll:
fmt.Printf("Ignoring event that originated from Bot ID: %v\n", messageEvent.BotID)
s.debugf("Ignoring event that originated from Bot ID: %v\n", messageEvent.BotID)
return
default:
// BotInteractionModeIgnoreNone is handled in the default case
Expand Down Expand Up @@ -467,3 +470,13 @@ func (s *Slacker) handleMessageEvent(ctx context.Context, event interface{}, req
s.defaultMessageHandler(botCtx, request, response)
}
}

func (s *Slacker) logf(format string, v ...interface{}) {
log.Printf(format, v...)
}

func (s *Slacker) debugf(format string, v ...interface{}) {
if s.debug {
log.Printf(format, v...)
}
}

0 comments on commit 48e84a3

Please sign in to comment.