🕌

Block Kit Kotlin DSLでSlackメッセージにattachmentsを追加する

2022/12/30に公開

Block Kit Kotlin DSLでSlackメッセージを組み立てるときattachmentsの追加の仕方がわからなかったのでメモ。

import com.slack.api.Slack
import com.slack.api.methods.kotlin_extension.request.chat.blocks
import com.slack.api.model.Attachments.*
import com.slack.api.model.block.Blocks.*
import com.slack.api.model.kotlin_extension.block.withBlocks


val slack = Slack.getInstance()
slack.methods("xxxx")
    .chatPostMessage { req ->
        req.channel("xxx")
            .blocks {
                section {
                    markdownText("aaa")
                }
                divider()
                section {
                    fields {
                        markdownText("bbb")
                        markdownText("ccc")
                    }
                }
            }
            .attachments(
                asAttachments(
                    attachment { a ->
                        a.color("#FF00FF")
                            .blocks(
                                withBlocks {
                                    section {
                                        fields {
                                            markdownText("ddd")
                                            markdownText("fff")
                                        }
                                    }
                                }
                            )
                    }
                )
            )
    }

Discussion