slackapi / java-slack-sdk

Slack Developer Kit (including Bolt for Java) for any JVM language
https://tools.slack.dev/java-slack-sdk/
MIT License
578 stars 215 forks source link

when a button is clicked in a slack app it sends a post request to my url but i am not getting the payload it is returning null #1188

Closed Harishkumarcratoflow closed 1 year ago

Harishkumarcratoflow commented 1 year ago
@PostMapping(value ="/slack-response",produces =MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String responseAction(String payload) {
    JsonObject json = JsonParser.parseString(payload).getAsJsonObject();
    String actionId = json.getAsJsonArray("actions")
        .get(0).getAsJsonObject()
        .get("action_id").getAsString();
    System.out.println("action Id : " + actionId);
    return "Success";
}

this is the code

seratch commented 1 year ago

Hi @Harishkumarcratoflow, thanks for asking the question, but I'm still not entirely sure what the challenge you're facing is.

Perhaps, you're trying to add Slack app code into a Spring Boot app. If you're working on an existing app, the Spring Boot app may have some Spring Boot extensions, which consume request body data beforehand. In this case, your app may not work properly.

To narrow down your issue, please start with a brand-new simple Spring Boot app first. You can follow the instructions here: https://slack.dev/java-slack-sdk/guides/supported-web-frameworks#spring-boot. Also, we've already answered numerous questions about Spring Boot in the past. Checking the past issues may be helpful too: https://github.com/slackapi/java-slack-sdk/issues?q=is%3Aissue+spring+boot+is%3Aclosed

I hope you will figure the cause out soon.

Harishkumarcratoflow commented 1 year ago
@PostMapping(value ="/slack-response",produces =MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String responseAction(SlackInteractionPayload payload) {
    System.out.println(payload.getType());
    return "Ok";

}

While having the interaction in slack,I am getting the request payload as null.Is there any other way to access the paylaod?

Jasmine-maryj commented 1 year ago

We use @RequestBody annotation to access the payload in Spring Boot

seratch commented 1 year ago

@Jasmine-maryj Thanks for sharing your knowledge! @Harishkumarcratoflow We highly discourage implementing the code to parse request body data. It's not just for the sake of simplicity, but also for enhanced security. The Bolt framework verifies if a request originates from Slack and ensures the data is never manipulated. For this purpose, the framework parses body data as-is and verifies the x-slack-signature header in the request using the "Signing Secret", which is shared exclusively between Slack and you. Therefore, please consider relying on the Bolt framework instead of trying to implement everything on your own.

Jasmine-maryj commented 1 year ago

Thank you for the information; I have not worked on Slack and Spring Boot before.

seratch commented 1 year ago

@Harishkumarcratoflow Here is my reply to your question https://github.com/slackapi/java-slack-sdk/issues/1191

Please refer to the document for code examples: https://slack.dev/java-slack-sdk/guides/interactive-components To learn how to construct button UI in blocks in message, modal, or home tab, you can use Block Kit Builder (https://app.slack.com/block-kit-builder) for it.

To run the code as part of a Spring Boot app, you can reuse the code here: https://github.com/slackapi/java-slack-sdk/blob/v1.30.0/bolt-spring-boot-examples/spring-boot-3/src/main/java/example/SlackApp.java#L42-L44

I hope you will figure how to build your app out soon!

github-actions[bot] commented 1 year ago

👋 It looks like this issue has been open for 30 days with no activity. We'll mark this as stale for now, and wait 10 days for an update or for further comment before closing this issue out. If you think this issue needs to be prioritized, please comment to get the thread going again! Maintainers also review issues marked as stale on a regular basis and comment or adjust status if the issue needs to be reprioritized.

github-actions[bot] commented 1 year ago

As this issue has been inactive for more than one month, we will be closing it. Thank you to all the participants! If you would like to raise a related issue, please create a new issue which includes your specific details and references this issue number.