1
votes

I have a problem with slack-azure integration. I am trying to build a bot using slash commands, which sends request to azure function. After the function execution I want to return results to a user. I am using JSON and simple return statement at the end of my function.

The problem is that Slack doesn't interpret this json, but it treats it like the normal string and prints the raw json.

I think that the json in written properly, because I tested it out in the Slack Block Kit Builder and sent it to my channel and it was displayed properly.

This is how message from Block Kit Builder looks like (and that's how it should look like):

expected output

This is how the bot response looks like:

bot response

Here is this json string

[{"type":"section","text":{"type":"mrkdwn","text":"• https://www.nike.com/pl/t/jordan-why-not-buty-do-koszykowki-zer02-6P4dl5/AO6219-100?nst=0&cp=euns_kw_pla!pl!goo!cssgeneric!c!!!305375159198&ds_rl=1252249&gclid=Cj0KCQjwjrvpBRC0ARIsAFrFuV9pv41cqv0h8USkHXpK0yay6pqZGnAklqJukHC-JCi3EGHVQX3MELsaAmmUEALw_wcB&gclsrc=aw.ds\\n"}}]

This is my function to construct the json payload

public JArray FormatResponse(List<string> results)
        {
            var links = ExtractLinksFromResponse(results);

            string textString = string.Empty;
            foreach (var l in links)
            {
                textString += $@"• {l}\n";
            }


            dynamic response = new ExpandoObject();
            response.type = "section";

            dynamic text = new ExpandoObject();
            text.type = "mrkdwn";
            text.text = textString;

            response.text = text;

            string json = JsonConvert.SerializeObject(response);

            json.Replace("&", "&amp;");
            json.Replace("<", "&lt;");
            json.Replace(">", "&gt;");

            var parsedJson = JObject.Parse(json);

            var jsonArray = new JArray();
            jsonArray.Add(parsedJson);

            return jsonArray;
        }

And here is part of my "main" azure function where I call FormatResponse and return it to my Slack bot:

            var responseContent = responseFormatter.FormatResponse(results);

            var response = req.CreateResponse(HttpStatusCode.OK, responseContent, JsonMediaTypeFormatter.DefaultMediaType);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            return response;

Maybe there is some missing header in my response or I should send it in another way?

2
Can you please add the exact JSON string that is not working to your question? (as text, not as screenshot) Thanks! - Erik Kalkoken
Of course! I've already edited my post. - Patrycja
Are you maybe missing the blocks property in your response? Did not find it in your code. the response message should be: {"blocks": YOUR JSON} - Erik Kalkoken
It works! Thanks! So it seems that the Block Kit Builder does not work how it should? - Patrycja
Awesome. Well, the Block Kit Builder only creates the blocks part, but you still need to add meta information to create a full message. Since not every message has layout blocks this property is needed. - Erik Kalkoken

2 Answers

0
votes

Your response is missing the blocks property, which is needed to tell Slack that you have layout blocks in your message.

The JSON for a complete message looks like this:

{
    "blocks": [{
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "• https://www.nike.com/pl/t/jordan-why-not-buty-do-koszykowki-zer02-6P4dl5/AO6219-100?nst=0&cp=euns_kw_pla!pl!goo!cssgeneric!c!!!305375159198&ds_rl=1252249&gclid=Cj0KCQjwjrvpBRC0ARIsAFrFuV9pv41cqv0h8USkHXpK0yay6pqZGnAklqJukHC-JCi3EGHVQX3MELsaAmmUEALw_wcB&gclsrc=aw.ds\\n"
        }
    }]
}
0
votes

I am using aws Lambda and I have had the same problem however it was not about the format of the JSON, it was because I forgot to set the header "content-type" to "application/json".