Storing values to global variables

Dear Community,
I’ve been fiddling with an annoying issue today and would like to ask you for ideas or even a solution. (I’m on a Pro plan)
I’ve been creating a Telegram bot workflow with an AI agent. I wanted to solve a simple issue, the user could either send a text or voice message. So I’m using a Switch node to pass the text or transcribe the voice message and pass that result. Okay, after the agent did it’s work, I wanted to make sure the reply is sent in the input format, so either text or voice message. In order to achieve that, I again wanted to use Switch node and use the result of the first one to direct the output. But that didn’t work, as the output values wouldn’t be available at this stage of my workflow anymore.
Okay, so I thought, I’ll create a global variable and based on the input I’d set it’s value to “text” or “voice” and later on, match in my Switch node. But it turned out that I even can’t change a variable’s value, which makes it quite a bummer. So I somehow managed to set a kind of value via a code node at the very beginning of my workflow and it’s value survived till the end Switch node (YAY). But after creating the voice output (via Elevenlabs), when trying to send it via a Telegram Voice note node, the ChatID from the trigger is unavailable as well.

As you can imagine, I’m a little frustrated. Maybe somebody here can share an example or help me out, I’d be very happy and thankful.

Cheers
Drfiter

I can see how frustrating that setup can be.

To solve this, the most reliable approach in n8n is to store the input type and Chat ID in the item.json early in the workflow, using a Function or Set node. For example, right after the trigger, use a Function node to attach inputType ("text" or "voice") and chatId to the item. This way, those values persist throughout the flow and can be accessed later by any node, including your final Switch and Telegram node.

For the reply format logic, you can safely reference {{$json.inputType}} in your end-stage Switch node to route the correct output (text or voice). And for sending the reply, use {{$json.chatId}} which you stored early on.

why not just use the first branching and then keep them in their own branch do not merge it together and keep them in a separate branch, this way there’s a clear separation of concern between the 2