Live Chat Function Telegram

Describe the problem/error/question

I have built a three-workflow live chat system to connect a website chat widget to a Telegram support group.

Workflow 1: (Works) Takes a user message, has an AI Agent process it, and sends an alert to Telegram with a sessionId.

Workflow 2: (Problem) Listens for replies in Telegram and should save the reply back to the conversation history in MongoDB.

Workflow 3: (Works) A webhook for the website to poll for new messages.

The problem is in Workflow 2. The MongoDB - Update documents node consistently fails to find the document to update. It runs successfully without an error, but it always outputs 0 items.

We have already verified the following:

The sessionId being used in the query definitely exists in the database. I have found it by querying my MongoDB database manually with the exact same ID.

The n8n Code node is correctly extracting the sessionId before the MongoDB step.

The database user credential has read and write permissions.

What is the error message (if any)?

There is no error message. The MongoDB node simply outputs “No output data returned,” and the workflow stops.

Please share your workflow

My “Save Reply” workflow (Workflow 2) is very simple:
Telegram Trigger → Code → MongoDB

  1. Code Node (JavaScript):
    This node successfully runs and outputs the correct sessionId and agentReply.
    JavaScript

const item = items[0];
if (!item.json.message.reply_to_message) { return ; }
const originalMessage = item.json.message.reply_to_message.text;
const agentReplyText = item.json.message.text;
const match = originalMessage.match(/Ref ID: ([\w_.-]+)/);
if (match && match[1]) {
return [{ json: { sessionId: match[1], agentReply: agentReplyText } }];
}
return ;

  1. MongoDB Node (Configuration):
    This is the node that is not working.

    Action: Update documents

    Collection: n8n_chat_histories

    Query: { “sessionId”: “{{ $(‘Code’).item.json.sessionId }}” }

    Update:
    JSON

    {
    “$push”: {
    “messages”: {
    “type”: “ai”,
    “data”: {
    “content”: “{{ $(‘Code’).item.json.agentReply }}”
    }
    }
    }
    }

Note: We have also tried a hardcoded query like { “sessionId”: “a2c54c853a4d4f29ac2a7d50371b5b66” } and it still fails to find the document. We also tried a very simple update with $set, which also failed.

Share the output returned by the last node

The output of the MongoDB - Update documents node is always empty (), with the message “No output data returned.”

Is your sessionId was an ObjectID type?

string type

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