chat_sdk-slack 0.5.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 +79 -1
- 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
|
|
@@ -5,7 +5,8 @@ module ChatSDK
|
|
|
5
5
|
class Adapter < ChatSDK::Adapter::Base
|
|
6
6
|
capabilities :edit_messages, :delete_messages, :ephemeral_messages,
|
|
7
7
|
:file_uploads, :reactions, :modals, :typing_indicator,
|
|
8
|
-
:streaming_edit, :threads, :direct_messages, :message_history
|
|
8
|
+
:streaming_edit, :threads, :direct_messages, :message_history,
|
|
9
|
+
:scheduled_messages
|
|
9
10
|
|
|
10
11
|
attr_reader :client
|
|
11
12
|
|
|
@@ -135,11 +136,44 @@ module ChatSDK
|
|
|
135
136
|
@client.reactions_remove(channel: channel_id, timestamp: message_id, name: emoji)
|
|
136
137
|
end
|
|
137
138
|
|
|
139
|
+
def get_user(user_id)
|
|
140
|
+
result = @client.users_info(user: user_id)
|
|
141
|
+
return nil unless result&.dig("user", "id")
|
|
142
|
+
|
|
143
|
+
ChatSDK::Author.new(
|
|
144
|
+
id: result.dig("user", "id"),
|
|
145
|
+
name: result.dig("user", "name"),
|
|
146
|
+
platform: :slack,
|
|
147
|
+
bot: result.dig("user", "is_bot") || false,
|
|
148
|
+
raw: result
|
|
149
|
+
)
|
|
150
|
+
end
|
|
151
|
+
|
|
138
152
|
def open_dm(user_id)
|
|
139
153
|
result = @client.conversations_open(users: user_id)
|
|
140
154
|
result["channel"]["id"]
|
|
141
155
|
end
|
|
142
156
|
|
|
157
|
+
def schedule_message(channel_id:, message:, post_at:, thread_id: nil)
|
|
158
|
+
msg = ChatSDK::PostableMessage.from(message)
|
|
159
|
+
text = msg.text || ""
|
|
160
|
+
|
|
161
|
+
params = {channel: channel_id, text: text, post_at: post_at.to_i}
|
|
162
|
+
params[:thread_ts] = thread_id if thread_id
|
|
163
|
+
|
|
164
|
+
result = @client.chat_scheduleMessage(**params)
|
|
165
|
+
|
|
166
|
+
ChatSDK::Message.new(
|
|
167
|
+
id: result["scheduled_message_id"],
|
|
168
|
+
text: text,
|
|
169
|
+
author: ChatSDK::Author.new(id: "bot", name: "bot", platform: :slack, bot: true),
|
|
170
|
+
thread_id: thread_id || result["scheduled_message_id"],
|
|
171
|
+
channel_id: channel_id,
|
|
172
|
+
platform: :slack,
|
|
173
|
+
raw: result
|
|
174
|
+
)
|
|
175
|
+
end
|
|
176
|
+
|
|
143
177
|
def fetch_messages(channel_id:, thread_id: nil, cursor: nil, limit: 50)
|
|
144
178
|
result = if thread_id
|
|
145
179
|
@client.conversations_replies(channel: channel_id, ts: thread_id, cursor: cursor, limit: limit)
|
|
@@ -155,11 +189,55 @@ module ChatSDK
|
|
|
155
189
|
@client.views_open(trigger_id: trigger_id, view: view)
|
|
156
190
|
end
|
|
157
191
|
|
|
192
|
+
def publish_home_view(user_id:, view:)
|
|
193
|
+
@client.views_publish(user_id: user_id, view: view)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def set_suggested_prompts(channel_id:, thread_id:, prompts:)
|
|
197
|
+
@client.assistant_threads_setSuggestedPrompts(
|
|
198
|
+
channel_id: channel_id,
|
|
199
|
+
thread_ts: thread_id,
|
|
200
|
+
prompts: prompts
|
|
201
|
+
)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def set_assistant_status(channel_id:, thread_id:, status:)
|
|
205
|
+
@client.assistant_threads_setStatus(
|
|
206
|
+
channel_id: channel_id,
|
|
207
|
+
thread_ts: thread_id,
|
|
208
|
+
status: status
|
|
209
|
+
)
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def set_assistant_title(channel_id:, thread_id:, title:)
|
|
213
|
+
@client.assistant_threads_setTitle(
|
|
214
|
+
channel_id: channel_id,
|
|
215
|
+
thread_ts: thread_id,
|
|
216
|
+
title: title
|
|
217
|
+
)
|
|
218
|
+
end
|
|
219
|
+
|
|
158
220
|
def start_typing(channel_id:, thread_id: nil)
|
|
159
221
|
# Slack doesn't have a native typing indicator API for bots
|
|
160
222
|
# This is a no-op but the capability is declared for streaming support
|
|
161
223
|
end
|
|
162
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
|
+
|
|
163
241
|
def mention(user_id)
|
|
164
242
|
"<@#{user_id}>"
|
|
165
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
|