chat_sdk-slack 0.6.0 → 0.8.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/format_converter.rb +104 -0
- data/lib/chat_sdk/slack/socket_mode.rb +107 -0
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 891f26261fed4ebb89b98b19ec57f2b304d9d3f8bedea493faa1457ac1bb742e
|
|
4
|
+
data.tar.gz: 9c1cc01f502f92642b89944aafc0ad24188d92c3c734d67735f00299c3bc6571
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4f59922bead78ae2f1f8a81cfc7f990d4ab2ef3c1fd193463def34897cb286696ac61c34699156435e21ed61c436099cfe1e3935ae84600b49c739f8beae8264
|
|
7
|
+
data.tar.gz: 07257e9662397d48d5c19798a2d8e5292b541ee600f046683a44b4b43b285dd1ab96900aa2eb0c5c593da885a98e26d8269c1c17a31b8c81d7cf7d549cefb3ca
|
|
@@ -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,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ChatSDK
|
|
4
|
+
module Slack
|
|
5
|
+
class FormatConverter < ChatSDK::Format::Converter
|
|
6
|
+
# Slack mrkdwn -> standard Markdown
|
|
7
|
+
def to_markdown(mrkdwn)
|
|
8
|
+
return "" if mrkdwn.nil? || mrkdwn.empty?
|
|
9
|
+
|
|
10
|
+
parts = split_code_blocks(mrkdwn)
|
|
11
|
+
parts.map.with_index { |part, i| i.odd? ? part : convert_mrkdwn_to_md(part) }.join
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Standard Markdown -> Slack mrkdwn
|
|
15
|
+
def from_markdown(markdown)
|
|
16
|
+
return "" if markdown.nil? || markdown.empty?
|
|
17
|
+
|
|
18
|
+
parts = split_code_blocks(markdown)
|
|
19
|
+
parts.map.with_index { |part, i| i.odd? ? part : convert_md_to_mrkdwn(part) }.join
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
# Splits text into alternating [text, code, text, code, ...] segments.
|
|
25
|
+
# Odd-indexed segments are code blocks (including their delimiters) and
|
|
26
|
+
# should be passed through without conversion.
|
|
27
|
+
def split_code_blocks(text)
|
|
28
|
+
# Match fenced code blocks (```...```) and inline code (`...`)
|
|
29
|
+
# The fenced block pattern must come first so triple backticks aren't
|
|
30
|
+
# eaten by the inline pattern.
|
|
31
|
+
text.split(/(```[\s\S]*?```|`[^`]+`)/)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# ── Slack mrkdwn → Markdown ──────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
def convert_mrkdwn_to_md(text)
|
|
37
|
+
result = text
|
|
38
|
+
|
|
39
|
+
# Mentions must be processed before generic links to avoid <#C123|name> matching <url|text>
|
|
40
|
+
# User mentions: <@U123ABC> -> @U123ABC
|
|
41
|
+
result = result.gsub(/<@([A-Z0-9]+)>/, '@\1')
|
|
42
|
+
|
|
43
|
+
# Channel mentions with label: <#C123|general> -> #general
|
|
44
|
+
result = result.gsub(/<#[A-Z0-9]+\|([^>]+)>/, '#\1')
|
|
45
|
+
|
|
46
|
+
# Channel mentions without label: <#C123> -> #C123
|
|
47
|
+
result = result.gsub(/<#([A-Z0-9]+)>/, '#\1')
|
|
48
|
+
|
|
49
|
+
# Special mentions: <!everyone>, <!here>, <!channel>
|
|
50
|
+
result = result.gsub("<!everyone>", "@everyone")
|
|
51
|
+
result = result.gsub("<!here>", "@here")
|
|
52
|
+
result = result.gsub("<!channel>", "@channel")
|
|
53
|
+
|
|
54
|
+
# Links with labels: <url|text> -> [text](url) (after mentions are consumed)
|
|
55
|
+
result = result.gsub(/<([^>|]+)\|([^>]+)>/) { "[#{Regexp.last_match(2)}](#{Regexp.last_match(1)})" }
|
|
56
|
+
|
|
57
|
+
# Bare links: <url> -> url (must come after labeled links)
|
|
58
|
+
result = result.gsub(/<([^@#!][^>|]*)>/, '\1')
|
|
59
|
+
|
|
60
|
+
# Bold: *text* -> **text** (only single asterisks, not already doubled)
|
|
61
|
+
# Negative lookbehind/lookahead to avoid matching inside ** pairs
|
|
62
|
+
result = result.gsub(/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/, '**\1**')
|
|
63
|
+
|
|
64
|
+
# Italic: _text_ -> *text*
|
|
65
|
+
result = result.gsub(/(?<![a-zA-Z0-9])_(.+?)_(?![a-zA-Z0-9])/, '*\1*')
|
|
66
|
+
|
|
67
|
+
# Strikethrough: ~text~ -> ~~text~~
|
|
68
|
+
result = result.gsub(/(?<!~)~(?!~)(.+?)(?<!~)~(?!~)/, '~~\1~~')
|
|
69
|
+
|
|
70
|
+
# HTML entities (must come last so earlier patterns can match on encoded text)
|
|
71
|
+
result = result.gsub(">", ">")
|
|
72
|
+
result = result.gsub("<", "<")
|
|
73
|
+
result.gsub("&", "&")
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# ── Markdown → Slack mrkdwn ──────────────────────────────────────────
|
|
77
|
+
|
|
78
|
+
def convert_md_to_mrkdwn(text)
|
|
79
|
+
result = text
|
|
80
|
+
|
|
81
|
+
# Links: [text](url) -> <url|text>
|
|
82
|
+
result = result.gsub(/\[([^\]]+)\]\(([^)]+)\)/) { "<#{Regexp.last_match(2)}|#{Regexp.last_match(1)}>" }
|
|
83
|
+
|
|
84
|
+
# Bold: **text** -> *text* (use placeholder to prevent italic pass from catching it)
|
|
85
|
+
result = result.gsub(/\*\*(.+?)\*\*/, "\x00BOLD\\1BOLD\x00")
|
|
86
|
+
|
|
87
|
+
# Italic: *text* -> _text_ (only single asterisks remaining after bold placeholder)
|
|
88
|
+
result = result.gsub(/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/, '_\1_')
|
|
89
|
+
|
|
90
|
+
# Restore bold placeholders
|
|
91
|
+
result = result.gsub(/\x00BOLD(.+?)BOLD\x00/, '*\1*')
|
|
92
|
+
|
|
93
|
+
# Strikethrough: ~~text~~ -> ~text~
|
|
94
|
+
result = result.gsub(/~~(.+?)~~/, '~\1~')
|
|
95
|
+
|
|
96
|
+
# Blockquote lines: > text -> > text (only at line start)
|
|
97
|
+
result.gsub(/^> /m, "> ")
|
|
98
|
+
|
|
99
|
+
# HTML entities for remaining special chars are not converted here —
|
|
100
|
+
# Slack handles raw < > & in normal text. Only blockquote > needs encoding.
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
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.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Quentin Rousseau
|
|
@@ -48,7 +48,9 @@ files:
|
|
|
48
48
|
- lib/chat_sdk/slack/adapter.rb
|
|
49
49
|
- lib/chat_sdk/slack/block_kit_renderer.rb
|
|
50
50
|
- lib/chat_sdk/slack/event_parser.rb
|
|
51
|
+
- lib/chat_sdk/slack/format_converter.rb
|
|
51
52
|
- lib/chat_sdk/slack/modal_renderer.rb
|
|
53
|
+
- lib/chat_sdk/slack/socket_mode.rb
|
|
52
54
|
homepage: https://github.com/rootlyhq/chat-sdk
|
|
53
55
|
licenses:
|
|
54
56
|
- MIT
|