Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slack-api: how to format text that contains url?

Tags:

java

slack-api

I want to post a message on slack using allbegrey Slack API . The message contains links. How to do so?

  <dependency>
      <groupId>com.github.allbegray</groupId>
      <artifactId>slack-api</artifactId>
      <version>1.7.0.RELEASE</version>
    </dependency>

Info:

My message is similar to this one.

enter image description here

Udpate

If you want to post a simple message to a channel. And your message contains a url. You use the slack markup to format the message and then call postMessage(String channel, String text). It works, but if you have attachments it does not work.


My message has attachments and I do it like this:

        val text = "Nostrum <https://codepen.io/anon/pen/VRRMNJ|similique> dolores eaque vero voluptatibus illum quibusdam ex sapiente!"
        val message = "Lorem ipsum dolor sit amet consectetur, adipisicing elit. Expedita tempora asperiores corporis hic quidem dicta maiores odit illo perferendis quaerat. Nostrum <https://codepen.io/anon/pen/VRRMNJ|similique> dolores eaque vero voluptatibus illum quibusdam ex sapiente!"


        val webApiClient = SlackWebApiClientImpl(slackApiToken, null, 8000)
        val channel = webApiClient.openDirectMessageChannel(author.slackId)
        val method = ChatPostMessageMethod(channel, text)
        val attachment = Attachment()

        attachment.author_name = author.name
        attachment.author_icon = author.profileImageUrl
        attachment.author_link = "https://XXXX.slack.com/team/${author.slackId}"
        attachment.text =  message
        attachment.pretext = message

        method.attachments = listOf(attachment)
        method.isLink_names = true
        webApiClient.postMessage(method)

Doing so, I get the message but without clickable links.

like image 671
mahan Avatar asked Nov 20 '25 11:11

mahan


1 Answers

I have never used this library, but after a cursory looks it seams to expose all the standard API methods as class methods.

So you should be able to use the usual Slack markup for formatting you text messages.

Here is an example on how to include URLs in your text:

Check out this link at <http://www.google.com|Google>. Its pretty cool.

Just call the method postMessage() with your message in text and it should work.

like image 104
Erik Kalkoken Avatar answered Nov 23 '25 02:11

Erik Kalkoken