✅ Resolved! Breaking up long text to correct nice looking chunks (with correct length)!

Hello guys!

Let me start from the beginning: I have a scenario, where Outlook module watch new emails and send it to me every hour in a short form. There might be 15-50 emails. Sending 50 emails to Telegram as 50 messages might be a good idea but not this time - telegram limits each message to 4096 symbols. That means that you need to cut your message to smaller chunks and send send them. But here is the thing: I use Markup (for hyperlinks), and if hyperlink will be in the middle between two 4096 chunks - I will get into trouble being unable to open a link.

I read several topics like this below but none of them explained how to do what I wanted. So I spent hours looking for a way to make it work:

:white_check_mark: Solution:

Let us dive deeper:

  1. Set up a repeater
  2. Inside a repeater cycle we need to get two values: FinishChar (Last letter position in latest chunk) and AllLength (that is just handy in my case but you can try to avoid it)
  3. Set up a filter
  4. Do the magic in Telegram:
  5. Do the magic while Setting/Updating our Variables:

Logic itself is quite simple:
Repeater makes the scenario repeat as many times as there are potential chunks by dividing total length by max limit of message length. Then we need to know the latest processed chunk size which must be less than 4000. Each element in a chunk end with :up_arrow::up_arrow::up_arrow::up_arrow::up_arrow::up_arrow::up_arrow::up_arrow: so what we need to do is to cut the last part from the end (4000) up to the latest :up_arrow::up_arrow::up_arrow::up_arrow::up_arrow::up_arrow::up_arrow::up_arrow: and then calculate its length. Lets say we have 4000 - 153 = 3847 - correct chunk size and 3847 is also the latest letter position of current chunk and also the first letter position of the next chunk. We just need to add 3847 + 4000 and now we know that the second chunk starts at 3847 and finishes at 7847 BUT we still need to subtract the small part on the end. So we do our search again to find the latest :up_arrow::up_arrow::up_arrow::up_arrow::up_arrow::up_arrow::up_arrow::up_arrow: and then calculate its length from the very end (which is in this example - 7847) to the first occurrence (if we count from the end). Now we know lets say its length i.e. 223 we need to subtract: 7847 - 223 = 7624 - means the second chunk is 3847 to 7624.

For reference:
{{ replace( substring( substring( trim(80.text); ifempty(99.finishChar; 0); if( (sum(ifempty(99.finishChar; 0); 4000) > 99.allLength) & (length(99.allLength) > 0); 99.allLength; sum(ifempty(99.finishChar; 0); 4000) ) ); 0; 4000 - length( last( split( substring( trim(80.text); ifempty(99.finishChar; 0); if( (sum(ifempty(99.finishChar; 0); 4000) > 99.allLength) & (length(99.allLength) > 0); 99.allLength; sum(ifempty(99.finishChar; 0); 4000) ) ); "⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️"; true ) ) ) ); "⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️"; newline ) }}

Oh no. You just need a single module without all those functions!

Example Output (ARRAY):

Example Output (Bundles):

If bundles is selected, it even counts the length and estimate the number of LLM tokens.

You can get and use it for free here: [Free App] My Toolbox of Useful Modules

Hope this helps! Let me know if there are any further questions or issues.

@samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

The problem is that you chunk the text irrespectively on the context. That means that if I am using a markdown like:
------ Element 1 ------
Lorem ipsum dolor sit amet, consectetur ~~ adipiscing elit ~~, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut - aliquip ex ea commodo consequat. ***Duis aute irure dolor *** in reprehenderit in voluptate velit esse cillum dolore eu fugiat 2+2 * 0.5 nulla pariatur. Excepteur (sint occaecat) cupidatat non proident, sunt in culpa qui: officia deserunt mollit anim id est laborum. [Some very long text with the link Link] (https://www.somewebsite.com/guides/content/editing-an-existing-page?post=BLIBYbwfiwgofbLWFgogf7gwbLgWOGf7ILUb) and [Some Second very long text with the link Link] (https://www.somewebsite.com/guides/content/editing-an-existing-page?post=BLIBYbwfiwgofbLWFgogf7gwbLgWOGf7ILUb) <br / >
%%% End of the post 1 %%%

------ Element 2 ------
Lorem ipsum ~~ dolor sit amet ~~, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut - aliquip ex ea commodo consequat. ***Duis aute irure dolor *** in reprehenderit in voluptate (velit esse cillum dolore eu fugiat) nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia: deserunt mollit anim 2+2 * 0.5 id est laborum. [Some very long text with the link Link] (https://www.somewebsite.com/guides/content/editing-an-existing-page?post=BLIBYbwfiwgofbLWFgogf7gwbLgWOGf7ILUb) and [Some Second very long text with the link Link] (https://www.somewebsite.com/guides/content/editing-an-existing-page?post=BLIBYbwfiwgofbLWFgogf7gwbLgWOGf7ILUb) <br / >
%%% End of the post 2 %%%

your method will eventually cut the markdown and brake it:(

If you have a known section element, like ------ Element 2 ------,

you can use a Text parser “Match pattern” module —

Finds string elements matching a fixed pattern (regular expression).

For more information, see https://www.make.com/en/integrations/regexp, and https://www.make.com/en/help/app/regexp in the help centre.

Hope this helps! Let me know if there are any further questions or issues.

@samliew