datalust / seq-app-httprequest

Send events and notifications to an HTTP/REST/Webhook endpoint.
Apache License 2.0
11 stars 3 forks source link

Using 'Repetition' syntax in templated JSON #16

Open mortifactor opened 2 months ago

mortifactor commented 2 months ago

Hi!

I'm trying to use webhook to send alerts to Telegram. So my body should look like this:

{
  "chat_id": "123456",
  "text": "alert content"
}

And I would like to send the value of Source.Results using repetition syntax as I've found in https://github.com/serilog/serilog-expressions?tab=readme-ov-file#repetition

{ {
  chat_id: '123456',
  text: {#each k, v in @p['Source']['Results']}{k} = {v}{#delimit},{#end}
} }

But the template above gives me error System.ArgumentException: Syntax error (line 3, column 8): unexpected#.

At the same time if I use it like this

{ {
  chat_id: '123456',
  text: @p['Source']['Results']
} }

it is ok, but this gives me unformatted array value. And I would like to concat formatted array output like this:

text: concat(@p['Alert']['Title'], '\n', {#each k, v in @p['Source']['Results']}{k} = {v}{#delimit},{#end})

Thanks in advance!

liammclennan commented 2 months ago

Hi @mortifactor ,

I think the array formatted string might be the best you can get here. The docs note that:

Template directives like {#if} only work in the outermost template - they can't be used as expressions nested inside holes or objects. In an expression, Seq's regular if construct can be used.

I believe you are running into that limitation.

mortifactor commented 2 months ago

Ah, I see. My apologies! I guess I'd missed it because I was searching in Serilog docs.

If I understand it's not possible to put concat array string (even unformatted) with other string? E.g. text: concat(@p['Alert']['Title'], @p['Source']['Results'])

liammclennan commented 2 months ago

@mortifactor - that looks ok. The docs say you can't use template directives (#if, #each) but template expressions should be ok. @p should be @Properties.