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:
Solution:
Let us dive deeper:
- Set up a repeater
- 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)
- Set up a filter
- Do the magic in Telegram:
- 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 so what we need to do is to cut the last part from the end (4000) up to the latest
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
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 ) }}