-
Notifications
You must be signed in to change notification settings - Fork 619
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
Add Events API in socket mode for Slack adapter #2161
Open
kolsys
wants to merge
1
commit into
42wim:master
Choose a base branch
from
kolsys:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,9 @@ import ( | |
"github.com/42wim/matterbridge/bridge/config" | ||
"github.com/42wim/matterbridge/bridge/helper" | ||
"github.com/slack-go/slack" | ||
"github.com/slack-go/slack/socketmode" | ||
"github.com/slack-go/slack/slackevents" | ||
"encoding/json" | ||
) | ||
|
||
// ErrEventIgnored is for events that should be ignored | ||
|
@@ -19,6 +22,9 @@ func (b *Bslack) handleSlack() { | |
if b.GetString(incomingWebhookConfig) != "" && b.GetString(tokenConfig) == "" { | ||
b.Log.Debugf("Choosing webhooks based receiving") | ||
go b.handleMatterHook(messages) | ||
} else if b.GetString(appTokenConfig) != "" && b.GetString(tokenConfig) != "" { | ||
b.Log.Debugf("Choosing socket mode based receiving") | ||
go b.handleSlackClientSocketMode(messages) | ||
} else { | ||
b.Log.Debugf("Choosing token based receiving") | ||
go b.handleSlackClient(messages) | ||
|
@@ -47,6 +53,135 @@ func (b *Bslack) handleSlack() { | |
} | ||
} | ||
|
||
func (b *Bslack) handleSlackClientSocketMode(messages chan *config.Message) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method |
||
|
||
for evt := range b.smc.Events { | ||
switch evt.Type { | ||
case socketmode.EventTypeConnecting: | ||
b.Log.Debug("Connecting to Slack with Socket Mode...") | ||
case socketmode.EventTypeConnectionError: | ||
b.Log.Debug("Connection failed. Retrying later...") | ||
case socketmode.EventTypeConnected: | ||
b.Log.Debug("Connected to Slack with Socket Mode.") | ||
if info, err := b.rtm.AuthTest(); err == nil { | ||
b.si = &slack.Info { | ||
User: &slack.UserDetails{ | ||
ID: info.UserID, | ||
Name: info.User, | ||
}, | ||
Team: &slack.Team{ | ||
ID: info.TeamID, | ||
Name: info.Team, | ||
}, | ||
} | ||
b.channels.populateChannels(true) | ||
b.users.populateUsers(true) | ||
} else { | ||
b.Log.Fatalf("Get user info error %+v", err) | ||
} | ||
case socketmode.EventTypeEventsAPI: | ||
eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent) | ||
if !ok { | ||
b.Log.Printf("Ignored %+v\n", evt) | ||
continue | ||
} | ||
|
||
b.smc.Ack(*evt.Request) | ||
|
||
switch eventsAPIEvent.Type { | ||
case slackevents.CallbackEvent: | ||
innerEvent := eventsAPIEvent.InnerEvent | ||
// b.Log.Debugf("Event received %+v", innerEvent) | ||
switch ev := innerEvent.Data.(type) { | ||
case *slackevents.MessageEvent: | ||
// Workaround for handler compability | ||
evString, _ := json.Marshal(ev) | ||
b.Log.Debugf("Message event: %s", evString) | ||
slackEvent := &slack.MessageEvent{} | ||
if err := json.Unmarshal(evString, &slackEvent); err != nil { | ||
b.Log.Errorf("Skipped message: %#v", err) | ||
continue | ||
} | ||
|
||
if b.skipMessageEvent(slackEvent) { | ||
b.Log.Debugf("Skipped message: %#v", slackEvent) | ||
continue | ||
} | ||
rmsg, err := b.handleMessageEvent(slackEvent) | ||
if err != nil { | ||
b.Log.Errorf("%#v", err) | ||
continue | ||
} | ||
messages <- rmsg | ||
case *slackevents.FileDeletedEvent: | ||
slackEvent := &slack.FileDeletedEvent{ | ||
Type: ev.Type, | ||
EventTimestamp: ev.EventTimestamp, | ||
FileID: ev.FileID, | ||
} | ||
rmsg, err := b.handleFileDeletedEvent(slackEvent) | ||
if err != nil { | ||
b.Log.Printf("%#v", err) | ||
continue | ||
} | ||
messages <- rmsg | ||
case *slackevents.MemberJoinedChannelEvent: | ||
b.users.populateUser(ev.User) | ||
case *slackevents.UserProfileChangedEvent: | ||
b.users.invalidateUser(ev.User.ID) | ||
|
||
// TODO not implemented | ||
// case *slack.ChannelJoinedEvent: | ||
// // When we join a channel we update the full list of users as | ||
// // well as the information for the channel that we joined as this | ||
// // should now tell that we are a member of it. | ||
// b.channels.registerChannel(ev.Channel) | ||
|
||
case *slackevents.AppMentionEvent: | ||
default: | ||
b.Log.Debugf("Unhandled incoming event: %T", ev) | ||
} | ||
default: | ||
b.Log.Printf("Unsupported Events API event received: %+v", eventsAPIEvent.Type) | ||
} | ||
case socketmode.EventTypeInteractive: | ||
callback, ok := evt.Data.(slack.InteractionCallback) | ||
if !ok { | ||
b.Log.Printf("Ignored %+v\n", evt) | ||
continue | ||
} | ||
|
||
b.Log.Debugf("Interaction skipped: %+v\n", callback) | ||
|
||
var payload interface{} | ||
|
||
switch callback.Type { | ||
case slack.InteractionTypeBlockActions: | ||
case slack.InteractionTypeShortcut: | ||
case slack.InteractionTypeViewSubmission: | ||
case slack.InteractionTypeDialogSubmission: | ||
default: | ||
|
||
} | ||
|
||
b.smc.Ack(*evt.Request, payload) | ||
case socketmode.EventTypeSlashCommand: | ||
cmd, ok := evt.Data.(slack.SlashCommand) | ||
if !ok { | ||
b.Log.Printf("Ignored %+v\n", evt) | ||
} else { | ||
b.Log.Debugf("Slash command skipped: %+v", cmd) | ||
} | ||
var payload interface{} | ||
b.smc.Ack(*evt.Request, payload) | ||
case "hello": | ||
continue | ||
default: | ||
b.Log.Errorf("Unexpected event type received: %s\n", evt.Type) | ||
} | ||
} | ||
} | ||
|
||
func (b *Bslack) handleSlackClient(messages chan *config.Message) { | ||
for msg := range b.rtm.IncomingEvents { | ||
if msg.Type != sUserTyping && msg.Type != sHello && msg.Type != sLatencyReport { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
vendor/github.com/slack-go/slack/slackevents/action_events.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method
Bslack.handleSlackClientSocketMode
has 107 lines of code (exceeds 50 allowed). Consider refactoring.