From 68986cbeb0dea97eac1ed7d65a9177c9f21f7433 Mon Sep 17 00:00:00 2001 From: Jarrod Smith Date: Tue, 11 Jun 2019 14:28:20 +1000 Subject: [PATCH] ignore previous conversation lines when transferring to a bot, 'unless' the previous agent has NOT read. --- examples/agent-bot/MyCoolAgent.js | 46 +++++++++++++++++++++---------- package-lock.json | 8 +++--- package.json | 3 +- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/examples/agent-bot/MyCoolAgent.js b/examples/agent-bot/MyCoolAgent.js index 3732923..c67db8f 100644 --- a/examples/agent-bot/MyCoolAgent.js +++ b/examples/agent-bot/MyCoolAgent.js @@ -26,6 +26,9 @@ class MyCoolAgent extends Agent { init() { let openConvs = {}; + /** Needed for transfer back to bot flow **/ + this.firstSequence = []; + this.lastSequence = []; this.on('connected', msg => { console.log('connected...', this.conf.id || '', msg); @@ -90,6 +93,8 @@ class MyCoolAgent extends Agent { } else if (change.type === 'DELETE') { // conversation was closed or transferred delete openConvs[change.result.convId]; + delete this.firstSequence[change.result.convId]; + delete this.lastSequence[change.result.convId]; } }); }); @@ -98,22 +103,33 @@ class MyCoolAgent extends Agent { this.on('ms.MessagingEventNotification', body => { const respond = {}; body.changes.forEach(c => { - // In the current version MessagingEventNotification are recived also without subscription - // Will be fixed in the next api version. So we have to check if this notification is handled by us. - if (openConvs[c.dialogId]) { - // add to respond list all content event not by me - if (c.event.type === 'ContentEvent' && c.originatorId !== this.agentId) { - respond[`${body.dialogId}-${c.sequence}`] = { - dialogId: body.dialogId, - sequence: c.sequence, - message: c.event.message - }; + // add to respond list all content events not by the bot and only consumer events + if (c.event.type === 'ContentEvent' && c.originatorId !== this.agentId && c.originatorMetadata.role === 'CONSUMER') { + + if (typeof this.firstSequence[body.dialogId] === 'undefined') { + this.firstSequence[body.dialogId] = c.sequence; } - // remove from respond list all the messages that were already read - if (c.event.type === 'AcceptStatusEvent' && c.originatorId === this.agentId) { - c.event.sequenceList.forEach(seq => { - delete respond[`${body.dialogId}-${seq}`]; - }); + + respond[`${body.dialogId}-${c.sequence}`] = { + dialogId: body.dialogId, + sequence: c.sequence, + message: c.event.message + }; + + this.lastSequence[body.dialogId] = c.sequence; + } + + // remove from respond list all the messages that were already read by the bot + if (c.event.type === 'AcceptStatusEvent' && c.originatorId === this.agentId) { + c.event.sequenceList.forEach(seq => { + delete respond[`${body.dialogId}-${seq}`]; + }); + } + + // if an assigned agent has already responded to a message from a visitor, the bot won't respond to it + if (c.event.type === 'ContentEvent' && c.originatorId !== this.agentId && (c.originatorMetadata.role === 'ASSIGNED_AGENT' || c.originatorMetadata.role === 'MANAGER')) { + for (let i = this.firstSequence[body.dialogId]; i <= this.lastSequence[body.dialogId]; i++) { + delete respond[`${body.dialogId}-${i}`]; } } }); diff --git a/package-lock.json b/package-lock.json index 559c73b..96a6f2c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "node-agent-sdk", - "version": "1.1.6", + "version": "1.1.7", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -382,9 +382,9 @@ } }, "dotenv": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-7.0.0.tgz", - "integrity": "sha512-M3NhsLbV1i6HuGzBUH8vXrtxOk+tWmzWKDMbAVSUp3Zsjm7ywFeuwrUXhmhQyRK1q5B5GGy7hcXPbj3bnfZg2g==" + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.0.0.tgz", + "integrity": "sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg==" }, "ecc-jsbn": { "version": "0.1.1", diff --git a/package.json b/package.json index 4045893..1fc0119 100755 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "node-agent-sdk", "description": "Liveperson Agent Messaging SDK for NodeJS", - "version": "1.1.6", + "version": "1.1.7", "author": { "name": "LivePersonInc", "email": "fe-infra-lp@liveperson.com" @@ -19,6 +19,7 @@ }, "devDependencies": { "chai": "^3.5.0", + "dotenv": "^8.0.0", "eslint": "^3.13.1", "mocha": "^5.2.0", "mockery": "^2.0.0",