sendandwait telegram and set redis and get redis
Configured with Agent, Execute Workflow, Execute Workflow Trigger, Information Extractor, and additional nodes. Review the connections and setup before adapting this template.
Node categories in this workflow
- Messaging
- Data store
- AI & models
- Trigger
- Routing
- Code
- Subworkflow
Icons describe general node roles, inferred from node types. Inspect the parameters for the configured action.
Drag to pan; use + and − to zoom. Reconstructed from the exported node positions; nothing runs here. Dashed lines represent non-main connections. Full node names and notes appear below.
How it works
The exported workflow contains the following configured nodes. Their connections are shown above; inspect the JSON for the complete logic, expressions, and external actions.
| Node | Category | Type | Operation |
|---|---|---|---|
| Human Handoff using Send and Wait | Messaging | Telegram | sendAndWait |
| Set Interaction to Bot | Data store | Redis | set |
| Set Interaction to Human | Data store | Redis | set |
| Get Interaction State | Data store | Redis | get |
| Model | AI & models | OpenAI Chat Model | See node parameters |
| Memory | Data store | Memory Redis Chat | See node parameters |
| Handoff Subworkflow | Trigger | Execute Workflow Trigger | See node parameters |
| Telegram Trigger | Trigger | Telegram Trigger | See node parameters |
| Send Response | Messaging | Telegram | See node parameters |
| Switch Interaction | Routing | Switch | See node parameters |
| Information Extractor | AI & models | Information Extractor | See node parameters |
| OpenAI Chat Model | AI & models | OpenAI Chat Model | See node parameters |
| With Defaults | Code | Code | See node parameters |
| Has All Criteria? | Routing | If | See node parameters |
| Onboarding Agent | AI & models | Agent | See node parameters |
| Get Onboarding Chat History | Data store | Redis | get |
| After Sales Agent | AI & models | Agent | See node parameters |
| Memory1 | Data store | Memory Redis Chat | See node parameters |
| Model1 | AI & models | OpenAI Chat Model | See node parameters |
| Handoff Tool | Subworkflow | Tool Workflow | See node parameters |
| Handoff Subworkflow1 | Subworkflow | Execute Workflow | See node parameters |
| Send Response2 | Messaging | Telegram | See node parameters |
| Memory2 | Data store | Memory Redis Chat | See node parameters |
| Update Agent Memory | Data store | Memory Manager | See node parameters |
| Send Response3 | Messaging | Telegram | See node parameters |
| Get Canned Response | Messaging | Telegram | See node parameters |
| Notify user | Messaging | Telegram | See node parameters |
| Memory3 | Data store | Memory Redis Chat | See node parameters |
| Update Agent Memory1 | Data store | Memory Manager | See node parameters |
| Continue Conversation | Messaging | Telegram | See node parameters |
Setup
- Download the JSON and import it into an isolated n8n instance. Leave the workflow inactive.
- Connect your own credentials through n8n’s credential store. Replace
REPLACE_WITH_*values with your resource identifiers. - Inspect API destinations, Code nodes, database queries, file paths, and outbound actions.
- Run with synthetic data and harmless destinations. Enable triggers only after checking the actual outputs.
Requirements
An n8n instance with the listed node types and compatible versions. Service-connected workflows may require accounts, credentials, or paid APIs.
Credential types declared in the source
openAiApiredistelegramApi
Workflow notes
1. Check Interaction State For Incoming Message
1. Check Interaction State For Incoming Message
Learn more about the telegram trigger
This is an example of a state-based agent - the technique commonly known as a finite-state machine. This is a great way to really control the flow and direction of the conversation where there are requirements to collect data or perform steps in sequence. To manage the session state, we can use Redis and the session key will be the user's number.
2. Onboarding Agent to Validate Customers
2. Onboarding Agent to Validate Customers
This agent unlike the common multi-tasking examples out there, is only tasked to handle the user's onboarding. In this way, we trade convenience for clutter but ensure the agent is less likely to stray from the desired path.
3. Extract Customer Details from Conversation
3. Extract Customer Details from Conversation
Learn more about the Information Extractor
To collect the user's details in a structured format, we can analyse the current conversation and extract the data with the Information Extractor node. This allows the conversation to remain free-form and avoids asking a question for each field. We use a code node to check if all details are captured.
4. Human Handoff when Criteria Met
4. Human Handoff when Criteria Met
Here, we initiate the hand-off which calls our hand-off subworkflow. Subworkflows can be a great way to run actions concurrently and without blocking the reply to the user. At this point, the session state would be set to "human" which means the human agent should take over.
5. Defer to Human
5. Defer to Human
When session state is "human", no further user messages can reach the agent. This is to ensure there isn't a parallel conversation happening which may confuse the human and agent.
6. Interaction Switched Back to Bot
6. Interaction Switched Back to Bot
Read more about the Customer Workflow Tool
When the session state is "bot", it signals the human agent has "transferred" the user back to the bot and so can continue processing their messages. A Custom Workflow Tool is used to retrigger the hand-off process whenever the user requests for it.
7. Interaction Subworkflow
7. Interaction Subworkflow
Learn more about n8n's human-in-the-loop feature
The hand-off implementation here involves a "human-in-the-loop" feature of n8n - a feature which "pauses" an execution whilst running until a reply or action is sent back by the human. Sounds complicated but good to note that n8n handles this interaction seemlessly! Here, we're using it to allow the human agent to reliquish control of the conversation back to the AI agent. Additionally, the human agent's feedback is captured and added to the agent's memory to better assist the user afterwards.
Try it out
Try it out
This n8n template demonstrates an approach to perform bot-to-human handoff using Human-in-the-loop functionality as a switch.
In this experiment, we play with the idea of states we want our agent to be in which controls it's interacton with the user.
- First state - the agent is onboarding the user by collecting their details for a sales inquiry. After which, they are handed-off / transferred to a human to continue the call.
- Second state - the agent is essentially "deactivated" as further messages to the bot will not reach it. Instead, a canned response is given to the user. The human agent must "reactivate" the bot by completing the human-in-the-loop form and give a summary of their conversation with the user.
- Third state - the agent is "reactivated" with context of the human-to-user conversation and is set to provide after sales assistance. An tool is made available to the agent to again delegate back to the human agent when requested.
How it works
- This template uses telegram to handle the interaction between the user and the agent.
- Each user message is checked for a session state to ensure it is guided to the right stage of the conversation. For this, we can use Redis as a simple key-value store.
- When no state is set, the user is directed through an onboarding step to attain their details. Once complete, the agent will "transfer" the user to a human agent - technically, all this involves is an update to the session state and a message to another chat forwarding the user's details.
- During this "human" state, the agent cannot reply to the user and must wait until the human "transfers" the conversation back. The human can do this by replying to "human-in-the-loop" message with a summary of their conversation with the user. This session state now changes to "bot" and the context is implanted in the agent's memory so that the agent can respond to future questions.
- At this stage of the conversation, the agent is now expected to handle and help the user with after-sales questions. The user can at anytime request transfer back to the human agent, repeating the previous steps as necessary.
How to use
- Plan your user journey! Here is a very basic example of a sales inquiry with at most 3 states. More thought should be developed when many more states are involved.
- You may want to better log and manage session states so no user is left in limbo. Try connecting the user and sessions to your CRM.
- Note, the Onboarding agent and After-Sales agent have separate chat memories. When adding more agents, it is recommend to continue having separate chat memories to help focus between states.
Need Help?
Join the Discord or ask in the Forum!
Happy Hacking!
Add Human Chat ID
Add Human Chat ID
This is needed to notify the human agent.
sendandwait telegram and set redis and get redis
sendandwait telegram and set redis and get redis
Purpose and structure
This folder contains the sendandwait telegram and set redis and get redis workflow. Its executable nodes use agent, code, executeWorkflow, executeWorkflowTrigger, if, informationExtractor, lmChatOpenAi, memoryManager, memoryRedisChat, redis, switch, telegram, telegramTrigger, toolWorkflow. The table and connection map below are derived from the JSON, not an inferred guarantee of behavior.
Setup
Import inactive. Configure your own credentials and replace REPLACE_WITH_* values. Review external destinations and Code nodes before running.
Entry points
Handoff Subworkflow, Telegram Trigger
Verification
Sanitized source candidate; not runtime-tested. This overview is generated from node metadata. Read the folder README for the connection map, credential types, and license.
Attribution
https://github.com/Zie619/n8n-workflows/blob/94007c1445d9258a7da116646b79473e7c7c3282/workflows/Telegram/0815_Telegram_Code_Update_Triggered.json MIT; preserve source notices.