如何在 Azure Logic Apps 的 Slack Webhook 中显示数组?

How to display array in Slack Webhook of Azure Logic Apps?

我有 LogicApps。它在 HTTP 请求中接收 属性 ListOfNames 数组。

我很难在 Slack webhook 中正确显示。如何格式化正文?

Slack 中显示的当前消息: 名单: ["Name1\n","Name2\n","Name3\n"]

slack 中的目标消息: 名单: 姓名1 名字2 名字3

我试过拆分:

代码:没有错误,但未排列消息中正确列出的项目:

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "@split(string(triggerBody()?['ListOfTesNames']),'\n') ",
                "runAfter": {},
                "type": "Compose"
            },
            "HTTP_Webhook": {
                "inputs": {
                    "subscribe": {
                        "body": {
                            "blocks": [
                                {
                                    "text": {
                                        "text": "*[dev] Testing:* \n ListOfTestNames @{outputs('Compose')}   ",
                                        "type": "mrkdwn"
                                    },
                                    "type": "section"
                                },
                                {
                                    "text": {
                                        "text": "Please check test results from database ",
                                        "type": "mrkdwn"
                                    },
                                    "type": "section"
                                }
                            ]
                        },
                        "method": "POST",
                        "uri": "https://hooks.slack.com/services/1111111111111111111111"
                    },
                    "unsubscribe": {}
                },
                "runAfter": {
                    "Compose": [
                        "Succeeded"
                    ]
                },
                "runtimeConfiguration": {
                    "staticResult": {
                        "name": "HTTP_Webhook0",
                        "staticResultOptions": "Disabled"
                    }
                },
                "type": "HttpWebhook"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "staticResults": {
            "HTTP_Webhook0": {
                "outputs": {
                    "headers": {},
                    "statusCode": "OK"
                },
                "status": "Succeeded"
            }
        },
        "triggers": {
            "manual": {
                "inputs": {
                    "method": "POST",
                    "schema": {
                        "properties": {
                            "ListOfTestNames": {
                                "type": "array"
                            }
                        },
                        "type": "object"
                    }
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

您可以在 webhook 连接器之前添加一个组合连接器,并使用提供正确格式的拆分功能,然后将 compose 连接器的输出添加到您的 webhook 连接器。

这里有截图供大家参考:

注意: 确保将来自 compose 连接器的拆分中的表达式修改为 split(triggerBody()?['ListOfFiles'],'\ n') 到 split(triggerBody()?['ListOfFiles'],'\n') 因为当我们为此编写表达式时,它首先将 '\n' 作为字符串然后添加另一个 ''.

下面是compose连接器中的拆分表达式:

split(triggerBody()?['ListOfFiles'],'

')

更新答案

当我们检索数组并将其传递给文本正文时,它正在向其中添加额外的字符。我们创建了一个新的逻辑应用程序,并将我们正在检索的每个项目添加到一个变量中,最后将该变量添加到 Webhook 主体中。

这是逻辑应用程序的屏幕截图

这是我的代码视图

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "For_each_2": {
                        "actions": {
                            "Append_to_string_variable": {
                                "inputs": {
                                    "name": "ListOfFilesFinal",
                                    "value": "@{items('For_each_2')},"
                                },
                                "runAfter": {},
                                "type": "AppendToStringVariable"
                            }
                        },
                        "foreach": "@split(item(),',')",
                        "runAfter": {},
                        "type": "Foreach"
                    }
                },
                "foreach": "@triggerBody()?['ListOfFiles']",
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "HTTP_Webhook": {
                "inputs": {
                    "subscribe": {
                        "body": {
                            "blocks": [
                                {
                                    "text": {
                                        "text": "*[dev] Testing: * \n ListOfTestNames :  @{variables('ListOfFilesFinal')} ",
                                        "type": "mrkdwn"
                                    },
                                    "type": "section"
                                },
                                {
                                    "text": {
                                        "text": "Please check test results from database ",
                                        "type": "mrkdwn"
                                    },
                                    "type": "section"
                                }
                            ]
                        },
                        "method": "POST",
                        "uri": "@listCallbackUrl()"
                    },
                    "unsubscribe": {}
                },
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "runtimeConfiguration": {
                    "staticResult": {
                        "name": "HTTP_Webhook0",
                        "staticResultOptions": "Disabled"
                    }
                },
                "type": "HttpWebhook"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "ListOfFilesFinal",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "staticResults": {
            "HTTP_Webhook0": {
                "outputs": {
                    "headers": {},
                    "statusCode": "OK"
                },
                "status": "Succeeded"
            }
        },
        "triggers": {
            "manual": {
                "inputs": {
                    "method": "POST",
                    "schema": {
                        "properties": {
                            "ListOfFiles": {
                                "type": "array"
                            }
                        },
                        "type": "object"
                    }
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}