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

`rich_text` Block is not available when post messages using Web API #1087

Closed akuzia closed 2 years ago

akuzia commented 2 years ago

What happened

Simple RichTextBlock is valid in block kit, but not valid in api. Returned error has no useful metadata.

Expected behavior

RichText is successfuly sent, nil error returned

Steps to reproduce

reproducible code

package main

import (
    "fmt"

    "github.com/slack-go/slack"
)

func main() {
    api := slack.New("xoxb-...")
    block := slack.NewRichTextBlock("",
        slack.NewRichTextSection(
            slack.NewRichTextSectionTextElement("test message", nil),
        ),
    )
    if _, _, err := api.PostMessage(
        "test",
        slack.MsgOptionBlocks(block),
    ); err != nil {
        err := err.(slack.SlackErrorResponse)
        fmt.Printf("slack: %s: %v\n", err.Error(), err.ResponseMetadata)
    }
}

manifest.yaml

Versions

kanata2 commented 2 years ago

I can't reproduce. Could you give me some more details?

akuzia commented 2 years ago

I've updated the lib, enabled debugging and still getting the same error with code below:

package main

import (
    "encoding/json"
    "fmt"

    "github.com/slack-go/slack"
)

func main() {
    api := slack.New(
        "xoxb-...",
        slack.OptionDebug(true),
    )
    block := slack.NewRichTextBlock("",
        slack.NewRichTextSection(
            slack.NewRichTextSectionTextElement("test message", nil),
        ),
    )

    if blocksJson, err := json.Marshal(block); err == nil {
        fmt.Println(string(blocksJson))
    } else {
        fmt.Println(err)
    }

    if _, _, err := api.PostMessage(
        "test",
        slack.MsgOptionBlocks(block),
    ); err != nil {
        err := err.(slack.SlackErrorResponse)
        fmt.Printf("slack: %s: %v\n", err.Error(), err.ResponseMetadata)
    }
}

go mod for reference:

module github.com/akuzia/first

go 1.18

require github.com/slack-go/slack v0.11.3

require github.com/gorilla/websocket v1.4.2 // indirect

Output:

# go run ./main.go
{"type":"rich_text","elements":[{"type":"rich_text_section","elements":[{"type":"text","text":"test message"}]}]}
slack-go/slack2022/09/12 09:41:17 chat.go:227: Sending request: blocks=%5B%7B%22type%22%3A%22rich_text%22%2C%22elements%22%3A%5B%7B%22type%22%3A%22rich_text_section%22%2C%22elements%22%3A%5B%7B%22type%22%3A%22text%22%2C%22text%22%3A%22test+message%22%7D%5D%7D%5D%7D%5D&channel=test&token=xoxb-...
slack: invalid_blocks: { [] []}
kanata2 commented 2 years ago

Thanks. I was able to reproduce it. By the way, it does not seem to be a library's bug because I get an error when I use Tester. I'll do some more research and report back here if I find anything.

kanata2 commented 2 years ago

@akuzia I inquired Slack support about this and got an answer. These are data structures that can only be used when received as a response from Slack, so we cannot use rich_text or rich_text_section for user posting messages...

p.s. You can use webhook if you want rich_text block.

kanata2 commented 2 years ago

I'm going to close this issue but I'd like to think of some way not to have problems with this confusing specification. Thank you for reporting.