Skip to content

Instantly share code, notes, and snippets.

@davehorton
Created November 3, 2022 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davehorton/b51999479e23891ca9e4956de44dcf05 to your computer and use it in GitHub Desktop.
Save davehorton/b51999479e23891ca9e4956de44dcf05 to your computer and use it in GitHub Desktop.
agent assist webhook
const router = require('express').Router();
const WebhookResponse = require('@jambonz/node-client').WebhookResponse;
const axios = require('axios');
router.post('/', (req, res) => {
const {logger} = req.app.locals;
const {call_sid} = req.body;
logger.debug({payload: req.body, call_sid}, 'POST /agent-assist');
try {
const app = new WebhookResponse();
app
.dial({
callerId: process.env.AGENT_ASSIST_CALLER_ID || req.body.from,
answerOnBridge: true,
target: [
{
type: 'phone',
number: process.env.AGENT_ASSIST_DDI,
trunk: process.env.AGENT_ASSIST_OUTBOUND_TRUNK,
headers: {
'User-To-User': `01${call_sid};encoding=ascii`
}
}
],
transcribe: {
transcriptionHook: '/agent-assist/transcribe',
recognizer: {
vendor: 'microsoft',
language: 'en-US',
outputFormat: 'detailed',
hints: ['Genesys', 'IVR', 'agent', 'queue', 'iphone'],
interim: true,
separateRecognitionPerChannel: true
}
}
});
res.status(200).json(app);
} catch (err) {
logger.error({err}, 'Error');
res.sendStatus(503);
}
});
router.post('/transcribe', (req, res) => {
const {logger} = req.app.locals;
const {speech, call_sid} = req.body;
const {language_code, is_final} = speech;
logger.debug({payload: req.body}, 'POST /agent-assist/transcribe');
res.sendStatus(200);
if (speech.is_final && speech.alternatives.length && speech.alternatives[0].transcript) {
const text = speech.alternatives[0].transcript;
const confidence = speech.alternatives[0].confidence;
const payload = {
userId: call_sid,
URLToken: process.env.AGENT_ASSIST_URL_TOKEN,
sessionId: call_sid,
text,
data: {
finalMessage: is_final,
channel: speech.channel,
...(confidence && {confidence}),
...(language_code && {language: language_code})
},
};
logger.info({payload}, `injecting ${text}`);
axios({
method: 'POST',
url: '/new/v2.0/endpoint/inject',
baseURL: process.env.AGENT_ASSIST_BASE_URL,
headers: { 'X-API-Key': process.env.AGENT_ASSIST_API_KEY},
data: payload
})
.then((response) => {
if (response.status !== 202) logger.info({data: response.data}, `got response ${response.status}`);
return;
})
.catch((err) => {
logger.info({err}, 'Error');
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment