Skip to content

Commit c6f0719

Browse files
JrmKrbdblock
authored andcommitted
Add support for Block Kit (#253)
1 parent 0c38a4d commit c6f0719

File tree

6 files changed

+146
-28
lines changed

6 files changed

+146
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [#242](https://github.com/slack-ruby/slack-ruby-client/pull/242): Added `apps_uninstall` to Web API - [@dblock](https://github.com/dblock).
77
* [#244](https://github.com/slack-ruby/slack-ruby-client/pull/244): Implementing #restart for the celluloid socket class - [@RodneyU215](https://github.com/RodneyU215).
88
* [#246](https://github.com/slack-ruby/slack-ruby-client/pull/246): Added TOC to README and danger-toc - [@dblock](https://github.com/dblock).
9+
* [#253](https://github.com/slack-ruby/slack-ruby-client/pull/253): Support for Block Kit - [@JrmKrb](https://github.com/JrmKrb).
910
* Your contribution here.
1011

1112
### 0.13.1 (2018/9/30)

bin/commands/chat.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
c.flag 'user', desc: 'id of the user who will receive the ephemeral message. The user should be in the channel specified by the channel argument.'
5353
c.flag 'as_user', desc: 'Pass true to post the message as the authed user. Defaults to true if the chat:write:bot scope is not included. Otherwise, defaults to false.'
5454
c.flag 'attachments', desc: 'A JSON-based array of structured attachments, presented as a URL-encoded string.'
55+
c.flag 'blocks', desc: 'A JSON-based array of structured blocks, presented as a URL-encoded string.'
5556
c.flag 'link_names', desc: 'Find and link channel names and usernames.'
5657
c.flag 'parse', desc: 'Change how messages are treated. Defaults to none. See below.'
5758
c.flag 'thread_ts', desc: "Provide another message's ts value to make this message a reply. Avoid using a reply's ts value; use its parent instead."
@@ -67,6 +68,7 @@
6768
c.flag 'text', desc: "Text of the message to send. See below for an explanation of formatting. This field is usually required, unless you're providing only attachments instead. Provide no more than 40,000 characters or risk truncation."
6869
c.flag 'as_user', desc: 'Pass true to post the message as the authed user, instead of as a bot. Defaults to false. See authorship below.'
6970
c.flag 'attachments', desc: 'A JSON-based array of structured attachments, presented as a URL-encoded string.'
71+
c.flag 'blocks', desc: 'A JSON-based array of structured blocks, presented as a URL-encoded string.'
7072
c.flag 'icon_emoji', desc: 'Emoji to use as the icon for this message. Overrides icon_url. Must be used in conjunction with as_user set to false, otherwise ignored. See authorship below.'
7173
c.flag 'icon_url', desc: 'URL to an image to use as the icon for this message. Must be used in conjunction with as_user set to false, otherwise ignored. See authorship below.'
7274
c.flag 'link_names', desc: 'Find and link channel names and usernames.'
@@ -104,6 +106,7 @@
104106
c.flag 'ts', desc: 'Timestamp of the message to be updated.'
105107
c.flag 'as_user', desc: 'Pass true to update the message as the authed user. Bot users in this context are considered authed users.'
106108
c.flag 'attachments', desc: 'A JSON-based array of structured attachments, presented as a URL-encoded string. This field is required when not presenting text.'
109+
c.flag 'blocks', desc: 'A JSON-based array of structured blocks, presented as a URL-encoded string.'
107110
c.flag 'link_names', desc: 'Find and link channel names and usernames. Defaults to none. See below.'
108111
c.flag 'parse', desc: 'Change how messages are treated. Defaults to client, unlike chat.postMessage. See below.'
109112
c.action do |_global_options, options, _args|

lib/slack/web/api/endpoints/chat.rb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ def chat_meMessage(options = {})
8585
# Pass true to post the message as the authed user. Defaults to true if the chat:write:bot scope is not included. Otherwise, defaults to false.
8686
# @option options [Object] :attachments
8787
# A JSON-based array of structured attachments, presented as a URL-encoded string.
88+
# @option options [Object] :blocks
89+
# A JSON-based array of structured blocks, presented as a URL-encoded string.
8890
# @option options [Object] :link_names
8991
# Find and link channel names and usernames.
9092
# @option options [Object] :parse
@@ -95,7 +97,7 @@ def chat_meMessage(options = {})
9597
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.postEphemeral.json
9698
def chat_postEphemeral(options = {})
9799
throw ArgumentError.new('Required arguments :channel missing') if options[:channel].nil?
98-
throw ArgumentError.new('Required arguments :text or :attachments missing') if options[:text].nil? && options[:attachments].nil?
100+
throw ArgumentError.new('Required arguments :text, :attachments or :blocks missing') if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil?
99101
throw ArgumentError.new('Required arguments :user missing') if options[:user].nil?
100102
options = options.merge(user: users_id(options)['user']['id']) if options[:user]
101103
# attachments must be passed as an encoded JSON string
@@ -104,6 +106,12 @@ def chat_postEphemeral(options = {})
104106
attachments = JSON.dump(attachments) unless attachments.is_a?(String)
105107
options = options.merge(attachments: attachments)
106108
end
109+
# blocks must be passed as an encoded JSON string
110+
if options.key?(:blocks)
111+
blocks = options[:blocks]
112+
blocks = JSON.dump(blocks) unless blocks.is_a?(String)
113+
options = options.merge(blocks: blocks)
114+
end
107115
post('chat.postEphemeral', options)
108116
end
109117

@@ -118,6 +126,8 @@ def chat_postEphemeral(options = {})
118126
# Pass true to post the message as the authed user, instead of as a bot. Defaults to false. See authorship below.
119127
# @option options [Object] :attachments
120128
# A JSON-based array of structured attachments, presented as a URL-encoded string.
129+
# @option options [Object] :blocks
130+
# A JSON-based array of structured blocks, presented as a URL-encoded string.
121131
# @option options [Object] :icon_emoji
122132
# Emoji to use as the icon for this message. Overrides icon_url. Must be used in conjunction with as_user set to false, otherwise ignored. See authorship below.
123133
# @option options [Object] :icon_url
@@ -142,13 +152,19 @@ def chat_postEphemeral(options = {})
142152
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.postMessage.json
143153
def chat_postMessage(options = {})
144154
throw ArgumentError.new('Required arguments :channel missing') if options[:channel].nil?
145-
throw ArgumentError.new('Required arguments :text or :attachments missing') if options[:text].nil? && options[:attachments].nil?
155+
throw ArgumentError.new('Required arguments :text, :attachments or :blocks missing') if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil?
146156
# attachments must be passed as an encoded JSON string
147157
if options.key?(:attachments)
148158
attachments = options[:attachments]
149159
attachments = JSON.dump(attachments) unless attachments.is_a?(String)
150160
options = options.merge(attachments: attachments)
151161
end
162+
# blocks must be passed as an encoded JSON string
163+
if options.key?(:blocks)
164+
blocks = options[:blocks]
165+
blocks = JSON.dump(blocks) unless blocks.is_a?(String)
166+
options = options.merge(blocks: blocks)
167+
end
152168
post('chat.postMessage', options)
153169
end
154170

@@ -190,6 +206,8 @@ def chat_unfurl(options = {})
190206
# Pass true to update the message as the authed user. Bot users in this context are considered authed users.
191207
# @option options [Object] :attachments
192208
# A JSON-based array of structured attachments, presented as a URL-encoded string. This field is required when not presenting text.
209+
# @option options [Object] :blocks
210+
# A JSON-based array of structured blocks, presented as a URL-encoded string.
193211
# @option options [Object] :link_names
194212
# Find and link channel names and usernames. Defaults to none. See below.
195213
# @option options [Object] :parse
@@ -198,7 +216,7 @@ def chat_unfurl(options = {})
198216
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.update.json
199217
def chat_update(options = {})
200218
throw ArgumentError.new('Required arguments :channel missing') if options[:channel].nil?
201-
throw ArgumentError.new('Required arguments :text or :attachments missing') if options[:text].nil? && options[:attachments].nil?
219+
throw ArgumentError.new('Required arguments :text, :attachments or :blocks missing') if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil?
202220
throw ArgumentError.new('Required arguments :ts missing') if options[:ts].nil?
203221
options = options.merge(channel: channels_id(options)['channel']['id']) if options[:channel]
204222
# attachments must be passed as an encoded JSON string
@@ -207,6 +225,12 @@ def chat_update(options = {})
207225
attachments = JSON.dump(attachments) unless attachments.is_a?(String)
208226
options = options.merge(attachments: attachments)
209227
end
228+
# blocks must be passed as an encoded JSON string
229+
if options.key?(:blocks)
230+
blocks = options[:blocks]
231+
blocks = JSON.dump(blocks) unless blocks.is_a?(String)
232+
options = options.merge(blocks: blocks)
233+
end
210234
post('chat.update', options)
211235
end
212236
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
diff --git a/lib/slack/web/api/endpoints/chat.rb b/lib/slack/web/api/endpoints/chat.rb
2+
index 54a7db1..c535bb5 100644
3+
--- a/lib/slack/web/api/endpoints/chat.rb
4+
+++ b/lib/slack/web/api/endpoints/chat.rb
5+
@@ -97,7 +97,7 @@ module Slack
6+
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.postEphemeral.json
7+
def chat_postEphemeral(options = {})
8+
throw ArgumentError.new('Required arguments :channel missing') if options[:channel].nil?
9+
- throw ArgumentError.new('Required arguments :text or :attachments missing') if options[:text].nil? && options[:attachments].nil?
10+
+ throw ArgumentError.new('Required arguments :text, :attachments or :blocks missing') if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil?
11+
throw ArgumentError.new('Required arguments :user missing') if options[:user].nil?
12+
options = options.merge(user: users_id(options)['user']['id']) if options[:user]
13+
# attachments must be passed as an encoded JSON string
14+
@@ -106,6 +106,12 @@ module Slack
15+
attachments = JSON.dump(attachments) unless attachments.is_a?(String)
16+
options = options.merge(attachments: attachments)
17+
end
18+
+ # blocks must be passed as an encoded JSON string
19+
+ if options.key?(:blocks)
20+
+ blocks = options[:blocks]
21+
+ blocks = JSON.dump(blocks) unless blocks.is_a?(String)
22+
+ options = options.merge(blocks: blocks)
23+
+ end
24+
post('chat.postEphemeral', options)
25+
end
26+
27+
@@ -146,13 +152,19 @@ module Slack
28+
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.postMessage.json
29+
def chat_postMessage(options = {})
30+
throw ArgumentError.new('Required arguments :channel missing') if options[:channel].nil?
31+
- throw ArgumentError.new('Required arguments :text or :attachments missing') if options[:text].nil? && options[:attachments].nil?
32+
+ throw ArgumentError.new('Required arguments :text, :attachments or :blocks missing') if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil?
33+
# attachments must be passed as an encoded JSON string
34+
if options.key?(:attachments)
35+
attachments = options[:attachments]
36+
attachments = JSON.dump(attachments) unless attachments.is_a?(String)
37+
options = options.merge(attachments: attachments)
38+
end
39+
+ # blocks must be passed as an encoded JSON string
40+
+ if options.key?(:blocks)
41+
+ blocks = options[:blocks]
42+
+ blocks = JSON.dump(blocks) unless blocks.is_a?(String)
43+
+ options = options.merge(blocks: blocks)
44+
+ end
45+
post('chat.postMessage', options)
46+
end
47+
48+
@@ -204,7 +216,7 @@ module Slack
49+
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.update.json
50+
def chat_update(options = {})
51+
throw ArgumentError.new('Required arguments :channel missing') if options[:channel].nil?
52+
- throw ArgumentError.new('Required arguments :text or :attachments missing') if options[:text].nil? && options[:attachments].nil?
53+
+ throw ArgumentError.new('Required arguments :text, :attachments or :blocks missing') if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil?
54+
throw ArgumentError.new('Required arguments :ts missing') if options[:ts].nil?
55+
options = options.merge(channel: channels_id(options)['channel']['id']) if options[:channel]
56+
# attachments must be passed as an encoded JSON string
57+
@@ -213,6 +225,12 @@ module Slack
58+
attachments = JSON.dump(attachments) unless attachments.is_a?(String)
59+
options = options.merge(attachments: attachments)
60+
end
61+
+ # blocks must be passed as an encoded JSON string
62+
+ if options.key?(:blocks)
63+
+ blocks = options[:blocks]
64+
+ blocks = JSON.dump(blocks) unless blocks.is_a?(String)
65+
+ options = options.merge(blocks: blocks)
66+
+ end
67+
post('chat.update', options)
68+
end
69+
end

lib/slack/web/api/slack-api-ref

Submodule slack-api-ref updated 216 files

0 commit comments

Comments
 (0)