-2

I am trying to get a program I wrote in C# to post to a Slack channel through a Slack App but using formatting suggested here: https://api.slack.com/tools/block-kit-builder

I have this code below which posts to the Slack channel so I know that is working.

    {
        static void Main(string[] args)
        {
            PostWebHookAsync();
            Console.ReadLine();
        }
            static async void PostWebHookAsync()
            {
                using (var httpClient = new HttpClient())
                {
                    using (var request = new HttpRequestMessage(new HttpMethod("POST"), "my-webhook-link"))
                    {         
                    string jsonValue = JsonConvert.SerializeObject(new
                    {
                        type = "section",
                        text = "Some text \n new line \t tab",
                    }
                    );
                    Console.WriteLine(jsonValue);
                    Type valueType = jsonValue.GetType();
                    if (valueType.IsArray)
                    {
                        jsonValue = jsonValue.ToString();
                        Console.WriteLine("Array Found");
                    }                   
                    request.Content = new StringContent(jsonValue, Encoding.UTF8, "application/json");
                    var response = await httpClient.SendAsync(request);
                    Console.WriteLine(response.StatusCode);
                    Console.WriteLine(response.Content);
                    }
                }
            }
        }

Which returns:

{"type":"section","text":"Some text \n new line \t tab"}

Now I want to POST this

    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "This is a mrkdwn section block :ghost: *this is bold*, and ~this is crossed out~, and <https://google.com|this is a link>"
        }
    }

But I am struggling to understand what to change this block of code for

string jsonValue = JsonConvert.SerializeObject(new
                    {
                        type = "section",
                        text = "Some text \n new line \t tab",
                    }
AWH
  • 9
  • 1

1 Answers1

2

You need to do the following, the text property is an object, so just create another anonymous object.

string jsonValue = JsonConvert.SerializeObject(new
                    {
                        type = "section",
                        text = new 
                        {
                            type = "mrkdwn",
                            text = "This is a mrkdwn section block :ghost: *this is bold*, and ~this is crossed out~, and <https://google.com|this is a link>"
                        }
                    }
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
  • Hi Jamie, Thanks for your response. I have tried this and the POST in Slack just says "Array" and none of the text. As you can see I have: ``` if (valueType.IsArray) { jsonValue = jsonValue.ToString(); Console.WriteLine("Array Found"); } ``` But this doesnt find an array – AWH Aug 05 '19 at 16:30
  • Thought I would also add the output: "{"type":"section","text":{"type":"mrkdwn","text":"This is a mrkdwn section block :ghost: *this is bold*, and ~this is crossed out~, and "}}" Seems the same as what I'm trying to post. Do i need to include \n \t where needed? – AWH Aug 05 '19 at 16:43