1

On the Slack website, I can format a message like this:

{
    "text": "<http://google.com|link to google>"
}

And in the message it will appear like this:

link to google

But I am trying to write a bot and those links aren't working. Using my websocket connection I can send a message like this:

var send = new MessageToSlack()
{
    type = "message",
    channel = msg.channel,
    text = $"http://google.com"
};
ws.SendAsync(JsonConvert.SerializeObject(send), b => { });

And Slack will correctly interpret http://google.com as a link and display it like:

http://google.com

But if I try to send a message with the link in angle brackets with the pipe between the link and the link text (which works on Slack's site) like this:

var send = new MessageToSlack()
{
    type = "message",
    channel = msg.channel,
    text = $"<http://google.com|to google>"
};
ws.SendAsync(JsonConvert.SerializeObject(send), b => { });

I end up with:

<http://google.com|to google>

So how do I get this working from my bot? Why is it not able to parse by links correctly? What am I missing?

As far as I can see from the docs, this should work. Here in the section on formatting messages it says:

The RTM API only supports posting simple messages formatted using our default message formatting mode.

And links to here which does mention links with the pipe character, so I think it should work.

(note MessageToSlack is just an ordinary .NET class with type, channel and text properties that gets serialized by JSON.Net and appears to give the correct JSON. ws is my websocket connection from the WebSocketSharp nuget package)

JSON:

{
    "id": 1,
    "type": "message",
    "channel": "C6QRKT0EA",
    "text": "<http://google.com|to google>"
}

Edit: So it seems if I switch from replying with the web socket connection and instead post to https://slack.com/api/chat.postMessage, it will work correctly, but it's a bit more fiddly to use and the documentation led me to believe that the links should work without needing to jump through that particular hoop. Am I just misreading the docs? Or are docs just not very clear on this point?

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • A guess would be to use `>` and `<` instead of < and >? – default Dec 11 '17 at 16:18
  • 1
    @Default: I did try that. Same result. – Matt Burland Dec 11 '17 at 16:20
  • 1
    I would also guess its some unsuspected character encoding. Please post the actual JSON string that is send to Slack. – Erik Kalkoken Dec 11 '17 at 16:22
  • @ErikKalkoken: Added JSON. It looks fine to me? – Matt Burland Dec 11 '17 at 16:29
  • 1
    It's not supported: https://github.com/slackhq/slack-api-docs/issues/32 (there are more similar issues in other slack libs) – Evk Dec 11 '17 at 19:17
  • Note that docs for basic formatting mode does not claim such urls are supported. It only claims regular urls are detected and (in some cases) may be send to client as piped version (such as when detected url does not contain http:// prefix) – Evk Dec 11 '17 at 19:21
  • Since its not supported you may want to consider switching to [Events API](https://api.slack.com/events-api). With that you have the full message scope including attachment and markup. – Erik Kalkoken Dec 12 '17 at 00:02

1 Answers1

1

Try to enable markdown support by adding "mrkdwn": true to your json

{
    "type": "message",
    "channel": "C6QRKT0EA",
    "text": "<http://google.com|to google>",
    "mrkdwn": true
}

Read Message Formatting section. Hope it will help.

ryche
  • 2,004
  • 2
  • 18
  • 27