Skip to content

Commit 704ad9f

Browse files
committed
Add builder argument patterns for assistant thread utilities
1 parent a767aa5 commit 704ad9f

File tree

3 files changed

+71
-13
lines changed

3 files changed

+71
-13
lines changed

bolt-socket-mode/src/test/java/samples/AssistantSimpleApp.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,31 @@ public static void main(String[] args) throws Exception {
2222

2323
assistant.threadStarted((req, ctx) -> {
2424
try {
25-
ctx.say(r -> r.text("Hi, how can I help you today?"));
26-
ctx.setSuggestedPrompts(Collections.singletonList(
27-
SuggestedPrompt.create("What does SLACK stand for?")
28-
));
25+
ctx.say("Hi, how can I help you today?");
26+
ctx.setSuggestedPrompts(r -> r
27+
.title("Select one of the following:") // optional
28+
.prompts(Collections.singletonList(SuggestedPrompt.create("What does SLACK stand for?")))
29+
);
2930
} catch (Exception e) {
3031
ctx.logger.error("Failed to handle assistant thread started event: {e}", e);
3132
}
3233
});
3334

3435
assistant.userMessage((req, ctx) -> {
3536
try {
37+
// ctx.setStatus(r -> r.status("is typing...")); works too
3638
ctx.setStatus("is typing...");
3739
Thread.sleep(500L);
3840
if (ctx.getThreadContext() != null) {
3941
String contextChannel = ctx.getThreadContext().getChannelId();
40-
ctx.say(r -> r.text("I am ware of the channel context: <#" + contextChannel + ">"));
42+
ctx.say("I am ware of the channel context: <#" + contextChannel + ">");
4143
} else {
42-
ctx.say(r -> r.text("Here you are!"));
44+
ctx.say("Here you are!");
4345
}
4446
} catch (Exception e) {
4547
ctx.logger.error("Failed to handle assistant user message event: {e}", e);
4648
try {
47-
ctx.say(r -> r.text(":warning: Sorry, something went wrong during processing your request!"));
49+
ctx.say(":warning: Sorry, something went wrong during processing your request!");
4850
} catch (Exception ee) {
4951
ctx.logger.error("Failed to inform the error to the end-user: {ee}", ee);
5052
}
@@ -57,11 +59,11 @@ public static void main(String[] args) throws Exception {
5759
Thread.sleep(500L);
5860
ctx.setStatus("is still checking the files...");
5961
Thread.sleep(500L);
60-
ctx.say(r -> r.text("Your files do not have any issues!"));
62+
ctx.say("Your files do not have any issues!");
6163
} catch (Exception e) {
6264
ctx.logger.error("Failed to handle assistant user message event: {e}", e);
6365
try {
64-
ctx.say(r -> r.text(":warning: Sorry, something went wrong during processing your request!"));
66+
ctx.say(":warning: Sorry, something went wrong during processing your request!");
6567
} catch (Exception ee) {
6668
ctx.logger.error("Failed to inform the error to the end-user: {ee}", ee);
6769
}

bolt/src/main/java/com/slack/api/bolt/context/builtin/EventContext.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package com.slack.api.bolt.context.builtin;
22

3+
import com.slack.api.RequestConfigurator;
34
import com.slack.api.bolt.context.Context;
45
import com.slack.api.bolt.context.FunctionUtility;
56
import com.slack.api.bolt.context.SayUtility;
67
import com.slack.api.bolt.service.AssistantThreadContextService;
78
import com.slack.api.bolt.util.BuilderConfigurator;
89
import com.slack.api.methods.SlackApiException;
10+
import com.slack.api.methods.request.assistant.threads.AssistantThreadsSetStatusRequest;
11+
import com.slack.api.methods.request.assistant.threads.AssistantThreadsSetSuggestedPromptsRequest;
12+
import com.slack.api.methods.request.assistant.threads.AssistantThreadsSetTitleRequest;
913
import com.slack.api.methods.request.chat.ChatPostMessageRequest;
1014
import com.slack.api.methods.response.asssistant.threads.AssistantThreadsSetStatusResponse;
1115
import com.slack.api.methods.response.asssistant.threads.AssistantThreadsSetSuggestedPromptsResponse;
16+
import com.slack.api.methods.response.asssistant.threads.AssistantThreadsSetTitleResponse;
1217
import com.slack.api.methods.response.chat.ChatPostMessageResponse;
1318
import com.slack.api.model.Message;
1419
import com.slack.api.model.assistant.AssistantThreadContext;
@@ -111,6 +116,40 @@ public AssistantThreadsSetStatusResponse setStatus(String status) throws IOExcep
111116
}
112117
}
113118

119+
public AssistantThreadsSetStatusResponse setStatus(RequestConfigurator<AssistantThreadsSetStatusRequest.AssistantThreadsSetStatusRequestBuilder> req) throws IOException, SlackApiException {
120+
if (isAssistantThreadEvent()) {
121+
return this.client().assistantThreadsSetStatus(req.configure(AssistantThreadsSetStatusRequest.builder()
122+
.channelId(this.getChannelId())
123+
.threadTs(this.getThreadTs())
124+
).build());
125+
} else {
126+
throw new IllegalStateException("This utility is only available for Assistant feature enabled app!");
127+
}
128+
}
129+
130+
public AssistantThreadsSetTitleResponse setTitle(String title) throws IOException, SlackApiException {
131+
if (isAssistantThreadEvent()) {
132+
return this.client().assistantThreadsSetTitle(r -> r
133+
.channelId(this.getChannelId())
134+
.threadTs(this.getThreadTs())
135+
.title(title)
136+
);
137+
} else {
138+
throw new IllegalStateException("This utility is only available for Assistant feature enabled app!");
139+
}
140+
}
141+
142+
public AssistantThreadsSetTitleResponse setTitle(RequestConfigurator<AssistantThreadsSetTitleRequest.AssistantThreadsSetTitleRequestBuilder> req) throws IOException, SlackApiException {
143+
if (isAssistantThreadEvent()) {
144+
return this.client().assistantThreadsSetTitle(req.configure(AssistantThreadsSetTitleRequest.builder()
145+
.channelId(this.getChannelId())
146+
.threadTs(this.getThreadTs())
147+
).build());
148+
} else {
149+
throw new IllegalStateException("This utility is only available for Assistant feature enabled app!");
150+
}
151+
}
152+
114153
public AssistantThreadsSetSuggestedPromptsResponse setSuggestedPrompts(List<SuggestedPrompt> prompts) throws IOException, SlackApiException {
115154
if (isAssistantThreadEvent()) {
116155
return this.client().assistantThreadsSetSuggestedPrompts(r -> r
@@ -123,6 +162,17 @@ public AssistantThreadsSetSuggestedPromptsResponse setSuggestedPrompts(List<Sugg
123162
}
124163
}
125164

165+
public AssistantThreadsSetSuggestedPromptsResponse setSuggestedPrompts(RequestConfigurator<AssistantThreadsSetSuggestedPromptsRequest.AssistantThreadsSetSuggestedPromptsRequestBuilder> req) throws IOException, SlackApiException {
166+
if (isAssistantThreadEvent()) {
167+
return this.client().assistantThreadsSetSuggestedPrompts(req.configure(AssistantThreadsSetSuggestedPromptsRequest.builder()
168+
.channelId(this.getChannelId())
169+
.threadTs(this.getThreadTs())
170+
).build());
171+
} else {
172+
throw new IllegalStateException("This utility is only available for Assistant feature enabled app!");
173+
}
174+
}
175+
126176
public AssistantThreadsSetSuggestedPromptsResponse setSuggestedPrompts(List<SuggestedPrompt> prompts, String title) throws IOException, SlackApiException {
127177
if (isAssistantThreadEvent()) {
128178
return this.client().assistantThreadsSetSuggestedPrompts(r -> r

bolt/src/test/java/test_locally/app/EventAssistantTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
import util.AuthTestMockServer;
1717
import util.MockSlackApiServer;
1818

19-
import java.util.Collections;
20-
import java.util.HashMap;
21-
import java.util.List;
22-
import java.util.Map;
19+
import java.util.*;
2320
import java.util.concurrent.atomic.AtomicBoolean;
2421

2522
import static org.junit.Assert.assertEquals;
@@ -55,6 +52,15 @@ public void test() throws Exception {
5552
Assistant assistant = new Assistant(app.executorService());
5653

5754
assistant.threadStarted((req, ctx) -> {
55+
try {
56+
ctx.setTitle("title");
57+
ctx.setTitle(r -> r.title("title"));
58+
ctx.setStatus("is typing...");
59+
ctx.setStatus(r -> r.status("is typing..."));
60+
ctx.setSuggestedPrompts(new ArrayList<>());
61+
ctx.setSuggestedPrompts(r -> r.prompts(new ArrayList<>()));
62+
} catch (Exception e) {
63+
}
5864
threadStartedReceived.set(true);
5965
});
6066
assistant.threadContextChanged((req, ctx) -> {

0 commit comments

Comments
 (0)