i'm searching for guideness about twilio voice and sms and the integration of these with dialogflow cx, i'm just learning about it and I think the information and documentation I've found is not enough and it's kind of confusiong to me.
I've already have a Twilio number connected to a webhook which I created from GCP run and build, but I don't know if I'm ignoring an important step, i'd be very grateful if someone can help me.
I've researching and found a repository where they explain the integration with twilio but focusing in whatsapp, this is my code:
imports...
const sessionClient = new SessionsClient({
apiEndpoint: `${process.env.LOCATION}-dialogflow.googleapis.com`,
});
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const listener = app.listen(process.env.PORT || 8081, () => {
console.log('Your Dialogflow integration server is listening on port ' +
listener.address().port);
});
const twilioToDetectIntent = (twilioReq) => {
const sessionId = twilioReq.body.To;
const sessionPath = sessionClient.projectLocationAgentSessionPath (
process.env.PROJECT_ID,
process.env.LOCATION,
process.env.AGENT_ID,
sessionId
);
const message = twilioReq.body.Body;
const languageCode = process.env.LANGUAGE_CODE;
const request = {
session: sessionPath,
queryInput:
{
text: {
text: message
},
languageCode
}
};
return request;
};
const detectIntentToTwilio = (dialogflowResponse) => {
let reply = "";
for (let responseMessage of dialogflowResponse.queryResult.responseMessages) {
if (responseMessage.hasOwnProperty('text')) {
reply += responseMessage.text.text;
}
}
const twiml = new MessagingResponse();
twiml.message(reply);
return twiml;
};
const getResponseMessage = async (req) => {
const dialogflowRequest = twilioToDetectIntent(req);
const [dialogflowResponse] = await sessionClient.detectIntent(dialogflowRequest);
const twiml = detectIntentToTwilio(dialogflowResponse);
return twiml.toString();
};
//voice
const sendToDialogflowVoice = async (speechResult, callSid) => {
const sessionPath = sessionClient.projectLocationAgentSessionPath(
process.env.PROJECT_ID,
process.env.LOCATION,
process.env.AGENT_ID,
callSid
);
const request = {
session: sessionPath,
queryInput: {
audio: {
config: {
languageCode: process.env.LANGUAGE_CODE,
},
audio: speechResult, // El mensaje de voz
},
},
};
try {
const [response] = await sessionClient.detectIntent(request);
const messageFromDialogflow = response.queryResult.responseMessages[0].text.text;
return messageFromDialogflow;
} catch (error) {
console.error('Something ocurred', error);
return 'error from Dialogflow.';
}
};
app.post('/sms', async (req, res) => {
const message = await getResponseMessage(req);
console.log("MESSAGE: " + message);
res.send(message);
});
app.post('/voice', async (req, res) => {
const speechResult = req.body.SpeechResult;
const callSid = req.body.CallSid;
const dialogflowResponse = await sendToDialogflowVoice(speechResult, callSid);
const twiml = new MessagingResponse();
twiml.message(dialogflowResponse);
res.setHeader('Content-Type', 'text/xml');
res.send(twiml.toString());
});
process.on('SIGTERM', () => {
listener.close(async ()=> {
console.log('Closing server.');
process.exit(0);
});
});
exports...
You can integrate with Voice using One click Integration but would have to add this "virtualagent-ccai-prod@dialogflow-prod-env.iam.gserviceaccount.com" in your project via IAM.
Link to One Click Integration of Voice Agent with Twilio:
For Integrating with SMS you would have to build via Twilio Studio.
Step#1: Create Session for incoming First Message.
Step#2: Pass the Session and First incoming Message to Dialogflow API.
Step#3: Send the Dialogflow API response via Send and Wait Widget to wait for user response this will work in a loop. You will also to add a way to handle end session and so on.
Just Import this into Twilio Studio adjust according to your Needs. Let me know if further help is needed : )