Iterate over the multiple values of a variable

Michail Kotantakis May 23, 2024

Hi team

I receive the body of a wqeb request, in particular from Chatwoot and then i get the content of the message, which is pretty straightforward adn i do not have issues with this.

The content part of the reply is only text. It may be though that someone who sent a message didn't add the attachments as attachments, but put them from a direct copy paste.

The content then is received as text with the attachment urls as well.

Content variable {[content}} made out of  {{webResponse.body.messages.content}}
Logs:
ext text text ![](https://app.chatwoot.com/rails/active_storage/blobs/redirect/e............./image.png) ![](https://app.chatwoot.com/rails/active_storage/blobs/redirect/................./image.png)

so the real text here is ext text text

i make a variable {{contentInlineImages}} which stores the matched image urls from the content: {{content.match("!\\[\\]\\(([^\\)]+)\\)").split(",")}} and this logs
[https://app.chatwoot.com/rails/active_storage/blobs/redirect/......./image.png], [https://app.chatwoot.com/rails/active_storage/blobs/redirect/......./image.png]

Now i want to send an email and use each value of those urls so i can create a link:

{{#contentInlineImages}}
<a href="{{contentInlineImages}}">Attachment</a>
{{/contentInlineImages}}

it doesn't send anything.

If i remove the .split(".") and use the same template for the email it sends only one

What do i miss so i can make it iterate and send all links?

 

Best Regards

2 answers

1 accepted

0 votes
Answer accepted
Michail Kotantakis June 2, 2024

Hi @Bill Sheboy 

Thanks for your suggestion

In fact i always print in the logs everything when i setup an autoamtion to be able to see what is happening in each step.

I still cannot figure out what is ahppening and how shoudl this be fixed...

I think this is not the case of the regex

This is what is happening:


Receiving a content like this:

Content: May 24, 1:46 A May 24, 2:21 AM May 24, 2:44 AM May 24, 3:25 AM May 24, 12:36 PM May 24, 1:15 PM 4. Jun 1, 11:46 PM Jun 1, 11:59 PM Jun 2, 12:44 AM Jun 2, 3:39 PM **LA** * ![](https://app.chatwoot.com/rails/active_storage/blobs/redirect/....../image.png) ![](https://app.chatwoot.com/rails/active_storage/blobs/redirect/ey....../image.png) May 24, 1:46 A May 24, 2:21 AM *

This is logged in the automation from this:
Content:{{webResponse.body.messages.content}}

Then i create a variable {{contentInlineImages}} with the below:
{{content.match("!\\[\\]\\(([^\\)]+)\\)").split(",")}}

and this prints in the logs :

InlineImages:[https://app.chatwoot.com/rails/active_storage/blobs/redirect/....../image.png], [https://app.chatwoot.com/rails/active_storage/blobs/redirect/....../image.png]

So a comma separated list

Then i create an email automation step and i include as body:

Inline attachments
{{#contentInlineImages}}
<a href="{{.}}">Link</a><br>
{{/}}

I receive this in the email:

Inline attachments

Link

which is jsut the textand not a link, and as we see only one

if i use in the email body simply the iteration of the {{.}} without <a> tags

Inline attachments

{{#contentInlineImages}}
{{.}}<br>
{{/}}

  

i receive this in the email:

Inline attachments

[https://app.chatwoot.com/rails/active_storage/blobs/redirect/......./image.png], 
[https://app.chatwoot.com/rails/active_storage/blobs/redirect/......./image.png] 

another approach i took is intsead of using the {{contentInlineImages}} variable to directly use the construction of it in the email body:

Inline attachments

{{#content.match("!\\[\\]\\(([^\\)]+)\\)").split(",")}}
<a href="{{.}}">{{.}}</a><br>
{{/}}

I added the {{.}} as the name for the Links to see if it gets the urls, and as of my surprise it gets the urls with the  [ ] , but doesn't make a link and prints this:


Inline attachments

[https://app.chatwoot.com/rails/active_storage/blobs/redirect/....../image.png]

[https://app.chatwoot.com/rails/active_storage/blobs/redirect/....../image.png]

So it gets the urls with [ ] as we see and prints them from the name part of the link, but doesn't make a link, it is just text printed there of course from the name part of the a tags but it is not a clickabel link

Thanks and regards

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2024

Please try to stay with one thread when responding.  That will help others reading this question in the future know if there are multiple solution approaches.  Thanks!

 

When you created the variable, that is stored as text.  It is no longer a list.

Please try this to parse your saved variable by converting it back into a list:

{{#contentInlineImages.split(", ").match("[(.*)]")}}
<a href="{{.}}">Link</a>
{{/}}

 

Michail Kotantakis June 2, 2024

Hi @Bill Sheboy 

Yeah my bad with the reply out of the thread

I tried your iteration but it prints nothing, it is empty

And in the logs this:

ContentImages:{{contentInlineImages.split(", ").match("[(.*)]")}}
or 
ContentImages:{{#contentInlineImages.split(", ").match("[(.*)]")}} <a href="{{.}}">Link</a> {{/}}  do not print anything

 

Michail Kotantakis June 2, 2024

Hi @Bill Sheboy 

Just managed to make it work:

The {{contentInlineImages}} variable instead of using split() i used join()

{{contentInlineImages}}
{{content.match("!\\[\\]\\(([^\\)]+)\\)").join(",")}}

This makes it a string of urls comma separated

 

Then in the email body construction i used split() on the variable

{{#contentInlineImages.split(",")}}
<a href="{{.}}">Image Link</a><br/>
{{/}}

And this creates links for each image

Thanks for your advice as always

Regards

 

 

Like Bill Sheboy likes this
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2024

Well done!

One of the challenges with splitting some text is ensuring the joining delimiter is a known string of text.  The default appears to be comma-followed-by-a-space: ", ".  That seems consistently used for lists but not arrays.  What you did by explicitly adding the join(",") helps to confirm what to split() on later.

Like Michail Kotantakis likes this
0 votes
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 24, 2024

Hi @Michail Kotantakis 

For unique scenarios like this, I suggest doing this step by step, writing results to the audit log for each step, to observe when it stops working.  That may confirm what the expression / variable contains that prevents split() from working as expected.

 

Also please note...

In my experience, the match() function in rules may not always handle complicated regular expressions well.  This seems to be caused by which constructs are supported (or not) and the parsing order of the regular expression, particularly when it contains escaped characters.

Incrementally building up your regular expression, from the simplest first and adding edge cases later, may uncover the problem.

A technique to help with the apparent parsing order symptoms is to first store the regular expression in a variable, and then use that in the match.  For example:

  • action: create variable
    • name: varRegEx
    • value: !\\[\\]\\(([^\\)]+)\\)
  • action: something using the match()
    • {{content.match(varRegEx)}}

 

Kind regards,
Bill

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events