0

I want to send a message in a channel in my slack workspace. I have seen many tutorials on how to use it but they are all in node.js, python but not java. Is there any way I could send a message at the slack channel #general when someone opens my app? Thanks in advance

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

1

You can follow below steps:

  1. First create your app for custom messages using this URL

  2. If you want to send messages in various beautiful layouts, follow this link

  3. Sample working code for aforementioned layouts:

Code:

        List<LayoutBlock> message = new ArrayList<>();
        message.add(SectionBlock.builder()
            .text(MarkdownTextObject.builder().text("*"Subject of the message "*").build()).build());

message.add(
            SectionBlock
                    .builder().fields(
                            Arrays.asList(
                                    MarkdownTextObject.builder()
                                            .text("*First Topic:* "Your text")
                                            .build(),
MarkdownTextObject.builder().text("*Second topic:* "Your text ").build())).build());

message.add(DividerBlock.builder().build());

ChatPostMessageRequest request = ChatPostMessageRequest.builder().channel("Your Slack Channel ID").blocks(message)
            .build();
ChatPostMessageResponse response = methodsClient.chatPostMessage(request);
response.getChannel();

PS: If you want to add any link in your message:

MarkdownTextObject.builder().text("*Link Text:* <"https://.."|click here>").build()

Note: You can edit the layout as per your ease, refer this url

0

Incoming webhooks on Slack are very easy to use and basically work with any programming language / platform as long as you are able to send a HTTP POST request.

First I would recommend to check out the excellent documentation with step-by-step instructions on how to get started with webhooks.

Then to get the feel for how webhooks I would advise playing around with it using tools like Postman or cURL.

Last but not least: if you want to learn how to make a HTTP POST request in Java, check out this answer.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • Thanks! Also, I can use the HTTP POST to use discord webhooks as well? (sorry for being a noob) –  Sep 11 '18 at 15:08
  • yes. Discord webhooks work similar and you can also use HTTP POST requests to use them. See the documentation for it here: https://discordapp.com/developers/docs/resources/webhook#execute-webhook – Erik Kalkoken Sep 11 '18 at 15:17
  • Cool, ill try it and I will ask if I have any problems, thanks for your help –  Sep 11 '18 at 15:37