From 35114b22d821d01f3b739f97b5e2c7d9a191a7a0 Mon Sep 17 00:00:00 2001 From: akirilyuk Date: Tue, 1 Feb 2022 12:47:46 +0100 Subject: [PATCH] add support for hangup and sip:refer --- lib/tasks/cognigy/index.js | 59 ++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/lib/tasks/cognigy/index.js b/lib/tasks/cognigy/index.js index b18a7ad8..ceb8d6f7 100644 --- a/lib/tasks/cognigy/index.js +++ b/lib/tasks/cognigy/index.js @@ -104,7 +104,9 @@ class Cognigy extends Task { this.client.on('error', this._onBotError.bind(this, cs, ep)); this.client.on('finalPing', this._onBotFinalPing.bind(this, cs, ep)); await this.client.connect(); - this.client.sendMessage('welcome message', {...this.data, ...cs.callInfo}); + // todo make welcome message configurable (enable or disable it when + // we start a conversation (should be enabled by defaul)) + this.client.sendMessage('Welcome Message', {...this.data, ...cs.callInfo}); await this.awaitTaskDone(); } catch (err) { @@ -157,6 +159,23 @@ class Cognigy extends Task { return say; } + _makeHangupTask(reason) { + const hangup = makeTask(this.logger, {hangup: { + headers: { + 'X-REASON': reason + } + }}, this); + return hangup; + } + + _makeReferTask(number) { + const refer = makeTask(this.logger, {'sip:refer': { + referTo: number, + referredBy: 'cognigy' + }}); + return refer; + } + async _onBotError(cs, ep, evt) { this.logger.info({evt}, 'Cognigy:_onBotError'); this.performAction({cognigyResult: 'botError', message: evt.message }); @@ -165,10 +184,10 @@ class Cognigy extends Task { } async _onBotFinalPing(cs, ep) { - this.logger.info({}, "TEST FROM ALEX") + this.logger.info({}, 'TEST FROM ALEX'); this.logger.info({prompts: this.prompts}, 'Cognigy:_onBotFinalPing'); if (this.prompts.length) { - const text = "test test test test test".concat(this.prompts.join('.')); + const text = 'test test test test test'.concat(this.prompts.join('.')); if (text && !this.killed) { this.gatherTask = this._makeGatherTask({textPrompt: text}); this.gatherTask.exec(cs, ep, this) @@ -185,16 +204,44 @@ class Cognigy extends Task { this.performHook(cs, this.eventHook, {event: 'botMessage', message: evt}) .then((redirected) => { if (redirected) { - this.logger.info('Cognigy_onTranscription: event handler for bot message redirected us to new webhook'); + this.logger.info('Cognigy_onBotUtterance: event handler for bot message redirected us to new webhook'); this.reportedFinalAction = true; this.performAction({cognigyResult: 'redirect'}, false); } return; }) .catch(({err}) => { - this.logger.info({err}, 'Cognigy_onTranscription: error sending event hook'); + this.logger.info({err}, 'Cognigy_onBotUtterance: error sending event hook'); }); } + + if (evt && evt.data && evt.data.type) { + try { + switch (evt.data.type) { + case 'hangup': + await this._makeHangupTask(evt.data.reason); + this.performAction({cognigyResult: 'hangup Succeeded'}); + this.reportedFinalAction = true; + this.notifyTaskDone(); + return; + case 'refer': + await this._makeReferTask(evt.data.number); + this.performAction({cognigyResult: 'refer succeeded'}); + this.reportedFinalAction = true; + this.notifyTaskDone(); + return; + default: + break; + } + } catch (err) { + this.logger.info({err, evtData: evt.data}, 'encountered error exeuting task'); + if (!this.hasReportedFinalAction) this.performAction({cognigyResult: 'error', err}); + this.reportedFinalAction = true; + this.notifyTaskDone(); + } + + + } const text = parseBotText(evt); if (evt.data) this.config.update(evt.data); if (text) this.prompts.push(text); @@ -226,7 +273,7 @@ class Cognigy extends Task { this.client.sendMessage(utterance); } else { - // if the bot is not connected, should we maybe throw an error here? + // if the bot is not connected, should we maybe throw an error here? this.logger.info('Cognigy_onTranscription - not sending user utterance as bot is disconnected'); } } catch (err) {