add support for hangup and sip:refer

This commit is contained in:
akirilyuk
2022-02-01 12:47:46 +01:00
parent 68b2ad526a
commit 35114b22d8

View File

@@ -104,7 +104,9 @@ class Cognigy extends Task {
this.client.on('error', this._onBotError.bind(this, cs, ep)); this.client.on('error', this._onBotError.bind(this, cs, ep));
this.client.on('finalPing', this._onBotFinalPing.bind(this, cs, ep)); this.client.on('finalPing', this._onBotFinalPing.bind(this, cs, ep));
await this.client.connect(); 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(); await this.awaitTaskDone();
} catch (err) { } catch (err) {
@@ -157,6 +159,23 @@ class Cognigy extends Task {
return say; 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) { async _onBotError(cs, ep, evt) {
this.logger.info({evt}, 'Cognigy:_onBotError'); this.logger.info({evt}, 'Cognigy:_onBotError');
this.performAction({cognigyResult: 'botError', message: evt.message }); this.performAction({cognigyResult: 'botError', message: evt.message });
@@ -165,10 +184,10 @@ class Cognigy extends Task {
} }
async _onBotFinalPing(cs, ep) { async _onBotFinalPing(cs, ep) {
this.logger.info({}, "TEST FROM ALEX") this.logger.info({}, 'TEST FROM ALEX');
this.logger.info({prompts: this.prompts}, 'Cognigy:_onBotFinalPing'); this.logger.info({prompts: this.prompts}, 'Cognigy:_onBotFinalPing');
if (this.prompts.length) { 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) { if (text && !this.killed) {
this.gatherTask = this._makeGatherTask({textPrompt: text}); this.gatherTask = this._makeGatherTask({textPrompt: text});
this.gatherTask.exec(cs, ep, this) this.gatherTask.exec(cs, ep, this)
@@ -185,16 +204,44 @@ class Cognigy extends Task {
this.performHook(cs, this.eventHook, {event: 'botMessage', message: evt}) this.performHook(cs, this.eventHook, {event: 'botMessage', message: evt})
.then((redirected) => { .then((redirected) => {
if (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.reportedFinalAction = true;
this.performAction({cognigyResult: 'redirect'}, false); this.performAction({cognigyResult: 'redirect'}, false);
} }
return; return;
}) })
.catch(({err}) => { .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); const text = parseBotText(evt);
if (evt.data) this.config.update(evt.data); if (evt.data) this.config.update(evt.data);
if (text) this.prompts.push(text); if (text) this.prompts.push(text);
@@ -226,7 +273,7 @@ class Cognigy extends Task {
this.client.sendMessage(utterance); this.client.sendMessage(utterance);
} }
else { 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'); this.logger.info('Cognigy_onTranscription - not sending user utterance as bot is disconnected');
} }
} catch (err) { } catch (err) {