Slack bot无法解释json消息 [英] Slack bot doesn't interpret json message

查看:125
本文介绍了Slack bot无法解释json消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

问题在于Slack不会解释此json,但会将其像普通字符串一样对待并打印原始json.

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

我认为json编写正确,因为我在Slack Block Kit Builder中对其进行了测试,然后将其发送到我的频道,并且显示正确.

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.

这是来自Block Kit Builder的消息的外观(也就是它的外观):

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:

这是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负载的功能

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;
        }

这是我的主要" azure函数的一部分,在这里我调用FormatResponse并将其返回给我的Slack机器人:

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?

推荐答案

您的响应缺少blocks属性,该属性用于告诉Slack您的消息中有布局块.

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

完整消息的JSON看起来像这样:

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

这篇关于Slack bot无法解释json消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆