Inumedia / SlackAPI

.NET Implementation of the Slack team communication platform API.
MIT License
452 stars 243 forks source link

Blocks #245

Open legoboyvdlp opened 4 years ago

legoboyvdlp commented 4 years ago

Hi,

I am having trouble sending a block message to slack. I wonder if someone could advise me what I am doing wrong? I can send ordinary messages just fine, but its these blocks that are giving problems. I was unable to find any example or documentation for this, but this, I understood should have worked...

var length = List.Count;
var blockArr = new SectionBlock[1];
                blockArr[0] = new SectionBlock();
                blockArr[0].fields = new Text[length];
                for (var i = 0; i < length; i++)
                {
                    var text = new Text();
                    text.text = // my string,
                    blockArr[0].fields[i] = text;
                }
                await sendMessageAsync("#channel", text: "Test", block: blockArr);
            });

My sendMessageAsync function is as such:

public static async Task<PostMessageResponse> sendMessageAsync(string channel, string text = "", IBlock[] block = null)
        {
            var response = await slackClient.PostMessageAsync(channel, text: text, blocks: block);
            Console.WriteLine(response.error);
            return response;
        }

In each case I get _invalidblocks as an error.

legoboyvdlp commented 4 years ago

Does anyone have any ideas on this -- this is somewhat delaying my project until I can solve this :)

Inumedia commented 4 years ago

Unfortunately, I'm not well-versed with the newer Blocks APIs.

Afaik, we don't do any validation in the library and the messages just get serialized as-is and sent to Slack. It might be worthwhile to check other libraries or the official API for any hints on what's causing this.

It also might be worthwhile to view the raw responses with Fiddler4 or use the library directly and step through to see the raw response from the API to see if it contains any more descriptive error messages.

legoboyvdlp commented 4 years ago

Hi, thank you in any case. I will try and inspect the serialized JSON to see what it shows :)

legoboyvdlp commented 4 years ago

Ok, I got this, using a new script: Block block = new Block(); Text text = new Text(); text.type = TextTypes.PlainText; block.type = BlockTypes.Section; text.text = "Test"; block.text = text; block.title = text; block.block_id = "test"; blocks[i] = block;

channel=#milcom-bot-test&text=:bubblealert: Test&blocks=[{"type":"section","block_id":"test","text":{"type":"plain_text","text":"Test"},"title":{"type":"plain_text","text":"Test"}},{"type":"section","block_id":"test","text":{"type":"plain_text","text":"Test"},"title":{"type":"plain_text","text":"Test"}},{"type":"section","block_id":"test","text":{"type":"plain_text","text":"Test"},"title":{"type":"plain_text","text":"Test"}},{"type":"section","block_id":"test","text":{"type":"plain_text","text":"Test"},"title":{"type":"plain_text","text":"Test"}}]

This is hard to read, but when imported to the slack Block Builder, I see that:

"title" is invalid for a block of type section: (Invalid additional property "title" error) Also, block ID must be unique.

I think there is a bug with the title element on your side, and a bug in the block ID on my side :)

So I will continue debugging, now moving on to try and add fields.