slack-go / slack

Slack API in Go, originally by @nlopes; Maintainers needed, contact @parsley42
https://pkg.go.dev/github.com/slack-go/slack
BSD 2-Clause "Simplified" License
4.69k stars 1.14k forks source link

Cannot use emoji with mrkdwn section #881

Open JoseFMP opened 3 years ago

JoseFMP commented 3 years ago

I try to send a hello world message using Slack go API.


const token = "my token in here!"

func main(){
  api := slack.New(token)

  chanelId := `do some stuff here to find the channel ID, means the API client is kind of working as I can list the channels`

  textBlockObject := slack.NewTextBlockObject("mrkdwn", "hello world!", true, false)
  sectionBlock := slack.NewSectionBlock(textBlockObject, nil, nil)

  blocks := []slack.Block{sectionBlock}
  blockOptions := slack.MsgOptionBlocks(blocks...)
  _, _, errorSending := api.PostMessage(channelID, blockOptions)
}

And in Slack response, (HTTP 200) there is an upper layer error message there is just this: invalid_blocks and obviously the message is not posted.

Since the message I am sending is so extremely simple, what could be wrong!?

Versions

JoseFMP commented 3 years ago

Seems is related to the mrkdwn type. If I set it to plain_text it works just fine. What is wrong? I can see in the Slack message builder, plenty of blocks text objects have the type mrkdn. Any ideas?

JoseFMP commented 3 years ago

I got to work around this issue by using mark down formatting and the option false to disable emojis. So this is really counter-intuitive and the error message from Slack API could be improved. Not even that, but the client itself could check this before sending the HTTP request.

So using emojis + mrkdown gives an error from backend.

kanata2 commented 3 years ago

Thanks for reporting. It seems that Slack does not allow emoji property in section block now.

https://api.slack.com/reference/block-kit/blocks#section

Enviy commented 3 years ago

In this example Slack does appear to support the use of emoji with mrkdown in section type blocks. However I still encounter errors when attempting to use mrkdown. Using mrkdown returns this error while replacing it with plain_text successfully uses the formatted string and uses emoji.

example block kit builder link

kanata2 commented 3 years ago

Indicates whether emojis in a text field should be escaped into the colon emoji format. This field is only usable when type is plain_text.

https://api.slack.com/reference/block-kit/composition-objects#text

kanata2 commented 3 years ago

FYI: Please use (TextBlockObject).Validate() for avoiding this error before sending a request.

See also: #895

orfeasa commented 2 years ago

If you are using mrkdown then I believe emoji has to be set to false. Emoji still get resolved though

lily-es commented 2 years ago

I can confirm that the mrkdwn type does not support the emoji property. Attempting to set emoji to either false or true will result in this response from the API. This is not an issue with the go client, but the block schema.

Response: {
"ok": false,
"error": "invalid_blocks",
"errors": [
   "failed to match all allowed schemas [json-pointer:/blocks/2/text]",
   "invalid additional property: emoji [json-pointer:/blocks/2/text]
]
}

You can verify this with the slack block kit builder app, too.

shzxcv commented 2 years ago

This may not be what you want to achieve with this issue, but I am sending emoji in markdown type with this.

text := ":bell: *Notification*"
block := slack.MsgOptionBlocks(
    &slack.SectionBlock{
        Type: slack.MBTSection,
        Text: &slack.TextBlockObject{Type: "mrkdwn", Text: text},
    },
)
return &block

https://www.webfx.com/tools/emoji-cheat-sheet/

JamesCHub commented 7 months ago

Not sure if there have been changes to supported emojis, or just the names, but as of 2024:

  1. I was not able to get simplistic emoji strings like :) and :( converted by Slack through the API, using plain_text or mrkdown
  2. Use of utf hex strings also did not get converted
  3. Use of the emoji:true argument resulted in no response at all, as indicated by @lily-es (plain_text or mrkdown) - but then, that's supposed to be the default anyway, so kind of confusing
  4. a number of what I thought were standard colon strings (e.g. ":grimace:") no longer worked

But thanks to the comment from @shzxcv, I discovered that ":bell:" did work with type set as mrkdown, so I assumed I must just have wrong or outdated colon strings.

In any event, I'm sure I'm just missing a link to the current set of colon emoticon strings, but in case it helps someone out, here's a happiness spectrum of supported colon strings: