Skip to content

Commit 6a63662

Browse files
committed
fix(websocket): add missing events (apps, activity, badge count)
Closes #1544.
1 parent 9e3d20a commit 6a63662

3 files changed

Lines changed: 103 additions & 1 deletion

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// This example connects to Slack via RTM and listens for events.
2+
//
3+
// Before the fix for #1544, the events "apps_uninstalled", "activity", and
4+
// "badge_counts_updated" were not mapped and would appear as
5+
// UnmarshallingErrorEvent with an "Received unmapped event" message.
6+
//
7+
// Usage:
8+
//
9+
// export SLACK_BOT_TOKEN=xoxb-...
10+
// go run ./examples/unmapped_events/
11+
//
12+
// Then interact with your workspace (open channels, browse around) and watch
13+
// for unmapped event errors in the output. The "activity" and
14+
// "badge_counts_updated" events tend to appear during normal workspace usage.
15+
// The "apps_uninstalled" event appears when an app is removed.
16+
package main
17+
18+
import (
19+
"fmt"
20+
"log"
21+
"os"
22+
23+
"github.com/slack-go/slack"
24+
)
25+
26+
func main() {
27+
token := os.Getenv("SLACK_BOT_TOKEN")
28+
if token == "" {
29+
fmt.Println("SLACK_BOT_TOKEN environment variable is required")
30+
os.Exit(1)
31+
}
32+
33+
api := slack.New(
34+
token,
35+
slack.OptionDebug(true),
36+
slack.OptionLog(log.New(os.Stdout, "slack-bot: ", log.Lshortfile|log.LstdFlags)),
37+
)
38+
39+
rtm := api.NewRTM()
40+
go rtm.ManageConnection()
41+
42+
for msg := range rtm.IncomingEvents {
43+
switch ev := msg.Data.(type) {
44+
case *slack.ConnectedEvent:
45+
fmt.Printf("Connected: %s (connection count: %d)\n", ev.Info.User.ID, ev.ConnectionCount)
46+
47+
case *slack.AppsUninstalledEvent:
48+
fmt.Printf("Apps uninstalled: %+v\n", ev)
49+
50+
case *slack.ActivityEvent:
51+
fmt.Printf("Activity: subtype=%s key=%s\n", ev.SubType, ev.Key)
52+
53+
case *slack.BadgeCountsUpdatedEvent:
54+
fmt.Printf("Badge counts updated: %+v\n", ev)
55+
56+
case *slack.UnmarshallingErrorEvent:
57+
// Before the fix, apps_uninstalled/activity/badge_counts_updated
58+
// end up here as unmapped events.
59+
fmt.Printf("UNMAPPED EVENT ERROR: %v\n", ev.ErrorObj)
60+
61+
case *slack.InvalidAuthEvent:
62+
fmt.Println("Invalid credentials")
63+
return
64+
65+
case *slack.DisconnectedEvent:
66+
if ev.Intentional {
67+
return
68+
}
69+
70+
default:
71+
fmt.Printf("Event: type=%s data=%T\n", msg.Type, ev)
72+
}
73+
}
74+
}

websocket_managed_conn.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,10 @@ var EventMapping = map[string]interface{}{
598598

599599
"accounts_changed": AccountsChangedEvent{},
600600

601+
"apps_uninstalled": AppsUninstalledEvent{},
602+
"activity": ActivityEvent{},
603+
"badge_counts_updated": BadgeCountsUpdatedEvent{},
604+
601605
"reconnect_url": ReconnectUrlEvent{},
602606

603607
"member_joined_channel": MemberJoinedChannelEvent{},

websocket_misc.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ type MemberLeftChannelEvent struct {
166166
Team string `json:"team"`
167167
}
168168

169-
// ChannelUpdatedEvent is fired when a channel's properties are updated (tabs, meeting notes, etc.).
169+
// ChannelUpdatedEvent is fired when a channel's properties are updated (tabs, meeting
170+
// notes, etc.).
170171
type ChannelUpdatedEvent struct {
171172
Type string `json:"type"`
172173
Updates map[string]any `json:"updates"`
@@ -253,3 +254,26 @@ type SHRoomUpdateEvent struct {
253254
EventTS string `json:"event_ts"`
254255
TS string `json:"ts"`
255256
}
257+
258+
// AppsUninstalledEvent represents the apps_uninstalled event sent via RTM
259+
// when one or more apps are uninstalled from the workspace.
260+
type AppsUninstalledEvent struct {
261+
Type string `json:"type"`
262+
}
263+
264+
// ActivityEvent represents the activity event sent via RTM. This is an
265+
// internal Slack event that fires during normal workspace usage (e.g. new
266+
// messages, bundle updates).
267+
type ActivityEvent struct {
268+
Type string `json:"type"`
269+
SubType string `json:"subtype"`
270+
Key string `json:"key"`
271+
Entry json.RawMessage `json:"entry"`
272+
EventTimestamp string `json:"event_ts"`
273+
}
274+
275+
// BadgeCountsUpdatedEvent represents the badge_counts_updated event sent via
276+
// RTM when notification badge counts change.
277+
type BadgeCountsUpdatedEvent struct {
278+
Type string `json:"type"`
279+
}

0 commit comments

Comments
 (0)