Can you share your code here? so that I can figure out what exactly you are trying to implement. As per my understanding so far, you want to send the to D365 Leads.
This can be achieved by calling this function "purechatApi.on('chat:start', function (args) {" inside the code snippet you got from PureChat.
You can store the lead details in a local object and then call the D365 API to push the data:
purechatApi.on('chat:start', function (args) {
console.log('Visitor started a new chat!');
console.log(args.chatboxId) // Prints the ID of the chatbox to the console window
console.log(args.name) // Prints the name of the visitor that started the chat
console.log(args.email) // Prints the email of the visitor that started the chat
console.log(args.phoneNumber) // Prints the email address of the visitor that started the chat
console.log(args.question) // Prints the question the visitor entered when he / she started the chat
// Replace the endpointUrl with the URL of your CRM instance
var endpointUrl = "">yourCRMinstance.crm.dynamics.com";
// Replace the accessToken with a valid access token
var accessToken = "yourAccessToken";
//Call your method that connects to the dynamics 365
fetch(endpointUrl + '/api/data/v9.1/leads', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken
},
body: JSON.stringify({
"firstname": args.name,
"email": args.email
})
})
.then(response => response.json())
.then(data => {
console.log("Lead created with ID: " + data.id);
})
.catch(error => {
console.error('Error:', error);
});
});
Please make sure to generate the valid access token based on the client id and secret.
https://www.c-sharpcorner.com/article/generate-access-token-for-dynamics-365-single-tenant-server-to-server-authentica/