How to split one long message into multiple messages in n8n

Hello everyone, I hope you are doing well. My name is Samar, and I need help with n8n. I am working on building a workflow where I want to separate one big message into 2 or 3 smaller messages.

For example, if the n8n agent generates a response containing two or three lines, I want to split it into 2 or 3 separate messages and send them back to the customer.

How can I achieve this? Please help me.

Hi @Samar_Abbas,

It would be much easier if you could provide an example of the node that output the data.

From your case, this task looks like an easy one for any AI, For instance, did you try asking ChatGPT the same question? You could say something like:

“Hi ChatGPT, my n8n node (node name) is outputting a long message. Can you help me write code to split it into 3?”

Of course, I’ll still help, but I thought this approach might also show you how to fix things yourself.

Hi @mohamed3nan, Please watch this loom video for better understanding.

https://www.loom.com/share/0a06a5e1b2a744c9ae8aca5ac1c88baa?sid=295ff723-688e-4d8b-9860-25876188eb30

1 Like

Hi @Samar_Abbas
Thanks for the explanation video.

So, let’s assume your agent output a single message, meaning one item, and you want to split it into 3 messages (items) so they can be sent separately.

In this case, you can use a code trick: take the message (1 item) and split it into 3 items, for example by using the positions of the dots in the text.

Here is an example of the logic:

So in your workflow in the video, put the code node here:

It is also possible to ask AI Agent to break the answer into sentences and format the output to be a json array with objects, where the key is “output” and the value is each next sentence, then you would only need to split out the AI Agent response with split out node. Just a thought.

1 Like

Yes, this approach is probably better overall, since you’ll get a structured array of objects by the AI Agent that can be easily split.

The code node, on the other hand, will split strictly at every dot . in the output.

So it depends on whichever option best suits your needs.

@mohamed3nan @jabbson Thank you for your responses. I will test it out both ways. Quick question: how can I use a wait function to send the second and third messages after 2 or 3 second? I don’t want to send all the messages at the same time — I want to use wait. Looking forward to your response.

1 Like

If I was trying to do it, after splitting the responses, I would probably create a loop, to loop over items, where I would add a wait node, then the send message node and loop back to the loop over items node.

If you have 5 minutes, we can jump on a Zoom call to do it, or you can share a Loom video with me. To be honest, I am building my first workflow in n8n and I don’t know the best way to use the Split Out and SplitInBatches nodes. I would really appreciate your help. @jabbson

Hi @Samar_Abbas

You can use the loop for that

the first approach:

the second approach:

1 Like

Thank you for helping, I am facing new issue in same workflow. Please see the loom video.

@mohamed3nan @jabbson

Hmm, this is interesting!
I think the loop wasn’t a good approach here, I just realized it’s a webhook.
Since you’re getting the response output and processing it in ManyChat, you can simply remove the loop node and connect the code node directly to the Respond to Webhook
Then, in your other ManyChat workflow, parse the webhook response (array of outputs)..

@mohamed3nan Thank you for your response. Could you please guide me on how to ‘parse the webhook response [array of outputs]’ in a ManyChat workflow?

I would really appreciate it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.