public TwiMLResult Index(VoiceRequest request)
  {
    var response = new TwilioResponse();

    object conferenceOptions;

    // If the caller is our MODERATOR, then start the conference when they
    // join and end the conference when they leave
    if (request.From == Moderator)
    {
      conferenceOptions = new
      {
        startConferenceOnEnter = true,
        endConferenceOnExit = true
      };
    }
    else
    {
      // Otherwise have the caller join as a regular participant
      conferenceOptions = new {startConferenceOnEnter = false};
    }

    response.DialConference("My conference", conferenceOptions);

    return TwiML(response);
  }
        public HttpResponseMessage CreateOrJoinConferenceCall(TwilioGatherRequest twilioRequest)
        {
            var conference = _client.ListConferences().Conferences.FirstOrDefault(name => name.FriendlyName.Equals(twilioRequest.Digits));

            var twilioResponse = new TwilioResponse();
            var digits = twilioRequest.Digits;

            twilioResponse.Say(string.Format("You're now joining conference {0}. Thank you.", digits), _voicesettings);

            if (conference != null)
            {
                var participant = _client.GetConferenceParticipant(conference.Sid, twilioRequest.CallSid);
                var context = GlobalHost.ConnectionManager.GetHubContext<Hubs.ConferenceHub>();
                context.Clients.addParticipant(participant.ConferenceSid, participant.CallSid, participant.Muted);
            }

            twilioResponse.DialConference(twilioRequest.Digits, new { waitUrl = "http://twimlets.com/holdmusic?Bucket=com.twilio.music.rock", endConferenceOnExit = true });
            twilioResponse.Redirect(Url.Link("ExtendedApi", new { controller = "Phone", action = "GoodByeMessage" }));

            return Request.CreateResponse(HttpStatusCode.OK, twilioResponse.Element);
        }
        public ActionResult voice2(string From)
        {
            Demo.LogNumber(From);

            var response = new TwilioResponse();
            response.Say("Welcome to 1 tug, conferencing you in");
            response.DialConference("onetug");

            Demo.Log("{0} joined the conference", From);

            return TwiML(response);
        }
        public TwiMLResult SecondClientReady(CallContextModel model)
        {
            var response = new TwilioResponse();

            try
            {
                model.CallerStatus = Request["CallStatus"];

                var message = string.Empty;

                // Get fromUser and toUser
                var fromUser = Services.Users.GetUserById(model.CallerId);
                var toUser = Services.Users.GetUserById(model.ReceiverId);

                if (model.IsCustomer) // Customer initiate call >> Second client and FromUser is customer
                {
                    // Check consultant is readly in conference
                    if (Services.Call.CheckCallInStatus(model.CallId, CallStatus.InProgress, true))
                    {
                        string template = ConferenceConst.SecondClientReadyForTalkNow;
                        message = string.Format(template, toUser.Name);

                        response.Say(message, new { voice = VoiceInConference, language = LanguageInConference });

                        // Get name conference room
                        var room = string.Format(ConferenceRoom, fromUser.UserName, toUser.UserName, model.BookingId);

                        // Update receiver call status
                        Services.Call.UpdateCallStatus(model.CallId, model.CallerStatus, true);

                        // Hangup Url
                        var callHangUpUrl = Utilities.FormatLink(Url.Action("CallHangUp", "Conference", model));
                        // Dial coference
                        response.DialConference(room,
                            new
                            {
                                endConferenceOnExit = true
                            },
                            new
                            {
                                record = true,
                                trim = "trim-silence",
                                transcribe = false,
                                action = callHangUpUrl
                            });
                    }
                    else
                    {
                        response.Say(ConferenceConst.ConsultantDenied, new { voice = VoiceInConference, language = LanguageInConference });
                    }
                }
                else
                {
                    string template = ConferenceConst.SecondClientReadyForSchedule;
                    message = string.Format(template, toUser.Name);
                    response.Say(message, new { voice = VoiceInConference, language = LanguageInConference });

                    // Get name conference room
                    var room = string.Format(ConferenceRoom, fromUser.UserName, toUser.UserName, model.BookingId); ;

                    // Dial coference
                    var callBackUrl = Utilities.FormatLink(Url.Action("CallBackAfterConference", "Conference", model));
                    response.DialConference(room,
                        new
                        {
                            endConferenceOnExit = true,
                            waitUrl = "http://twimlets.com/holdmusic?Bucket=com.twilio.music.electronica"
                        },
                        new
                        {
                            record = false,
                            transcribe = false,
                            action = callBackUrl
                        });

                    // Update receiver call status
                    Services.Call.UpdateCallStatus(model.CallId, model.CallerStatus, true);
                }
            }
            catch (Exception e)
            {
                Log.Error("Second client ready. Error: ", e);

                // Error
                response.Say(ConferenceConst.ErrorMessage, new { voice = VoiceInConference, language = LanguageInConference });
                response.Hangup();
            }

            return new TwiMLResult(response);
        }
        public TwiMLResult FirstClientConference(CallContextModel model)
        {
            var response = new TwilioResponse();

            try
            {
                // Get fromUser and toUser
                var caller = Services.Users.GetUserById(model.CallerId);
                var receiver = Services.Users.GetUserById(model.ReceiverId);
                var callBackUrl = Utilities.FormatLink(Url.Action("CallBackAfterConference", "Conference", model));

                if (model.IsCustomer) // Customer initiate call >> Consultant called first
                {
                    // Get name conference room
                    var room = string.Format(ConferenceRoom, caller.UserName, receiver.UserName, model.BookingId);

                    // Check customer is readly in conference
                    if (Services.Call.CheckCallInStatus(model.CallId, CallStatus.InProgress, false))
                    {
                        response.Say(ConferenceConst.CustomerReadyTakeCall, new { voice = VoiceInConference, language = LanguageInConference });

                        // Update start time conference
                        Services.Call.UpdateConferenceTime(model.CallId, true);

                        // Begin call duration show popup to user
                        CallHelper.CallDuration(Request["CallSid"], model.IsCustomer, Services, true);

                        // Start conference timer
                        CallHelper.StartConferenceTimer(model.CallerId, model.CallId, model.BookingId, room);

                        // Dial conference
                        response.DialConference(room,
                            new
                            {
                                endConferenceOnExit = true,
                                waitUrl = "http://twimlets.com/holdmusic?Bucket=com.twilio.music.electronica"
                            },
                            new
                            {
                                record = false,
                                transcribe = false,
                                action = callBackUrl
                            });
                    }
                    else
                    {
                        response.Say(ConferenceConst.CustomerDenied, new { voice = VoiceInConference, language = LanguageInConference });
                    }
                }
                else // Consultant initiate call >>  Customer called first
                {
                    response.Say(ConferenceConst.ConsultantReadyTakeCall, new { voice = VoiceInConference, language = LanguageInConference });
                    // Get name conference room
                    var room = string.Format(ConferenceRoom, caller.UserName, receiver.UserName, model.BookingId);

                    // Update start time conference
                    Services.Call.UpdateConferenceTime(model.CallId, true);

                    // Begin call duration show popup to user
                    CallHelper.CallDuration(Request["CallSid"], model.IsCustomer, Services, true);

                    // Begin conference timer
                    CallHelper.StartConferenceTimer(model.ReceiverId, model.CallId, model.BookingId, room);

                    // Dial conference
                    var callHangUpUrl = Utilities.FormatLink(Url.Action("CallHangUp", "Conference", model));
                    response.DialConference(room,
                        new
                        {
                            endConferenceOnExit = true
                        },
                        new
                        {
                            record = true,
                            trim = "trim-silence",
                            transcribe = false,
                            action = callHangUpUrl
                        });
                }
            }
            catch (Exception e)
            {
                Log.Error("First client join conference. Error: ", e);

                // Error
                response.Say(ConferenceConst.ErrorMessage, new { voice = VoiceInConference, language = LanguageInConference });
                response.Hangup();
            }

            return new TwiMLResult(response);
        }