slack-go / slack

Slack API in Go, originally by @nlopes; Maintainers needed, contact @parsley42

Home Page:https://pkg.go.dev/github.com/slack-go/slack

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Adding blocks in client.PostMessage()

nikko00 opened this issue · comments

Brief intro

I am trying to use blocks in a slack message <client.PostMessage()>. But I am unable to figure out how it is supposed to be done. Every time i get a "invalid arguments" error.

Sources/articles referred

I am referencing these sources
[git-repo blocks example](https://github.com/slack-go/slack/blob/master/examples/blocks/blocks.go
an article about how to use blocks
another article about using blocks

Sample code - how i am approaching this

Here is the sample code i used for adding blocks
`// func to build msg body & post it on slack

func SendCustomMshToChannel(msgToSend string){

	api := slack.New(os.Getenv("SLACK_BOT_TOKEN"))
	
	// attachement for msg
	attachment := slack.Attachment{
		Text:    msgToSend,
		CallbackID: "OpenModal",
		Color: "blue",

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


		Actions: []slack.AttachmentAction{
			// code for few buttons here
		},



	}

	// post msg to slack
	channelID, timestamp, errPostMsg := api.PostMessage(
		os.Getenv("SLACK_APP_testChannel_TOKEN"),
		slack.MsgOptionAttachments(attachment),
		slack.MsgOptionAsUser(true),
	)

	// check for error
	if errPostMsg != nil {
		fmt.Printf("%s\n", errPostMsg)
		return
	}
	fmt.Printf("\nNotification successfully sent to channel %s at %s", channelID, timestamp)
}

`

`// func where i build the blocks

 func exampleOne() []slack.Block {

	// Header Section
	headerText := slack.NewTextBlockObject("mrkdwn", "You have a new request:\n*<fakeLink.toEmployeeProfile.com|Fred Enriquez - New device request>*", 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

}

`

P.S.

I also tried using json data, and unmarshalling it. But i am unable to get "blocks" which i can pass to <SendCustomMshToChannel()> function.

What's the mistake in this approach. How should it be done correctly. Any suggestions would be greatly welcomed :)

I'm not certain, but I think Attachment might be producing two levels of blocks fields in the JSON data. One created by this field and another by the BlockSet field within that.