Azure-quickstart-templates: 201-alert-to-slack-with-logic-app

Created on 13 Apr 2017  路  15Comments  路  Source: Azure/azure-quickstart-templates

The 201-alert-to-slack-with-logic-app template appears to be not working ( issue #2514 seems to be the original report of this issue but is closed for some reason )

The problem

After deploying the template and configuring an alert to use the web hook, it fails with:

InvalidTemplate. Unable to process template language expressions in action 'Post_Message' inputs at line '1' and column '1412': 'The template language expression 'triggerBody()['context']['name']' cannot be evaluated because property 'context' doesn't exist, available properties are 'schemaId, data'. Please see https://aka.ms/logicexpressions for usage details.'.

As noted in #2514 I was eventually able to make it work by updating the schema to match what the azure alert is actually sending (it seems like "status", "context", and "properties" are actually inside a "data" node, rather than top level as documented in Configure a webhook on an Azure metric alert)

Most helpful comment

After spending a while untangling this information, here's a quick recap:

There are two implementations of webhooks. Azure classic alerts webhooks (https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/insights-webhooks-alerts) and the new log alerts webhooks (https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/monitor-alerts-unified-log-webhook).

This samples works only for the _classic_ alerts. So whenever you want to use the new alert webhooks, you'll have a problem. Look at the provided link for an example JSON, that you can copy and paste in the use sample payload to generate schema option like @SurferL suggested.

So to the the maintainers: if you just let us know that this is for _classic_ alerts, you'll save a lot of people time :)

So the things to change, in addition to the schema, are (looking at the code view for simplicity, note that the exact ):

  • @{triggerBody()['context']['portalLink']} becomes @{triggerBody()['data']['LinkToSearchResults']}
  • Azure Alert - '@{triggerBody()['context']['name']}' @{triggerBody()['status']} on '@{triggerBody()['context']['resourceName']}'. Details: @{body('Http')['id']} becomes something like Azure Alert - '@{triggerBody()['data']['AlertRuleName']}. Details: @{body('Http')['id']} (you can fiddle around with the available field, I did not see a "status" field in the new format)

All 15 comments

Hi @bjornicus
I'm having the same issue. Did you manage to resolve it ?
I've found on the net that triggerBody() should be cast as json, but that didn't help
"longUrl": "@{json(triggerBody())['context']['portalLink']}"

Now I'm getting an error

InvalidTemplate. Unable to process template language expressions in action 'Http' inputs at line '1' and column '2711': 'The template language function 'json' expects its parameter to be a string or an XML. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#json for usage details.'.

yes, I did get it working. See my comment in #2514 which contains the schema I used.

@bjornicus I'm also having the same issue, but I'm afraid your fix didn't work :(

When I posted your input into the use sample payload to generate schema it generated:

{
    "schema": {
        "properties": {
            "data": {
                "properties": {
                    "context": {
                        "properties": {
                            "activityLog": {
                                "properties": {
                                    "authorization": {
                                        "properties": {
                                            "action": {
                                                "type": "string"
                                            },
                                            "scope": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "caller": {
                                        "type": "string"
                                    },
                                    "channels": {
                                        "type": "string"
                                    },
                                    "claims": {
                                        "type": "string"
                                    },
                                    "correlationId": {
                                        "type": "string"
                                    },
                                    "description": {
                                        "type": "string"
                                    },
                                    "eventDataId": {
                                        "type": "string"
                                    },
                                    "eventSource": {
                                        "type": "string"
                                    },
                                    "eventTimestamp": {
                                        "type": "string"
                                    },
                                    "level": {
                                        "type": "string"
                                    },
                                    "operationId": {
                                        "type": "string"
                                    },
                                    "operationName": {
                                        "type": "string"
                                    },
                                    "resourceGroupName": {
                                        "type": "string"
                                    },
                                    "resourceId": {
                                        "type": "string"
                                    },
                                    "resourceProviderName": {
                                        "type": "string"
                                    },
                                    "resourceType": {
                                        "type": "string"
                                    },
                                    "status": {
                                        "type": "string"
                                    },
                                    "subStatus": {
                                        "type": "string"
                                    },
                                    "submissionTimestamp": {
                                        "type": "string"
                                    },
                                    "subscriptionId": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            }
                        },
                        "type": "object"
                    },
                    "properties": {
                        "properties": {},
                        "type": "object"
                    },
                    "status": {
                        "type": "string"
                    }
                },
                "type": "object"
            },
            "schemaId": {
                "type": "string"
            }
        },
        "type": "object"
    }
}

The Output connection of the first When a HTTP request is received says Connection: close?

And I still get the error at the Http step in the Logic App:

InvalidTemplate. Unable to process template language expressions in action 'Http' inputs at line '1' and column '1761': 'The template language expression 'triggerBody()['context']['portalLink']' cannot be evaluated because property 'context' cannot be selected. Please see https://aka.ms/logicexpressions for usage details.'.

Am I missing something or is there another way of fixing it?

@architechcro did you fix it using bjornicus' method?

@SurferL sorry to hear it didn't work for you, it's possible things have changed since I posted my fix, although I'm still getting alerts delivered to slack so there's a good chance it should still work. You might try diffing your generated schema with the one I posted in the other issue and see if anything jumps out at you.

@SurferL , try to change triggerBody() to triggerOutputs()?['body']

After spending a while untangling this information, here's a quick recap:

There are two implementations of webhooks. Azure classic alerts webhooks (https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/insights-webhooks-alerts) and the new log alerts webhooks (https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/monitor-alerts-unified-log-webhook).

This samples works only for the _classic_ alerts. So whenever you want to use the new alert webhooks, you'll have a problem. Look at the provided link for an example JSON, that you can copy and paste in the use sample payload to generate schema option like @SurferL suggested.

So to the the maintainers: if you just let us know that this is for _classic_ alerts, you'll save a lot of people time :)

So the things to change, in addition to the schema, are (looking at the code view for simplicity, note that the exact ):

  • @{triggerBody()['context']['portalLink']} becomes @{triggerBody()['data']['LinkToSearchResults']}
  • Azure Alert - '@{triggerBody()['context']['name']}' @{triggerBody()['status']} on '@{triggerBody()['context']['resourceName']}'. Details: @{body('Http')['id']} becomes something like Azure Alert - '@{triggerBody()['data']['AlertRuleName']}. Details: @{body('Http')['id']} (you can fiddle around with the available field, I did not see a "status" field in the new format)

@christianrondeau @SurferL @bjornicus Can one of you post the full ARM template that you've been able to get this working with? I've tried making the modifications that @christianrondeau has mentioned and have not been able to get it working as of yet

@jdogg89 make sure you know whether the webhook you're using is a classic webhook or a log alert webhook. Then, click on When a HTTP request is received and you'll see Use a sample payload to generate schema. This is where I generated my schema. Here's the said schema for me:

{
    "properties": {
        "data": {
            "properties": {
                "AlertRuleName": {
                    "type": "string"
                },
                "AlertThresholdOperator": {
                    "type": "string"
                },
                "AlertThresholdValue": {
                    "type": "integer"
                },
                "ApplicationId": {
                    "type": "string"
                },
                "Description": {
                    "type": "string"
                },
                "LinkToSearchResults": {
                    "type": "string"
                },
                "ResultCount": {
                    "type": "integer"
                },
                "SearchIntervalEndtimeUtc": {
                    "type": "string"
                },
                "SearchIntervalInSeconds": {
                    "type": "integer"
                },
                "SearchIntervalStartTimeUtc": {
                    "type": "string"
                },
                "SearchQuery": {
                    "type": "string"
                },
                "SearchResult": {
                    "properties": {
                        "tables": {
                            "items": {
                                "properties": {
                                    "columns": {
                                        "items": {
                                            "properties": {
                                                "name": {
                                                    "type": "string"
                                                },
                                                "type": {
                                                    "type": "string"
                                                }
                                            },
                                            "required": [
                                                "name",
                                                "type"
                                            ],
                                            "type": "object"
                                        },
                                        "type": "array"
                                    },
                                    "name": {
                                        "type": "string"
                                    },
                                    "rows": {
                                        "items": {
                                            "type": "array"
                                        },
                                        "type": "array"
                                    }
                                },
                                "required": [
                                    "name",
                                    "columns",
                                    "rows"
                                ],
                                "type": "object"
                            },
                            "type": "array"
                        }
                    },
                    "type": "object"
                },
                "Severity": {
                    "type": "string"
                },
                "SubscriptionId": {
                    "type": "string"
                }
            },
            "type": "object"
        },
        "schemaId": {
            "type": "string"
        }
    },
    "type": "object"
}

As to why you couldn't make it work, if my suggestions don't help I'd suggest to go step by step. Program the webhook to something like https://webhook.site (never used them, it's just an example), then ensure the payload gets through in Logic App Designer. Check in the Overview tab of your Logic App for failed calls and you should find more details there. If you have a more precise question then, I'll try to help!

I'm doing a log alert webhook, specifically on when someone views storage account keys. Getting the following error:

InvalidTemplate. Unable to process template language expressions in action 'Http' inputs at line '1' and column '1832': 'The template language expression 'triggerBody()['data']['LinkToSearchResults']' cannot be evaluated because property 'data' cannot be selected. Please see https://aka.ms/logicexpressions for usage details.'.

I'll try your schema you just posted in the morning and see where I get, and thanks for the help.

@christianrondeau in you schema section, are you leaving in the variable $schema, as shown here:

"triggers": {
            "manual": {
              "type": "request",
              "kind": "Http",
              "inputs": {
                "schema": {
                  "$schema": "http://json-schema.org/draft-04/schema#",
                    "properties": {

@christianrondeau can you please share your full azuredeploy.json file so I can diff it to mine? (less any subscription ID's or other personal identifiable info) Here is the file, still getting the error above:

{
    "$connections": {
        "value": {
            "slack": {
                "connectionId": "/subscriptions/ID/resourceGroups/RGName/providers/Microsoft.Web/connections/slackConnection",
                "connectionName": "slackConnection",
                "id": "/subscriptions/ID/providers/Microsoft.Web/locations/eastus/managedApis/slack"
            }
        }
    },
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Http": {
                "inputs": {
                    "body": {
                        "longUrl": "@{triggerBody()['data']['LinkToSearchResults']}"
                    },
                    "headers": {
                        "Content-Type": "application/json"
                    },
                    "method": "POST",
                    "uri": "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyBkT1BRbA-uULHz8HMUAi0ywJtpNLXHShI"
                },
                "runAfter": {},
                "type": "Http"
            },
            "Post_Message": {
                "inputs": {
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['slack']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/chat.postMessage",
                    "queries": {
                        "channel": "#general",
                        "text": "Azure Alert - '@{triggerBody()['data']['AlertRuleName']}'. Details: @{body('Http')['id']}"
                    }
                },
                "runAfter": {
                    "Http": [
                        "Succeeded"
                    ]
                },
                "type": "ApiConnection"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {
                        "properties": {
                            "data": {
                                "properties": {
                                    "AlertRuleName": {
                                        "type": "string"
                                    },
                                    "AlertThresholdOperator": {
                                        "type": "string"
                                    },
                                    "AlertThresholdValue": {
                                        "type": "integer"
                                    },
                                    "ApplicationId": {
                                        "type": "string"
                                    },
                                    "Description": {
                                        "type": "string"
                                    },
                                    "LinkToSearchResults": {
                                        "type": "string"
                                    },
                                    "ResultCount": {
                                        "type": "integer"
                                    },
                                    "SearchIntervalEndtimeUtc": {
                                        "type": "string"
                                    },
                                    "SearchIntervalInSeconds": {
                                        "type": "integer"
                                    },
                                    "SearchIntervalStartTimeUtc": {
                                        "type": "string"
                                    },
                                    "SearchQuery": {
                                        "type": "string"
                                    },
                                    "SearchResult": {
                                        "properties": {
                                            "tables": {
                                                "items": {
                                                    "properties": {
                                                        "columns": {
                                                            "items": {
                                                                "properties": {
                                                                    "name": {
                                                                        "type": "string"
                                                                    },
                                                                    "type": {
                                                                        "type": "string"
                                                                    }
                                                                },
                                                                "required": [
                                                                    "name",
                                                                    "type"
                                                                ],
                                                                "type": "object"
                                                            },
                                                            "type": "array"
                                                        },
                                                        "name": {
                                                            "type": "string"
                                                        },
                                                        "rows": {
                                                            "items": {
                                                                "type": "array"
                                                            },
                                                            "type": "array"
                                                        }
                                                    },
                                                    "required": [
                                                        "name",
                                                        "columns",
                                                        "rows"
                                                    ],
                                                    "type": "object"
                                                },
                                                "type": "array"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "Severity": {
                                        "type": "string"
                                    },
                                    "SubscriptionId": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            },
                            "schemaId": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    }
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    }
}

Was able to make this work finally. my key was adding "activity log" to my schema. @{triggerBody()['data']?['context']['activityLog']['resourceProviderName']}. If anyone else has difficulty I can try assist.

Hey @jdogg89, glad you could figure it out (sorry I wasn't available to help before). Weird you had to manually customize the schema!

@justinhauer hello. Could you please help me on this? With so many different schemas and attempts I wasted a lot of time already to try to make this work, and documentation doesn't help :(

Was able to make this work finally. my key was adding "activity log" to my schema. @{triggerBody()['data']?['context']['activityLog']['resourceProviderName']}. If anyone else has difficulty I can try assist.

Hi @justinhauer are you able share your full azuredeploy.json
if possible please?

Was this page helpful?
0 / 5 - 0 ratings