Search code examples
javascripteventsbotscircuitcircuit-sdk

How can I add a message to a topic to a message by using the API?


We have a bot who is connected via node-sdk (https://www.npmjs.com/package/circuit-sdk) with Circuit. We use the following code

this.addEventListeners = function addEventListeners(client) {
    client.addEventListener('itemAdded', function (evt) {
            client.addTextItem(evt.item.convId, 'answer from bot');
    });
};

but then, the message is not shown under the current topic but as a separate message in the conversation.

Let me explain it with a screenshot:

Screenshot from circuit ui with example dialog

enter image description here

If I open a new topic ("Topic" in my screenshot) with a message ("Hi, this is the first message"), the bot opens a new topic aswell in which it replies to my message ("Answer from Bot (via Websocket)"). How to get it to reply in the topic I opened?


Solution

  • You need to pass the id for the thread (parentId) in the addTextItem API. See https://circuitsandbox.net/sdk/classes/Client.html#method_addTextItem.

    E.g.

    client.addEventListener('itemAdded', function (evt) {
        client.addTextItem(evt.item.convId, {
            content: 'answer from bot',
            parentId: evt.item.itemId
        });
    });