Skip to content

Commit

Permalink
Add complete/fail to context
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Aug 20, 2024
1 parent 0a077ce commit c9a204a
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 45 deletions.
5 changes: 5 additions & 0 deletions bolt/src/main/java/com/slack/api/bolt/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public abstract class Context {
*/
protected String functionBotAccessToken;

/**
* The ID of function_executed event delivery.
*/
protected String functionExecutionId;

/**
* The scopes associated to the botToken
*/
Expand Down
35 changes: 35 additions & 0 deletions bolt/src/main/java/com/slack/api/bolt/context/FunctionUtility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.slack.api.bolt.context;

import com.slack.api.methods.MethodsClient;
import com.slack.api.methods.SlackApiException;
import com.slack.api.methods.request.chat.ChatPostMessageRequest;
import com.slack.api.methods.response.chat.ChatPostMessageResponse;
import com.slack.api.methods.response.functions.FunctionsCompleteErrorResponse;
import com.slack.api.methods.response.functions.FunctionsCompleteSuccessResponse;
import com.slack.api.model.block.LayoutBlock;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public interface FunctionUtility {

String getFunctionExecutionId();

MethodsClient client();

default FunctionsCompleteSuccessResponse complete(Map<String, ?> outputs) throws IOException, SlackApiException {
return this.client().functionsCompleteSuccess(r -> r
.functionExecutionId(this.getFunctionExecutionId())
.outputs(outputs)
);
}

default FunctionsCompleteErrorResponse fail(String error) throws IOException, SlackApiException {
return this.client().functionsCompleteError(r -> r
.functionExecutionId(this.getFunctionExecutionId())
.error(error)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.slack.api.bolt.context.ActionRespondUtility;
import com.slack.api.bolt.context.Context;
import com.slack.api.bolt.context.FunctionUtility;
import com.slack.api.bolt.util.Responder;
import lombok.*;

Expand All @@ -15,7 +16,7 @@
@AllArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class ActionContext extends Context implements ActionRespondUtility {
public class ActionContext extends Context implements ActionRespondUtility, FunctionUtility {

private String triggerId;
private String responseUrl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.slack.api.bolt.context.builtin;

import com.slack.api.bolt.context.Context;
import com.slack.api.bolt.context.FunctionUtility;
import com.slack.api.bolt.context.SayUtility;
import com.slack.api.methods.SlackApiException;
import com.slack.api.methods.response.functions.FunctionsCompleteErrorResponse;
import com.slack.api.methods.response.functions.FunctionsCompleteSuccessResponse;
import lombok.*;

import java.io.IOException;
import java.util.Map;

@Getter
@Setter
@Builder
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
public class EventContext extends Context implements SayUtility {
public class EventContext extends Context implements SayUtility, FunctionUtility {

private String channelId;

Expand All @@ -21,5 +28,4 @@ public class EventContext extends Context implements SayUtility {
// X-Slack-Retry-Reason: http_error in HTTP Mode
// "retry_reason": "timeout", in Socket Mode
private String retryReason;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.slack.api.app_backend.views.payload.ViewSubmissionPayload;
import com.slack.api.app_backend.views.response.ViewSubmissionResponse;
import com.slack.api.bolt.context.Context;
import com.slack.api.bolt.context.FunctionUtility;
import com.slack.api.bolt.context.InputBlockRespondUtility;
import com.slack.api.bolt.util.Responder;
import com.slack.api.bolt.response.Response;
Expand All @@ -20,7 +21,7 @@
@AllArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class ViewSubmissionContext extends Context implements InputBlockRespondUtility {
public class ViewSubmissionContext extends Context implements InputBlockRespondUtility, FunctionUtility {

private List<ViewSubmissionPayload.ResponseUrl> responseUrls;
private Responder responder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public BlockActionRequest(
getContext().setTeamId(payload.getUser().getTeamId());
}
getContext().setRequestUserId(payload.getUser().getId());
if (payload.getFunctionData() != null) {
getContext().setFunctionExecutionId(payload.getFunctionData().getExecutionId());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@ public EventRequest(
this.getContext().setChannelId(event.get("channel_id").getAsString());
}

if (this.eventType != null
&& this.eventType.equals(FunctionExecutedEvent.TYPE_NAME)
&& event.get("bot_access_token") != null) {
String functionBotAccessToken = event.get("bot_access_token").getAsString();
this.getContext().setFunctionBotAccessToken(functionBotAccessToken);
if (this.eventType != null && this.eventType.equals(FunctionExecutedEvent.TYPE_NAME)) {
if (event.get("bot_access_token") != null) {
String functionBotAccessToken = event.get("bot_access_token").getAsString();
this.getContext().setFunctionBotAccessToken(functionBotAccessToken);
}
if (event.get("function_execution_id") != null) {
String functionExecutionId = event.get("function_execution_id").getAsString();
this.getContext().setFunctionExecutionId(functionExecutionId);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public ViewClosedRequest(
}
getContext().setRequestUserId(payload.getUser().getId());
getContext().setFunctionBotAccessToken(payload.getBotAccessToken());
if (payload.getFunctionData() != null) {
getContext().setFunctionExecutionId(payload.getFunctionData().getExecutionId());

Check warning on line 47 in bolt/src/main/java/com/slack/api/bolt/request/builtin/ViewClosedRequest.java

View check run for this annotation

Codecov / codecov/patch

bolt/src/main/java/com/slack/api/bolt/request/builtin/ViewClosedRequest.java#L47

Added line #L47 was not covered by tests
}
}

private DefaultContext context = new DefaultContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public ViewSubmissionRequest(
getContext().setRequestUserId(payload.getUser().getId());
getContext().setResponseUrls(payload.getResponseUrls());
getContext().setFunctionBotAccessToken(payload.getBotAccessToken());
if (payload.getFunctionData() != null) {
getContext().setFunctionExecutionId(payload.getFunctionData().getExecutionId());
}
}

private ViewSubmissionContext context = new ViewSubmissionContext();
Expand Down
Loading

0 comments on commit c9a204a

Please sign in to comment.