在松弛消息正文中使用块

分享于2023年02月22日 go slack-api 问答
【问题标题】:using Blocks in slack message body在松弛消息正文中使用块
【发布时间】:2023-02-13 22:48:21
【问题描述】:

我正在尝试在 go-slack_BlockExamples 的示例的帮助下向我的消息正文添加块。 我总是收到“无效参数”错误,但没有进一步的错误描述,说明出了什么问题。任何建议将不胜感激!

示例代码

func SendCustomMshToChannel(msgToSend string){

    if msgToSend==""{msgToSend="Hello everyone. I have nothing to say"}
    api := slack.New(os.Getenv("SLACK_BOT_TOKEN"))
    attachment := slack.Attachment{
        Pretext: "notification!",
        Text:    msgToSend,
        CallbackID: "OpenModal",
        Color: "blue",

        Blocks: slack.Blocks{
            BlockSet:exampleOne(),
                },
        },


    }

    channelID, timestamp, errPostMsg := api.PostMessage(
        os.Getenv("SLACK_APP_testChannel_TOKEN"),
        slack.MsgOptionText("notification!", false),
        slack.MsgOptionAttachments(attachment),
        slack.MsgOptionAsUser(true),
    )
    if errPostMsg != nil {
        fmt.Printf("%s\n", errPostMsg)
        return
    }
    fmt.Printf("\nNotification successfully sent to channel %s at %s", channelID, timestamp)
}

func exampleOne() []slack.Block {

    // Header Section
    headerText := slack.NewTextBlockObject("mrkdwn", "You have a new request:\n**", false, false)
    headerSection := slack.NewSectionBlock(headerText, nil, nil)

    // Fields
    typeField := slack.NewTextBlockObject("mrkdwn", "*Type:*\nComputer (laptop)", false, false)
    whenField := slack.NewTextBlockObject("mrkdwn", "*When:*\nSubmitted Aut 10", false, false)
    lastUpdateField := slack.NewTextBlockObject("mrkdwn", "*Last Update:*\nMar 10, 2015 (3 years, 5 months)", false, false)
    reasonField := slack.NewTextBlockObject("mrkdwn", "*Reason:*\nAll vowel keys aren't working.", false, false)
    specsField := slack.NewTextBlockObject("mrkdwn", "*Specs:*\n\"Cheetah Pro 15\" - Fast, really fast\"", false, false)

    fieldSlice := make([]*slack.TextBlockObject, 0)
    fieldSlice = append(fieldSlice, typeField)
    fieldSlice = append(fieldSlice, whenField)
    fieldSlice = append(fieldSlice, lastUpdateField)
    fieldSlice = append(fieldSlice, reasonField)
    fieldSlice = append(fieldSlice, specsField)

    fieldsSection := slack.NewSectionBlock(nil, fieldSlice, nil)

    // Approve and Deny Buttons
    approveBtnTxt := slack.NewTextBlockObject("plain_text", "Approve", false, false)
    approveBtn := slack.NewButtonBlockElement("b1approve", "approve", approveBtnTxt)

    denyBtnTxt := slack.NewTextBlockObject("plain_text", "Deny", false, false)
    denyBtn := slack.NewButtonBlockElement("b2deny", "deny", denyBtnTxt)

    actionBlock := slack.NewActionBlock("testBlock", approveBtn, denyBtn)


    blocksSlice := []slack.Block{
        headerSection,
        fieldsSection,
        actionBlock,
    }

    return blocksSlice

}

笔记 : 在上面给出的链接中给出的示例中,块只是转换为 json 并打印在控制台上。除此之外,我正在尝试在真实的邮件正文中使用它们。


【解决方案1】:

SendCustomMshToChannel 函数中存在无效大括号,应按照以下链接中的程序进行更改。

https://goplay.tools/snippet/17AiVsncGhI

【讨论】: