Keep track of context for a Telegram multi-step command

Describe the question

I’m trying to implement a multi-step Telegram command

Please share the workflow

As of now, I’m using a Telegram trigger node and a switch to determine the command I’ve received, and redirect the workflow to different branches for different commands.

While this works great for single commands workflows (like getting the weather or running a Speedtest), I don’t know how to approach it for multi-step commands, like the following example:

  • User sends /search command to the bot
  • Bot asks the user for a category (Movie, TV Shows, Music, etc)
  • User responds
  • Bot asks the user for a media title
  • User responds
  • Bot asks the user for the media year
  • User responds
  • Bot uses different APIs (IMDb, MovieDb, Spotify etc) to search information about the media and share the results

I don’t understand if and how I can keep track of the context of the conversation, recognizing a response and sending the next question until the last response is obtained.

Does anyone have an idea on how to approach a case like this?

Thank you!!

Information on your n8n setup

  • n8n version: 0.175.1
  • Running n8n via: Docker

Hey @Havock94, that’s a tricky one. Each workflow execution (triggered by a user interaction in your case) would be independent from other workflow executions. Meaning there is no built-in way for managing entire conversations.

You would essentially have to store all relevant details for each conversation in a database and then fetch the required details again when a user interaction comes in.

Thank you for your input, I’m now trying to handle it with workflowStaticData, basically saving a stringified object for each chatID, something like:

staticData["answers-" + items[0]] = JSON.stringify({
    step1: "Answer 1", //Old answer
    step2: "Answer 2". //Old answer
    step3: items[0].json.message.text //Current answer

This way I should be able to know at each execution what was the last answer and what is the next question to ask, up to the last answer when I can process all the responses and then delete the staticData object for the current chat to free up memory.

For a low number of users that chat with my bot this should suffice (shouldn’t be more than 5 at the moment), in case of many users I might think to move the logic on a database.

Thank you!