Slack 机器人不解释 json 消息

我在 slack-azure 集成方面遇到问题。我正在尝试使用斜杠命令构建一个机器人,该命令将请求发送到 azure 函数。函数执行后我想将结果返回给用户。我在函数末尾使用 JSON 和简单的 return 语句。

问题是 Slack 不会解释这个 json,而是将其视为普通字符串并打印原始 json。

我认为 json 写得正确,因为我在 Slack Block Kit Builder 中测试了它并将其发送到我的频道并且它显示正确。

这是来自 Block Kit Builder 的消息的样子(这就是它应该的样子):

http://img4.mukewang.com/64aa1188000189d506520123.jpg

机器人响应如下所示:

http://img4.mukewang.com/64aa120f00014cef14670153.jpg

这是这个 json 字符串


[{"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"}}]

这是我构建 json 有效负载的函数


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;

        }

这是我的“主要”azure 函数的一部分,我在其中调用 FormatResponse 并将其返回到我的 Slack 机器人:


            var responseContent = responseFormatter.FormatResponse(results);


            var response = req.CreateResponse(HttpStatusCode.OK, responseContent, JsonMediaTypeFormatter.DefaultMediaType);

            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            return response;

也许我的回复中缺少一些标头,或者我应该以其他方式发送它?


Smart猫小萌
浏览 132回答 2
2回答

蛊毒传说

我正在使用 aws Lambda 并且遇到了同样的问题,但这与 JSON 的格式无关,这是因为我忘记将标头“content-type”设置为“application/json”。

白板的微信

您的响应缺少该blocks属性,需要该属性来告诉 Slack 您的消息中有布局块。完整消息的 JSON 如下所示:{&nbsp; &nbsp; "blocks": [{&nbsp; &nbsp; &nbsp; &nbsp; "type": "section",&nbsp; &nbsp; &nbsp; &nbsp; "text": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "type": "mrkdwn",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "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"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }]}
打开App,查看更多内容
随时随地看视频慕课网APP