Enhance Customer Chat by Buffering Messages with Twilio and Redis n8n Workflow Template

n8n workflow template · 12 nodes · OpenAI

Enhance Customer Chat by Buffering Messages with Twilio and Redis is a free n8n workflow template that starts from the Twilio Trigger node and works with OpenAI, Redis and Twilio. It has 12 nodes, 5 of them AI nodes (OpenAI Chat Model, Window Buffer Memory, Chat Memory Manager and AI Agent). To run it, you add your own credentials for OpenAI API, Redis and Twilio API.

At a glance

Trigger
Twilio Trigger (Twilio Trigger)
Nodes
12
Apps
OpenAI, Redis and Twilio
AI nodes
5
Credentials
3 types
Category
Chatbots and messaging

Apps and integrations

  • OpenAI
  • Redis
  • Twilio

Node types used

  • OpenAI Chat Model
  • Redis
  • If
  • Window Buffer Memory
  • Twilio Trigger
  • Twilio
  • Wait
  • Chat Memory Manager
  • Edit Fields (Set)
  • AI Agent

How it works

The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.

Try It Out!

This workflow demonstrates a simple approach to stagger an AI Agent's reply if users often send in a sequence of partial messages and in short bursts.

  • Twilio webhook receives user's messages which are recorded in a message stack powered by Redis.
  • The execution is immediately paused for 5 seconds and then another check is done against the message stack for the latest message.
  • The purpose of this check lets use know if the user is sending more messages or if they are waiting for a reply.
  • The execution is aborted if the latest message on the stack differs from the incoming message and continues if they are the same.
  • For the latter, the agent receives buffered messages and is able to respond to all in a single reply.

Step 1. Listen for Twilio Messages

Read more about Twilio Trigger

In this example, we'll use the sender's phone number as the session ID. This will be important in retrieving chat history.

Step 2. Buffer Incoming Messages

Learn more about using Redis

  • New messages are captured into a list.
  • After X seconds, we get a fresh copy of this list
  • If the last message on the list is the same as the incoming message, then we know no new follow-on messages were sent within the last 5 seconds. Hence the user should be waiting and it is safe to reply.
  • But if the reverse is true, then we will abort the execution here.

Step 3. Get Messages Since Last Reply

Read more about using Chat Memory

Once conditions are met and we allow the agent to reply, we'll need to find the bot's last reply and work out the buffer of user messages since then. We can do this by looking using chat memory and comparing this to the latest message in our redis messages stack.

Step 4. Send Single Agent Reply For Many Messages

Learn more about using AI Agents

Finally, our buffered messages are sent to the AI Agent that can formulate a single response for all. This could potentially improve the conversation experience if the chat interaction is naturally more rapid and spontaneous. A drawback however is that responses could be feel much slower - tweak the wait threshold to suit your needs!

Nodes in this workflow

The 12 nodes in file order (sticky notes excluded). Branches and connections determine execution order in n8n:

  1. OpenAI Chat Model (OpenAI Chat Model)
  2. Add to Messages Stack (Redis)
  3. Should Continue? (If)
  4. Window Buffer Memory (Window Buffer Memory)
  5. Twilio Trigger (Twilio Trigger)
  6. Get Latest Message Stack (Redis)
  7. Send Reply (Twilio)
  8. Wait 5 seconds (Wait)
  9. Get Chat History (Chat Memory Manager)
  10. Window Buffer Memory1 (Window Buffer Memory)
  11. Get Messages Buffer (Edit Fields (Set))
  12. AI Agent (AI Agent)

Credentials you need

  • OpenAI API – used by OpenAI Chat Model
  • Redis – used by Add to Messages Stack and Get Latest Message Stack
  • Twilio API – used by Send Reply and Twilio Trigger

Configure your own credentials after importing. Create each credential in n8n, then select it in the node.

How to import into n8n

  1. Download the JSON file with the button above.
  2. In n8n, open a new workflow, select the ... menu and choose Import from File. You can also paste the JSON straight into the editor canvas.
  3. Open each node that shows a warning and select or create its credential.
  4. Read the notes on the canvas, test the workflow with sample data, then activate it.

Do you need somewhere to run it? Read our guide to self-hosted n8n.

Need this customised?

We can adapt this workflow to your apps, data and process, then deploy and support it.

Contact Us