i have been trying to make a call with the twilio client.calls.create since it has the functionality i needed for my app using the url to play or say something to the called party before they get connected but i keep getting 404 error whenever it tries to play the audio to the called party.
and testing the getaudio endpoint give the right twiml response to be executed response from audio endpoint
https://drab-zebu-6611.twil.io/assets/TunePocket-Touch-Of-Life-Logo-Preview.mp3makecall endpoint
const makeCall = async (req, res) => {
const { calleeNumber, calleeName, calldirection, audioCategory} = req.body;
try {
if (!calleeNumber) {
return res.status(400).send("Recipient number is required");
}
// Generate the access token
// const token = await callTokenGenerator(req);
// const twimlResponse = generateCallTwiML(calleeNumber);
// const twimlUrl = new URL(
// `https://${req.get("host")}/play-audio/${audioCategory}`
// ).toString();
const call = await client.calls.create({
// twiml: twimlResponse,
url:`https://${req.get("host")}/play-audio/${audioCategory}`,
to: calleeNumber,
from: process.env.PHONE_NUMBER,
statusCallback: "https://drab-zebu-6611.twil.io/status",
statusCallbackMethod: "POST",
statusCallbackEvent: ["initiated", "ringing", "answered", "completed"],
});
const checkDirection = function () {
if (call.direction === "outbound-api") {
return "outbound";
}
};
const callRecord = new Call({
callSid: call.sid,
phoneNumber: calleeNumber,
calldirection: checkDirection(),
callDuration: call.duration,
callDate: new Date().toDateString(),
});
await callRecord.save();
res.status(200).json({
message: "Call initiated successfully",
callRecord: callRecord,
// call,
/* token: token,*/
});
} catch (err) {
console.error("Error making the call:", err);
res.status(500).send("Failed to make the call");
}
};
get audio to play endpoint
const getAudioLinkByCategory = async (req, res) => {
try {
const { category } = req.params;
const audioFiles = await audio.find({ category });
if (audioFiles.length === 0) {
throw new Error(`No audio files found for category: ${category}`);
}
const randomIndex = Math.floor(Math.random() * audioFiles.length);
const randomAudioFile = audioFiles[randomIndex];
const twiml = new twilio.twiml.VoiceResponse();
twiml.play(randomAudioFile.assetLink, { loop: 1 });
// Send the TwiML as response
res.type("text/xml");
res.send(twiml.toString());
} catch (error) {
console.error("Error fetching audio files:", error);
res
.status(500)
.json({ error: `Error fetching audio files: ${error.message}` });
}
};
routes router.post("/make-call", callcontroller.makeCall); router.get("/play-audio/:category", audiocontroller.getAudioLinkByCategory);
i have tried playing the returned link from the response of the endpoint for selecting audio to play to check if the error was with the audio and the audio plays perfectly.