我可以解决我的问题以及代码中的错误吗?
问题
服务器代码
generateToken: () {
let clientName = 'twilio_agent';
let _id = req.body._id;
let phoneno = req.body.phoneno;
//
realmsProvider.get({_id: _id})
.then(data => {
if (!data) {
return res.status(404).json({ message: 'Account not found with _id:'+ _id });
}
var capability = new ClientCapability({
accountSid: data.sid,
authToken: data.authToken,
ttl: config.twilio.ttl
});
capability.addScope(
new ClientCapability.OutgoingClientScope({
applicationSid: data.application.sid,
clientName: data.name,
callerId: phoneno
}));
// incoming call
if (phoneno != null) {
capability.addScope(
new ClientCapability.IncomingClientScope(phoneno));
}
else {
capability.addScope(
new ClientCapability.IncomingClientScope(clientName));
}
var token = capability.toJwt();
return res.status(200).json({ token: token });
})
.catch(err => {
return utils.handleError(res, err);
});
}
voice: () {
let phoneNumber = req.body.PhoneNumber;
let callerId = req.body.CallerId;
let twiml = new VoiceResponse();
let dial = twiml.dial({
//https://www.twilio.com/docs/api/twiml/dial
answerOnBridge: true,
//phone number from
callerId : callerId,
//record all calls
record: 'record-from-answer-dual'
});
if (phoneNumber != null) {
dial.number(phoneNumber);
}
dial.client(callerId.replace(/\+/, ''));
res.type('text/xml');
res.send(twiml.toString());
}
浏览器代码
Twilio.Device.setup(token, {
debug: true,
enableRingingState: true,
sounds: {
incoming: 'URL-TO-MP3.mp3',
outgoing: 'URL-TO-MP3.mp3',
dtmf8: 'URL-TO-MP3.mp3'
}
});
function call(currentPhoneno, phoneTo, companyNameInTwilio) {
$scope.calling = 1;
if (!validatePhoneno(phoneTo)) {
growl.error('Bad phone number');
return;
}
var connection = Twilio.Device.connect({ // Connect our call.
CallerId: currentPhoneno, // Your Twilio number (Format: +15556667777).
PhoneNumber: phoneTo, // Number to call (Format: +15556667777).
From: companyNameInTwilio
});
connection.on('ringing', function(hasEarlyMedia) {
// some code for ringing
});
}