chat_sdk-slack 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/chat_sdk/slack/adapter.rb +16 -0
- data/lib/chat_sdk/slack/socket_mode.rb +107 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1110d3c38cf19a4274fab211b64f496c6fc0fdc9461375a6c55b87711e3395c3
|
|
4
|
+
data.tar.gz: b78730c7b156b9f2ae0c4b548fe671b062fa12cb7ee31de6993a9e28caca553b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f44439e7c4fe05bac0c9b4d886af0fa01e3a49171712220e82649dca86e3b8f8d7d4277a614d6c9a9c3f3df82694deb3661bc11200b0036934ae5878b0222195
|
|
7
|
+
data.tar.gz: d845dbde4c82f06e22658e563294ab69b2067dea31f931d2229baf12c6d13230355bf3dad2e731084996789292181139617cbbeef23b798cc7d1975bda6f1466
|
|
@@ -222,6 +222,22 @@ module ChatSDK
|
|
|
222
222
|
# This is a no-op but the capability is declared for streaming support
|
|
223
223
|
end
|
|
224
224
|
|
|
225
|
+
# Slack-specific: receive real-time events via Socket Mode WebSocket.
|
|
226
|
+
# Requires the optional 'faye-websocket' gem and an app-level token (xapp-*).
|
|
227
|
+
# Not part of the base adapter contract.
|
|
228
|
+
#
|
|
229
|
+
# Usage:
|
|
230
|
+
# adapter.start_socket_mode(app_token: "xapp-...") do |event|
|
|
231
|
+
# # event is a ChatSDK::Events::* instance
|
|
232
|
+
# end
|
|
233
|
+
def start_socket_mode(app_token: nil, &block)
|
|
234
|
+
app_token ||= ENV["SLACK_APP_TOKEN"]
|
|
235
|
+
raise ChatSDK::ConfigurationError, "Slack app_token required for socket mode" unless app_token
|
|
236
|
+
|
|
237
|
+
socket = SocketMode.new(app_token: app_token, bot_client: @client)
|
|
238
|
+
socket.start(&block)
|
|
239
|
+
end
|
|
240
|
+
|
|
225
241
|
def mention(user_id)
|
|
226
242
|
"<@#{user_id}>"
|
|
227
243
|
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ChatSDK
|
|
4
|
+
module Slack
|
|
5
|
+
# Implements Slack Socket Mode: receives events over a WebSocket instead of
|
|
6
|
+
# incoming HTTP webhooks. Useful for local development, firewalled environments,
|
|
7
|
+
# or any setup that cannot expose a public URL.
|
|
8
|
+
#
|
|
9
|
+
# Requires the optional `faye-websocket` gem and an app-level token (xapp-*).
|
|
10
|
+
#
|
|
11
|
+
# Usage:
|
|
12
|
+
# adapter.start_socket_mode(app_token: "xapp-...") do |event|
|
|
13
|
+
# # event is a ChatSDK::Events::* instance
|
|
14
|
+
# end
|
|
15
|
+
class SocketMode
|
|
16
|
+
PING_INTERVAL = 30
|
|
17
|
+
|
|
18
|
+
attr_reader :app_token, :bot_client
|
|
19
|
+
|
|
20
|
+
def initialize(app_token:, bot_client:)
|
|
21
|
+
@app_token = app_token
|
|
22
|
+
@bot_client = bot_client
|
|
23
|
+
@running = false
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def start(&block)
|
|
27
|
+
raise ArgumentError, "start requires a block" unless block
|
|
28
|
+
|
|
29
|
+
load_websocket_driver!
|
|
30
|
+
|
|
31
|
+
@running = true
|
|
32
|
+
while @running
|
|
33
|
+
url = obtain_websocket_url
|
|
34
|
+
run_connection(url, &block)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def stop
|
|
39
|
+
@running = false
|
|
40
|
+
@ws&.close
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def load_websocket_driver!
|
|
46
|
+
require "faye/websocket"
|
|
47
|
+
rescue LoadError
|
|
48
|
+
raise ChatSDK::ConfigurationError,
|
|
49
|
+
"Slack Socket Mode requires the 'faye-websocket' gem. " \
|
|
50
|
+
"Add gem 'faye-websocket' to your Gemfile."
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def obtain_websocket_url
|
|
54
|
+
# apps.connections.open must be called with the app-level token, not the bot token
|
|
55
|
+
app_client = ::Slack::Web::Client.new(token: @app_token)
|
|
56
|
+
response = app_client.apps_connections_open
|
|
57
|
+
response["url"]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def run_connection(url, &block)
|
|
61
|
+
EM.run do
|
|
62
|
+
@ws = Faye::WebSocket::Client.new(url, nil, ping: PING_INTERVAL)
|
|
63
|
+
|
|
64
|
+
@ws.on :message do |ws_event|
|
|
65
|
+
handle_message(ws_event, &block)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
@ws.on :close do |_event|
|
|
69
|
+
@ws = nil
|
|
70
|
+
EM.stop
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
rescue
|
|
74
|
+
# Let transient errors trigger a reconnect rather than crashing the loop
|
|
75
|
+
raise unless @running
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def handle_message(ws_event, &block)
|
|
79
|
+
data = JSON.parse(ws_event.data)
|
|
80
|
+
|
|
81
|
+
# Acknowledge the envelope so Slack doesn't retry
|
|
82
|
+
acknowledge(data["envelope_id"]) if data["envelope_id"]
|
|
83
|
+
|
|
84
|
+
payload = data["payload"]
|
|
85
|
+
return unless payload
|
|
86
|
+
|
|
87
|
+
# Socket Mode wraps Events API payloads in an envelope. The inner payload
|
|
88
|
+
# matches the same structure our EventParser already handles from webhooks.
|
|
89
|
+
#
|
|
90
|
+
# Envelope types:
|
|
91
|
+
# "events_api" -> payload is an event_callback body
|
|
92
|
+
# "interactive" -> payload is a block_actions / view_submission body
|
|
93
|
+
# "slash_commands" -> payload is a slash command body
|
|
94
|
+
events = EventParser.parse(payload)
|
|
95
|
+
events.each { |event| block.call(event) }
|
|
96
|
+
rescue JSON::ParserError
|
|
97
|
+
# Ignore malformed frames (e.g. pong/control frames)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def acknowledge(envelope_id)
|
|
101
|
+
return unless envelope_id && @ws
|
|
102
|
+
|
|
103
|
+
@ws.send(JSON.generate({envelope_id: envelope_id}))
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: chat_sdk-slack
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Quentin Rousseau
|
|
@@ -49,6 +49,7 @@ files:
|
|
|
49
49
|
- lib/chat_sdk/slack/block_kit_renderer.rb
|
|
50
50
|
- lib/chat_sdk/slack/event_parser.rb
|
|
51
51
|
- lib/chat_sdk/slack/modal_renderer.rb
|
|
52
|
+
- lib/chat_sdk/slack/socket_mode.rb
|
|
52
53
|
homepage: https://github.com/rootlyhq/chat-sdk
|
|
53
54
|
licenses:
|
|
54
55
|
- MIT
|