Skip to content

Commit e2b7874

Browse files
authored
Fix #895 Calling chat.unfurl with user_auth_blocks resulting in error invalid_arguments (#896)
1 parent 952791e commit e2b7874

File tree

3 files changed

+108
-7
lines changed

3 files changed

+108
-7
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package samples;
2+
3+
import com.slack.api.app_backend.events.payload.EventsApiPayload;
4+
import com.slack.api.bolt.App;
5+
import com.slack.api.bolt.AppConfig;
6+
import com.slack.api.bolt.context.builtin.EventContext;
7+
import com.slack.api.methods.response.chat.ChatUnfurlResponse;
8+
import com.slack.api.model.event.LinkSharedEvent;
9+
import com.slack.api.model.event.MessageEvent;
10+
import util.ResourceLoader;
11+
import util.TestSlackAppServer;
12+
13+
import java.util.concurrent.CompletableFuture;
14+
15+
import static com.slack.api.model.block.Blocks.*;
16+
import static com.slack.api.model.block.composition.BlockCompositions.plainText;
17+
import static com.slack.api.model.block.element.BlockElements.asElements;
18+
import static com.slack.api.model.block.element.BlockElements.button;
19+
20+
public class AuthenticatedUnfurlsSample {
21+
22+
public static CompletableFuture<ChatUnfurlResponse> displayUserAuthRequired(
23+
EventsApiPayload<LinkSharedEvent> req,
24+
EventContext ctx
25+
) {
26+
return ctx.asyncClient().chatUnfurl(r -> r
27+
.channel(ctx.getChannelId())
28+
.ts(req.getEvent().getMessageTs())
29+
.userAuthRequired(true)
30+
);
31+
}
32+
33+
public static CompletableFuture<ChatUnfurlResponse> displayUserAuthMessage(
34+
EventsApiPayload<LinkSharedEvent> req,
35+
EventContext ctx
36+
) {
37+
return ctx.asyncClient().chatUnfurl(r -> r
38+
.channel(ctx.getChannelId())
39+
.ts(req.getEvent().getMessageTs())
40+
.userAuthMessage("Loading the preview... <https://www.example.com/auth|Connect your account>")
41+
);
42+
}
43+
44+
public static CompletableFuture<ChatUnfurlResponse> displayUserAuthUrl(
45+
EventsApiPayload<LinkSharedEvent> req,
46+
EventContext ctx
47+
) {
48+
return ctx.asyncClient().chatUnfurl(r -> r
49+
.channel(ctx.getChannelId())
50+
.ts(req.getEvent().getMessageTs())
51+
.userAuthUrl("https://www.example.com/auth")
52+
);
53+
}
54+
55+
public static CompletableFuture<ChatUnfurlResponse> displayUserAuthBlocks(
56+
EventsApiPayload<LinkSharedEvent> req,
57+
EventContext ctx
58+
) {
59+
return ctx.asyncClient().chatUnfurl(r -> r
60+
.channel(ctx.getChannelId())
61+
.ts(req.getEvent().getMessageTs())
62+
.userAuthBlocks(asBlocks(
63+
section(s -> s
64+
.blockId("intro-section")
65+
.text(plainText("Loading the preview... :eyes:"))
66+
),
67+
actions(a -> a.elements(asElements(
68+
button(b -> b
69+
.actionId("button-click")
70+
.url("https://www.example.com/auth")
71+
.text(plainText("Connect your account"))
72+
)
73+
)))
74+
))
75+
);
76+
}
77+
78+
public static void main(String[] args) throws Exception {
79+
AppConfig config = ResourceLoader.loadAppConfig();
80+
App app = new App(config);
81+
app.event(MessageEvent.class, (req, ctx) -> ctx.ack());
82+
app.event(LinkSharedEvent.class, (req, ctx) -> {
83+
displayUserAuthRequired(req, ctx);
84+
displayUserAuthMessage(req, ctx);
85+
displayUserAuthBlocks(req, ctx);
86+
displayUserAuthUrl(req, ctx);
87+
return ctx.ack();
88+
});
89+
app.blockAction("button-click", (req, ctx) -> ctx.ack());
90+
91+
TestSlackAppServer server = new TestSlackAppServer(app);
92+
server.start();
93+
}
94+
}

slack-api-client/src/main/java/com/slack/api/methods/RequestFormBuilder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,12 @@ public static FormBody.Builder toForm(ChatUnfurlRequest req) {
12291229
}
12301230
setIfNotNull("user_auth_required", req.isUserAuthRequired(), form);
12311231
setIfNotNull("user_auth_message", req.getUserAuthMessage(), form);
1232-
setIfNotNull("user_auth_blocks", req.getUserAuthBlocks(), form);
1232+
if (req.getRawUserAuthBlocks() != null) {
1233+
setIfNotNull("user_auth_blocks", req.getRawUserAuthBlocks(), form);
1234+
} else if (req.getUserAuthBlocks() != null) {
1235+
String json = GSON.toJson(req.getUserAuthBlocks());
1236+
setIfNotNull("user_auth_blocks", json, form);
1237+
}
12331238
setIfNotNull("user_auth_url", req.getUserAuthUrl(), form);
12341239
setIfNotNull("unfurl_id", req.getUnfurlId(), form);
12351240
setIfNotNull("source", req.getSource(), form);

slack-api-client/src/main/java/com/slack/api/methods/request/chat/ChatUnfurlRequest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class ChatUnfurlRequest implements SlackApiRequest {
2828
*/
2929
private String userAuthMessage;
3030

31+
private String rawUserAuthBlocks;
32+
3133
/**
3234
* Provide an array of blocks to send as an ephemeral message to the user
3335
* as invitation to authenticate further and enable full unfurling behavior
@@ -39,6 +41,12 @@ public class ChatUnfurlRequest implements SlackApiRequest {
3941
*/
4042
private boolean userAuthRequired;
4143

44+
/**
45+
* Send users to this custom URL where they will complete authentication in your app to fully trigger unfurling.
46+
* Value should be properly URL-encoded.
47+
*/
48+
private String userAuthUrl;
49+
4250
/**
4351
* URL-encoded JSON map with keys set to URLs featured in the message, pointing to their unfurl message attachments.
4452
*/
@@ -51,12 +59,6 @@ public class ChatUnfurlRequest implements SlackApiRequest {
5159
*/
5260
private String ts;
5361

54-
/**
55-
* Send users to this custom URL where they will complete authentication in your app to fully trigger unfurling.
56-
* Value should be properly URL-encoded.
57-
*/
58-
private String userAuthUrl;
59-
6062
/**
6163
* Channel ID of the message
6264
*/

0 commit comments

Comments
 (0)