Skip to content

Instantly share code, notes, and snippets.

@slavahatnuke
Created June 9, 2015 18:20
Show Gist options
  • Save slavahatnuke/a993998aa79399e03506 to your computer and use it in GitHub Desktop.
Save slavahatnuke/a993998aa79399e03506 to your computer and use it in GitHub Desktop.
twilio.integration
'use strict';
var container = require('../container');
var Twilio = container.get('Twilio');
var ReceptionService = container.get('ReceptionService');
var StreamModel = container.get('StreamModel');
var User = container.get('User');
var SocketIO = container.get('SocketIO');
var Call = container.get('Call');
exports.connect = function (req, res, next) {
var resp = new Twilio.twilio.TwimlResponse();
resp
.dial({
action: Twilio.getUrl('/api/twilio/queue/hangup/' + req.params.token),
hangupOnStar: true,
record: true
}, function (node) {
node.queue(req.Token.queue)
});
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(resp.toString());
ReceptionService.findCallById(req.Token.callId, function (err, call) {
if (err) return next(err);
//call.user = receptionist
call.connectedUser = req.Token.userId;
call.status = 'speak';
call.save(function (err) {
if (err) return next(err);
var isMine = call.isMine();
SocketIO.emit(call.user, 'call.connected', {callId: call._id, userId: call.connectedUser, mine: isMine});
SocketIO.emit(call.user, 'call.updated', {callId: call._id});
if (!isMine) {
User.findById(call.user, function (err, user) {
if (err) return next(err);
Call.disconnectUser(user, function (err) {
if (err) return next(err);
});
});
}
});
});
}
/// .....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment