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

Add Events API in socket mode for Slack adapter #2161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kolsys
Copy link

@kolsys kolsys commented Jul 1, 2024

This is beta version for modern Slack applications.
Changes implement Slack Events API in socket mode, see #964.

Fixes #2159
It also fixes files upload to Slack for modern Apps.

To activate Ecvents API in SocketMode you must:

  1. Create new modern App with following OAuth Bot scope (or upgrade Legacy)
channels:history
channels:read
chat:write
files:read
files:write
groups:history
groups:read
im:history
im:read
mpim:history
mpim:read
users.profile:read
users:read
  1. Acivate Socket mode
  2. Subscribe to the following events:
channel_history_changed
group_history_changed
im_history_changed	
message.channels
message.groups
message.im
message.mpim
  1. Add AppToken to the toml:
[slack]
    [slack.mybot]
    Token="xoxb-*******"
    AppToken="xapp-**********"

@@ -47,6 +53,135 @@ func (b *Bslack) handleSlack() {
}
}

func (b *Bslack) handleSlackClientSocketMode(messages chan *config.Message) {
Copy link

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.

@@ -47,6 +53,135 @@ func (b *Bslack) handleSlack() {
}
}

func (b *Bslack) handleSlackClientSocketMode(messages chan *config.Message) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method Bslack.handleSlackClientSocketMode has a Cognitive Complexity of 47 (exceeds 20 allowed). Consider refactoring.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR diff size of 236869 lines exceeds the maximum allowed for the inline comments feature.

- Added support of Event API in socketMode
- Fix files upload for new applications
Copy link

codeclimate bot commented Jul 1, 2024

Code Climate has analyzed commit 5b86262 and detected 4 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 4

View more on Code Climate.

@petqa
Copy link

petqa commented Jul 7, 2024

FROM golang:1.22.0-alpine3.19 AS builder

COPY matterbridge /go/src/matterbridge
RUN apk --no-cache add git \
        && go version \
        && cd /go/src/matterbridge \
        && CGO_ENABLED=0 go build -mod vendor -o /bin/matterbridge

FROM alpine:3.19
RUN apk --no-cache add ca-certificates mailcap
COPY --from=builder /bin/matterbridge /bin/matterbridge
RUN mkdir /etc/matterbridge \
  && touch /etc/matterbridge/matterbridge.toml \
  && ln -sf /matterbridge.toml /etc/matterbridge/matterbridge.toml
ENTRYPOINT ["/bin/matterbridge", "-conf", "/etc/matterbridge/matterbridge.toml"]

@kolsys Could you please update the Dockerfile? The current version in the master branch doesn’t work correctly.

@kolsys
Copy link
Author

kolsys commented Jul 9, 2024

FROM golang:1.22.0-alpine3.19 AS builder

COPY matterbridge /go/src/matterbridge
RUN apk --no-cache add git \
        && go version \
        && cd /go/src/matterbridge \
        && CGO_ENABLED=0 go build -mod vendor -o /bin/matterbridge

FROM alpine:3.19
RUN apk --no-cache add ca-certificates mailcap
COPY --from=builder /bin/matterbridge /bin/matterbridge
RUN mkdir /etc/matterbridge \
  && touch /etc/matterbridge/matterbridge.toml \
  && ln -sf /matterbridge.toml /etc/matterbridge/matterbridge.toml
ENTRYPOINT ["/bin/matterbridge", "-conf", "/etc/matterbridge/matterbridge.toml"]

@kolsys Could you please update the Dockerfile? The current version in the master branch doesn’t work correctly.

Whats wrong with the master version? I've build and run it.

@xeonic-ant
Copy link

Hi. We experiencing errors downloading images. Look like the file URL is empty.
Bot configured according your recommendations.

config

[api.api]
    BindAddress="0.0.0.0:4242"
    Buffer=1000
    RemoteNickFormat="{NICK}"
[general]
    MediaDownloadPath="/var/www/matterbridge"
    MediaServerDownload="http://matterbridge.matterbridge-namespace/matterbridge"
[slack]
  [slack.one]
    Token="xoxb-*****"
    AppToken="xapp-********"
    PrefixMessagesWithNick=true
    Debug=true
    RemoteNickFormat="[{PROTOCOL}] <{NICK}> "
    PreserveThreading=true
    UseFullName=true
[mattermost]
  [mattermost.two]
    Server="company.com:443"
    Team="team1"
    Token="*****************"
    RemoteNickFormat="[{PROTOCOL}] <{NICK}> "
    PrefixMessagesWithNick=true
    PreserveThreading=true
    UseUserName=true
    Debug=true
[[gateway]]
name="api"
enable=true
[[gateway.inout]]
    account="api.api"
    channel="api"
[[gateway]]
name="client"
enable=true
  [[gateway.inout]]
  account="mattermost.two"
  channel="channel2"
  [[gateway.inout]]
  account="slack.one"
  channel="channel1"

Also tried without MediaDownloadPath/MediaServerDownload options - result is the same.

logs

time="2024-07-12T11:23:12Z" level=debug msg="Message event: {"client_msg_id":"3c60172b-85be-20cb-bed3-1b041bdd51af","type":"message","user":"U05******","text":"test40","thread_ts":"1720779786.805609","ts":"1720783391.651979","channel":"C061W7QFYPM","channel_type":"channel","event_ts":"1720783391.651979","subtype":"file_share","upload":false,"files":[{"id":"F07BZLYLJ6A","created":0,"timestamp":0,"name":"","title":"","mimetype":"","filetype":"png","pretty_type":"","user":"","editable":false,"size":0,"mode":"","is_external":false,"external_type":"","is_public":false,"public_url_shared":false,"display_as_bot":false,"username":"","url_private":"","url_private_download":"","thumb_64":"","thumb_80":"","thumb_360":"","thumb_360_w":0,"thumb_360_h":0,"thumb_480":"","thumb_480_w":0,"thumb_480_h":0,"thumb_160":"","thumb_720":"","thumb_720_w":0,"thumb_720_h":0,"thumb_800":"","thumb_800_w":0,"thumb_800_h":0,"thumb_960":"","thumb_960_w":0,"thumb_960_h":0,"thumb_1024":"","thumb_1024_w":0,"thumb_1024_h":0,"image_exif_rotation":0,"original_w":0,"original_h":0,"permalink":"","permalink_public":""}],"blocks":[{"type":"rich_text","block_id":"o6CuE","elements":[{"type":"rich_text_section","elements":[{"type":"text","text":"test40"}]}]}],"root":null}" func=handleSlackClientSocketMode file="bridge/slack/handlers.go:99" prefix=slack
time="2024-07-12T11:23:12Z" level=debug msg="Trying to download "" with size 0" func=HandleDownloadSize file="bridge/helper/helper.go:162" prefix=slack
time="2024-07-12T11:23:12Z" level=error msg="Could not download incoming file: &errors.errorString{s:"download  failed &url.Error{Op:\"Get\", URL:\"\", Err:(*errors.errorString)(0xc0009131e0)}"}" func=handleAttachments file="bridge/slack/handlers.go:460" prefix=slack

we are running bridge in k8s (without nat) but webockets seems to be work, pings look fine

slack-go/slack/socketmode2024/07/12 12:56:36 socket_mode_managed_conn.go:627: WebSocket ping message received: Ping from applink-13
slack-go/slack/socketmode2024/07/12 12:56:46 socket_mode_managed_conn.go:627: WebSocket ping message received: Ping from applink-13
time="2024-07-12T12:56:52Z" level=debug msg="WsReceiver response: &model.WebSocketResponse{Status:"OK", SeqReply:127, Data:map[string]interface {}{"node_id":"", "server_time":1.720789012475e+12, "text":"pong", "version":"9.2.2"}, Error:(*model.AppError)(nil)}" prefix=matterclient
time="2024-07-12T12:56:52Z" level=debug msg="getting response: &model.WebSocketResponse{Status:"OK", SeqReply:127, Data:map[string]interface {}{"node_id":"", "server_time":1.720789012475e+12, "text":"pong", "version":"9.2.2"}, Error:(*model.AppError)(nil)}" prefix=matterclient

@kolsys
Copy link
Author

kolsys commented Jul 12, 2024

@xeonic-ant
Hi. Looks like the RTM attachment message format is not fully compatible with Event API and it's required to write more complex converter for message than my workaround:
https://github.com/42wim/matterbridge/pull/2161/files#diff-014f29c022fd23b8c7f870b5cfa1cb6905ca8a8eb1c83d0209d77d279f4962b9R97

@mfw78
Copy link

mfw78 commented Aug 16, 2024

Just confirming, is there any ability within this using the new slack app to post in a channel within slack using a nickname that isn't the same name as the bot (so as to preserve / bridge the nickname and avatar?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Slack app classic deprecation, can't add new bots since 06/06/2024
5 participants