问题
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):

This is how the bot response looks like:

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("&", "&");
json.Replace("<", "<");
json.Replace(">", ">");
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?
回答1:
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"
}
}]
}
来源:https://stackoverflow.com/questions/57132115/slack-bot-doesnt-interpret-json-message