我可以使用 Json 构建模板并将其集成到我的应用程序中以使 Slackbot 更美观吗?

问题描述 投票:0回答:1

当前模板如下所示:

enter image description here

这是用于构建此“发送秘密消息”的代码:


import (
    "fmt"
    "strings"

    "github.com/google/uuid"
    "github.com/pkg/errors"
    "github.com/slack-go/slack"
)

// SendMessage sends a message to the receiver through slack
func SendMessage(id uuid.UUID, sender string, receiver string, description string) error {
    username := strings.Split(receiver, "@")[0]
    _, _, err := rtm.PostMessage(
        "@"+username,
        slack.MsgOptionText(fmt.Sprintf(
            "*%s has shared a secret with you!* \n*Description:* %s "+
                "\nView the secret at https://whisper.sky/%s",
            sender,
            description,
            id), false),
        slack.MsgOptionAsUser(true),
    )

    if err != nil {
        return errors.Wrap(err, "sending message through slack")
    }

    return nil
}

块引用

但是,在 slack Block Kit Builder 上,他们使用 Json,所以我的问题是,我可以按照我想要的方式在 Json 中编辑模板并将 Json 导入到我当前的代码中,这样它看起来更像这样:

enter image description here

任何帮助将不胜感激,因为我是这个背景的新手,所以不确定要更改什么以及在哪里更改。谢谢。

这是来自区块生成器的代码:

{
    "blocks": [
        {
            "type": "header",
            "text": {
                "type": "plain_text",
                "text": "{name} has sent you a secret!",
                "emoji": true
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*Type:*\nPassword"
                },
                {
                    "type": "mrkdwn",
                    "text": "*For:*\nGitHub Service Account"
                },
                {
                    "type": "mrkdwn",
                    "text": "*Message:*\nHere's the password to the GitHub Service Account"
                },
                {
                    "type": "mrkdwn",
                    "text": "*Expires:*\nIn 7 Days"
                }
            ]
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "*To access this secret visit http://whisper.int.discoott.sky.com/ *"
            },
            "accessory": {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "Click Here",
                    "emoji": true
                },
                "value": "access_secret_button",
                "url": "https://whisper.int.discoott.sky.com/",
                "action_id": "button-action"
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "context",
            "elements": [
                {
                    "type": "mrkdwn",
                    "text": "If you did not expect this secret, you can ignore it and allow it to expire."
                }
            ]
        }
    ]
}

我只是不知道如何将该代码实现到我的 Go 文件中。

json go formatting slack slack-block-kit
1个回答
0
投票

您可以引入任何块来获取所需的 JSON。

// AnyBlock represents a generic structure for a Slack message block.
// It is designed to be versatile enough to represent any type of block supported by Slack.
// The fields within AnyBlock correspond to various properties that a Slack block can have,
// making it a flexible structure to construct a wide range of message layouts.
type AnyBlock struct {
    Accessory any                    `json:"accessory,omitempty"` // Accessory element (like buttons or images) for blocks like section.
    Elements  []any                  `json:"elements,omitempty"`  // Elements of the block, used for constructing complex block types like rich_text.
    Emoji     bool                   `json:"emoji,omitempty"`     // Flag to indicate if emojis should be interpreted in text objects.
    Fields    []any                  `json:"fields,omitempty"`    // Fields are used in section blocks to display text in a columnar fashion.
    Name      any                    `json:"name,omitempty"`      // Name of the element, used in elements like emoji.
    Style     any                    `json:"style,omitempty"`     // Style properties for elements, like style of text or buttons.
    Text      any                    `json:"text,omitempty"`      // Text object, can be a plain text or markdown text.
    Type      slack.MessageBlockType `json:"type"`                // Type of the block, defines how the block should be rendered.
}

// BlockType returns the type of the block.
// This method is necessary to satisfy the slack.Block interface,
// allowing AnyBlock to be used interchangeably with other Slack block types.
func (b *AnyBlock) BlockType() slack.MessageBlockType {
    return b.Type
}

现在您可以使用 AnyBlock 来表示 Block Kit Builder 中的任何 JSON 元素

                        {
                            "type": "rich_text_section",
                            "elements": [
                                {
                                    "type": "text",
                                    "text": "item 3: "
                                },
                                {
                                    "type": "link",
                                    "url": "https://slack.com/",
                                    "text": "with a link",
                                    "style": {
                                        "bold": true
                                    }
                                }
                            ]
                        },

AnyBlock 的代码示例

    var blocks []slack.Block

    // Header Block
    headerText := &AnyBlock{Type: "plain_text", Text: "Header 🔔", Emoji: true}
    headerBlock := &AnyBlock{Type: slack.MBTHeader, Text: headerText}
    blocks = append(blocks, headerBlock)

    // Rich Text Block
    richTextElements := make([]any, 0)

    // Adding a section with text
    richTextElements = append(richTextElements, &AnyBlock{
        Type: "rich_text_section",
        Elements: []any{
            &AnyBlock{Type: "text", Text: "Basic bullet list with rich elements"},
        },
    })

    listItems := []any{}
    // Iterating over each channel and alert
    for _, data := range someData {
        for _, := range data {
            listItems = append(listItems, &AnyBlock{
                Type: "rich_text_section",
                Elements: []any{
                    &AnyBlock{Type: "text", Text: myText, Style: map[string]any{"bold": true}},
                    &AnyBlock{Type: "text", Text: fmt.Sprintf(": %d counter(s) (", myCounter1)},
                    &AnyBlock{Type: "text", Text: "number", Style: map[string]any{"italic": true}},
                    &AnyBlock{Type: "text", Text: fmt.Sprintf(": %d)", myCounter2)},
                },
            },
            )
        }
    }
    richTextElements = append(richTextElements, &AnyBlock{
        Type:     "rich_text_list",
        Style:    "bullet",
        Elements: listItems,
    })

等等...

我希望你有主意。

© www.soinside.com 2019 - 2024. All rights reserved.