i am using open ai assitant api to generate my own messages, to Tawk chatbot , i successfully received the reponse and i want to append my response into tawk chatbot with the help of Dom manipulation,i can’t change the innerHTML by using getElementByClassname or idname because the Tawk is generating random id’s and including it into its message dialog html id, i also cannot target the iframe of the page because it also has seprate id’s, but i already saw the html of chatbot which includes 4 iframes, the chatbox is in the second iframe and in this iframe the html content is there which has the message will be coming from the support or we can say the responses , but i want to append new response by myself for your reference this is html of the chatbox :
https://gist.github.com/nikhilkcodes/866613abbcd3f992f70695fd5c1d0fc4
and the function i am trying to implement is this function :
function appendMessage(text) {
var iframes = document.querySelectorAll('iframe');
if (iframes.length > 1) {
var secondIframe = iframes[1];
secondIframe.addEventListener('load', function () {
var iframeDocument = secondIframe.contentDocument || secondIframe.contentWindow.document;
var chatWidget = iframeDocument.querySelector('.tawk-max-container');
if (chatWidget) {
var newMessageDiv = iframeDocument.createElement('div');
var newMessageP = iframeDocument.createElement('p');
var newMessageSpan = iframeDocument.createElement('span');
var messageText = iframeDocument.createTextNode(text);
newMessageSpan.appendChild(messageText);
newMessageP.appendChild(newMessageSpan);
newMessageDiv.appendChild(newMessageP);
chatWidget.appendChild(newMessageDiv);
}
});
}
}
i already tried simple ways like targeting the id and classes by DOM manipulation, i also want to add that i have my Tawk api , since this is my task by my company thats why i have to solve this.
basically i want to append my reponse here shown in the image as new response :
enter image description here
i am really stuck in this problem please help me , i will be always thankful to stack overflow and whoever solve this issue