Twilio: Hangup a call in an existing conference, ERROR 20404 - node.js

Following situation:
Someone called my Twilio Number
Twilio requested my url
Caller gets into a conference (don't starts until a second person join)
TwiML makes call to a Mobile
The Moblie dont accept the call
=> no second Person enters the conference so it wont stop and the caller is stuck there.
My solution is to end the whole call if this happens, I already know where to place the endCall function so this is not my problem. The function looks like this (You'll find it in the twilio API too):
client.calls(accountSid).update({
status: "completed"
}, function(err, call) {
if(err){
console.log(err);
}
});
My programm logic is fine, I see that this function is called at the right place but I receive this Error:
{ status: 404,
message: 'The requested resource /2010-04-01/Accounts/AC/Calls/AC.json was not found',
code: 20404,
moreInfo: 'https://www.twilio.com/docs/errors/20404' }
I already red whats at the moreInfo url but I disqualify
the solutions there. Maybe you have an idea whats the problem with this.

Twilio developer evangelist here.
You are almost all the way there. Your problem is that you are using your accountSid when trying to update the call's status.
You need to get hold of the callSid of the original call. You will find the callSid in the parameters that you receive in the incoming webhook when the person calls your Twilio number.
app.post('/calls', function(req, res, next) {
var callSid = req.body.CallSid;
// store callSid somewhere for use later
// return TwiML to set up conference and dial your mobile number
});
You'll need to save that callSid and then use it at this point later when you want to hangup the call.
client.calls(callSid).update({
status: "completed"
}, function(err, call) {
if(err){
console.log(err);
}
});
Let me know if this helps at all.

Related

How to add an extension number in twilio click to call using node js

As per the documentation here & the github source code here, I have cloned the application, its working perfectly.
Suppose if my sales person having some extension then how can I give that extension in this script. Normally, using senddigit I can pass the extension in twilio but I dont know how to implement that with this salesNumber.
twilioClient.createCall(salesNumber, phoneNumber, headersHost)
.then((result) => {
response.send({message: result});
})
.catch((error) => {
response.status(500).send(error);
});
Please some one help on this.
I think you're looking at the wrong code snippet here. The code above doesn't call the Twilio client directly. Instead, it calls the helper function from this file to initiate the call.
Once the user picks up, they will be connected to the sales person via TwiML in this function:
voiceResponse: (salesNumber, Voice = VoiceResponse) => {
let twimlResponse = new Voice();
twimlResponse.say('Thanks for contacting our sales department. Our ' +
'next available representative will take your call. ',
{ voice: 'alice' });
twimlResponse.dial(salesNumber);
return twimlResponse.toString();
}
In this function, you'll be able to send digits along as mentioned here.

Twilio Call, use custom endpoint for xml

Using this code, i am able to receive the call and listen to the contents coming from the voice.xml.
client.calls
.create({
url: 'http://demo.twilio.com/docs/voice.xml',
from: '+16173973230',
to: '+13392153860'
})
.then(call => console.log(call.sid))
I'm trying to use my own generated twiml but when i change to
url: 'http://myserver.com/twilio/auto-message.xml',
which replies exactly the same way:
twilioRouter.get('/auto-message.xml', (req, res) => {
res.type('text/xml')
res.send(`<Response>
<Say voice="alice">Thanks for calling the E T Phone Home Service.</Say>
</Response>`)
})
I get a giant (error) log in the console and on the phone call i hear "We are sorry, an application error has occurred. Goodbye"
What am i doing wrong?
Twilio uses a POST by default. You can tell Twilio to use a GET where you configure the webhook for your phone number.

How to get Conference Sid at the time of dialing twilio call

I've been working with twilio, using Node.js, and dialing call between two web end points. One is client and other is agent. I'm using following code to dial call.
function dialCall(calledNumber, url) {
client.calls.create({
to: `client:${calledNumber}`,
from: twilioNumber,
url: url
})
.then(call => call.sid));
}
I'm using following twiml to establish a call.
const generateTwiml = (conferenceName) => {
let twimlResponse = new VoiceResponse();
twimlResponse.say(`Welcome to unity dialer.`, {
voice: 'alice',
});
const dial = twimlResponse.dial({
timeLimit: '600',
});
dial.conference({
startConferenceOnEnter: true,
endConferenceOnExit: true
}, "Test Room");
return twimlResponse.toString();
};
I've been successfully calling both agents and clients and getting callSid of both calls. However, my question is that at this point of time I also want to get conference Sid as well as I'm dialing the call as conference. What is the method to get that. As per documentation there is a method to fetch conference using conference name and status. However, if I use this some time the same is not returned due to race condition and I have to implement set time out function for same arbitrary delay. I've been getting the result but is there any other solution available for that.
Twilio developer evangelist here.
At the time you return the TwiML to create the conference there is not yet a conference resource so there's no way to get the conference SID at that stage.
As you describe, you can use the conference resource to list conferences and filter by the name you give it. However, you can't list the conferences at the time you return the TwiML because that conference hasn't been created by then.
Rather than setting a timeout, which could be flaky, I recommend you use the statusCallback attribute of the <Conference> TwiML to set a URL to callback to when the conference starts. In the parameters to that callback you will get the ConferenceSid.

Recording calls using twilio rest api

I am using twilio's rest API's to make and record calls.
client.calls.create({
url:" <my callback url>",
to: " <called number> ",
from: <calling number>,
recordingStatusCallback: <my recording url>,
Record: "true",
sendDigits: "1234"
}, function(err, call) {
if(err){
console.log(err);
}
else{
console.log("Call connected");
}
});
Now, I am unable to understand one thing. Since I have already set the recording property, When twilio makes a call at my callback url, What twiML am I supposed to send if I want to record the entirety of the call ??
Twilio developer evangelist here.
Because you have set the record parameter in the request to make the call you don't need to return return any more TwiML to record the whole call.
You just need to return TwiML to do whatever else that you are then recording, like forward to another number with <Dial> or <Play> the user a message.

How to get Express to return the object I just deleted in MongoDB

Feel free to let me know if this isn't a common practice - I'm a fairly new programmer - but I thought I've seen APIs in the past that, when you submit a DELETE request to a resource (/todo/1234), some servers will return the object you just deleted in the response. Is that a thing? If so, I'd be interested in learning how to do it. Here's what I have:
.delete(function (req, res) {
Todo.findById(req.params.todoId).remove(function (err) {
if (err) res.status(500).send(err);
res.send("Todo item successfully deleted");
});
});
This code does delete the item, but I would like to return the item that got deleted in the response instead of a string message. If that's a normal/okay thing to do. If it isn't normal or okay for some reason, please let me know why and I'll just move on. Or perhaps there's a more common way.
This is what I found in the [RFC 7231 docs][1]:
If a DELETE method is successfully applied, the origin server SHOULD
send a 202 (Accepted) status code if the action will likely succeed
but has not yet been enacted, a 204 (No Content) status code if the
action has been enacted and no further information is to be supplied,
or a 200 (OK) status code if the action has been enacted and the
response message includes a representation describing the status.
I'm having a hard time interpreting what the 200 response means - is it only kosher to send a string message (Success!) or an object containing a message attribute ({message: "Success!"})? Or can you do whatever you want there? What's the best practice in Express using Mongoose?
Thanks in advance for the help, and sorry for my noobness with HTTP stuff.
[1]: https://www.rfc-editor.org/rfc/rfc7231#section-4.3.5
You should use findOneAndRemove! Something like:
Todo.findOneAndremove({ id: req.params.todoId }, function( error, doc, result) {
// it will be already removed, but doc is what you need:
if (err) res.status(500).send(err);
res.send(doc.id);
});

Resources