private static async Task <bool> HasChildren(CallResource call, TwilioRestClient client) { if (!string.IsNullOrEmpty(call.ParentCallSid)) { throw new ArgumentException($"Child call {call.Sid} isn't expected."); } var children = await CallResource.ReadAsync(new ReadCallOptions { ParentCallSid = call.Sid }, client); return(children.Any()); }
public async Task CallsPagingTest(int numberOfCalls, int pageSize) { await ClearDatabase(); var callSids1 = new List <string>(); var callSids2 = new List <string>(); for (int i = 0; i < numberOfCalls; i++) { var call = await CallResource.CreateAsync(TEST_TO_NUMBER, TEST_FROM_NUMBER); callSids1.Insert(0, call.Sid); callSids2.Insert(0, call.Sid); await Task.Delay(1); } var callsSet1 = await CallResource.ReadAsync(pageSize : pageSize); // we now have the first page in memory. add a newer calls to make sure this does not disturb the existing pagination for (int i = 0; i < 3; i++) { var call = await CallResource.CreateAsync(TEST_TO_NUMBER, TEST_FROM_NUMBER); callSids2.Insert(0, call.Sid); await Task.Delay(1); } var calls1 = callsSet1.ToList(); Assert.Collection(calls1, callSids1.Select <string, Action <CallResource> >(sid => { return((CallResource call) => Assert.Equal(sid, call.Sid)); }).ToArray()); var callsSet2 = await CallResource.ReadAsync(pageSize : pageSize); var calls2 = callsSet2.ToList(); Assert.Collection(calls2, callSids2.Select <string, Action <CallResource> >(sid => { return((CallResource call) => Assert.Equal(sid, call.Sid)); }).ToArray()); }
public async System.Threading.Tasks.Task SaveRecordings() { var recordingurl = Request["RecordingUrl"]; var callsid = Request["CallSid"]; var callfrom = Request["From"]; var recordingsid = Request["RecordingSid"]; var fromcode = Request["CallerCountry"]; var tocode = Request["ToCountry"]; try { TwilioClient.Init(ConfigurationManager.AppSettings["TwilioAccountSid"], ConfigurationManager.AppSettings["TwilioAuthToken"]); var calls = await CallResource.ReadAsync(parentCallSid : (callsid)); if (calls.Any()) { int?patientid = 0; foreach (var call in calls) { var callHistory = _db.CallHistories.Where(x => x.TwilioCallId == call.ParentCallSid).FirstOrDefault(); if (callHistory != null) { try { callHistory.From = callHistory.From.Replace("+1", ""); patientid = _db.Patients.AsNoTracking().Where(x => (x.MobilePhoneNumber != null && x.MobilePhoneNumber.Replace("", "").Contains(callHistory.From)) || (x.WorkPhoneNumber != null && x.WorkPhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.EmergencyNumber != null && x.EmergencyNumber.Replace(" ", "").Contains(callHistory.From)) || (x.HomePhoneNumber != null && x.HomePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.CaretakerPhoneNumber != null && x.CaretakerPhoneNumber.Replace(" ", "").Contains(callHistory.From))).FirstOrDefault()?.Id; if (patientid == null) { patientid = _db.PatientProfile_Contact.AsNoTracking().Where(x => (x.CellPhoneNumber != null && x.CellPhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.CellPhoneNumber1 != null && x.CellPhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.CellPhoneNumber2 != null && x.CellPhoneNumber2.Replace(" ", "").Contains(callHistory.From)) || (x.EmergencyNumber != null && x.EmergencyNumber.Replace(" ", "").Contains(callHistory.From)) || (x.EmergencyNumber1 != null && x.EmergencyNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.EmergencyNumber2 != null && x.EmergencyNumber2.Replace(" ", "").Contains(callHistory.From)) || (x.HomePhoneNumber != null && x.HomePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.HomePhoneNumber1 != null && x.HomePhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.HomePhoneNumber2 != null && x.HomePhoneNumber2.Replace(" ", "").Contains(callHistory.From)) || (x.WorkPhoneNumber != null && x.WorkPhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.WorkPhoneNumber1 != null && x.WorkPhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.WorkPhoneNumber2 != null && x.WorkPhoneNumber2.Replace(" ", "").Contains(callHistory.From))).FirstOrDefault()?.PatientId; if (patientid == null) { patientid = _db.PatientProfile_UrgencyContacts.AsNoTracking().Where(x => (x.PrimaryHomePhoneNumber != null && x.PrimaryHomePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.PrimaryHomePhoneNumber1 != null && x.PrimaryHomePhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.PrimaryHomePhoneNumber2 != null && x.PrimaryHomePhoneNumber2.Replace(" ", "").Contains(callHistory.From)) || (x.PrimaryMobilePhoneNumber != null && x.PrimaryMobilePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.PrimaryMobilePhoneNumber1 != null && x.PrimaryMobilePhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.PrimaryMobilePhoneNumber2 != null && x.PrimaryMobilePhoneNumber2.Replace(" ", "").Contains(callHistory.From)) || (x.SecondaryHomePhoneNumber != null && x.SecondaryHomePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.SecondaryHomePhoneNumber1 != null && x.SecondaryHomePhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.SecondaryHomePhoneNumber2 != null && x.SecondaryHomePhoneNumber2.Replace(" ", "").Contains(callHistory.From)) || (x.SecondaryMobilePhoneNumber != null && x.SecondaryMobilePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.SecondaryMobilePhoneNumber1 != null && x.SecondaryMobilePhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.SecondaryMobilePhoneNumber2 != null && x.SecondaryMobilePhoneNumber2.Replace(" ", "").Contains(callHistory.From))).FirstOrDefault()?.PatientId; if (patientid == null) { patientid = _db.CallHistories.AsNoTracking().Where(x => x.To.Replace(" ", "").Contains(callHistory.From)).FirstOrDefault()?.PatientID; } } } if (patientid == null) { patientid = 0; } } catch (Exception ex) { WriteErrorLog(ex); } callHistory.StartTime = call?.StartTime; callHistory.EndTime = call?.EndTime; callHistory.Status = call?.Status.ToString(); callHistory.Duration = call?.StartTime != null && call.EndTime != null ? (TimeSpan)(call.EndTime - call.StartTime) : TimeSpan.Zero; callHistory.PatientID = callHistory.PatientID > 0 ? callHistory.PatientID : patientid.Value; } ; //var recordings = await RecordingResource.ReadAsync(callSid: call?.ParentCallSid); //if (recordings.Any()) //{ // foreach (var recording in recordings) // callHistory.RecordingURL = recording?.Uri; // var src = callHistory.RecordingURL.Substring(0, callHistory.RecordingURL.IndexOf('.')); // var fullSource = "https://api.twilio.com" + src + ".mp3"; // WebClient webClient = new WebClient(); // string physicalPath = Server.MapPath("~/Recordings/" + callHistory.TwilioCallId + ".mp3"); // webClient.DownloadFile(fullSource, physicalPath); //} try { callHistory.RecordingURL = recordingurl; var src = recordingurl; var fullSource = src + ".mp3"; WebClient webClient = new WebClient(); string physicalPath = Server.MapPath("~/Recordings/" + callHistory.TwilioCallId + ".mp3"); webClient.DownloadFile(fullSource, physicalPath); } catch (Exception ex) { WriteErrorLog(ex); } _db.Entry(callHistory).State = EntityState.Modified; _db.SaveChanges(); } } } catch (Exception ex) { WriteErrorLog(ex); } }
public async System.Threading.Tasks.Task <ActionResult> VoiceMail() { var recordingurl = Request["RecordingUrl"]; var callsid = Request["CallSid"]; var recordingsid = Request["RecordingSid"]; var fromcode = Request["CallerCountry"]; var tocode = Request["ToCountry"]; try { TwilioClient.Init(ConfigurationManager.AppSettings["TwilioAccountSid"], ConfigurationManager.AppSettings["TwilioAuthToken"]); var calls = await CallResource.ReadAsync(parentCallSid : (callsid)); if (calls.Any()) { int?patientid = 0; foreach (var call in calls) { var callHistory = _db.CallHistories.AsNoTracking().Where(x => x.TwilioCallId == call.ParentCallSid).FirstOrDefault(); try { //callHistory.From = callHistory.From.Replace("+1", ""); patientid = _db.Patients.AsNoTracking().Where(x => (x.MobilePhoneNumber != null && x.MobilePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.WorkPhoneNumber != null && x.WorkPhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.EmergencyNumber != null && x.EmergencyNumber.Replace(" ", "").Contains(callHistory.From)) || (x.HomePhoneNumber != null && x.HomePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.CaretakerPhoneNumber != null && x.CaretakerPhoneNumber.Replace(" ", "").Contains(callHistory.From))).FirstOrDefault()?.Id; if (patientid == null) { patientid = _db.PatientProfile_Contact.AsNoTracking().Where(x => (x.CellPhoneNumber != null && x.CellPhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.CellPhoneNumber1 != null && x.CellPhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.CellPhoneNumber2 != null && x.CellPhoneNumber2.Replace(" ", "").Contains(callHistory.From)) || (x.EmergencyNumber != null && x.EmergencyNumber.Replace(" ", "").Contains(callHistory.From)) || (x.EmergencyNumber1 != null && x.EmergencyNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.EmergencyNumber2 != null && x.EmergencyNumber2.Replace(" ", "").Contains(callHistory.From)) || (x.HomePhoneNumber != null && x.HomePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.HomePhoneNumber1 != null && x.HomePhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.HomePhoneNumber2 != null && x.HomePhoneNumber2.Replace(" ", "").Contains(callHistory.From)) || (x.WorkPhoneNumber != null && x.WorkPhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.WorkPhoneNumber1 != null && x.WorkPhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.WorkPhoneNumber2 != null && x.WorkPhoneNumber2.Replace(" ", "").Contains(callHistory.From))).FirstOrDefault()?.PatientId; if (patientid == null) { patientid = _db.PatientProfile_UrgencyContacts.AsNoTracking().Where(x => (x.PrimaryHomePhoneNumber != null && x.PrimaryHomePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.PrimaryHomePhoneNumber1 != null && x.PrimaryHomePhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.PrimaryHomePhoneNumber2 != null && x.PrimaryHomePhoneNumber2.Replace(" ", "").Contains(callHistory.From)) || (x.PrimaryMobilePhoneNumber != null && x.PrimaryMobilePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.PrimaryMobilePhoneNumber1 != null && x.PrimaryMobilePhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.PrimaryMobilePhoneNumber2 != null && x.PrimaryMobilePhoneNumber2.Replace(" ", "").Contains(callHistory.From)) || (x.SecondaryHomePhoneNumber != null && x.SecondaryHomePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.SecondaryHomePhoneNumber1 != null && x.SecondaryHomePhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.SecondaryHomePhoneNumber2 != null && x.SecondaryHomePhoneNumber2.Replace(" ", "").Contains(callHistory.From)) || (x.SecondaryMobilePhoneNumber != null && x.SecondaryMobilePhoneNumber.Replace(" ", "").Contains(callHistory.From)) || (x.SecondaryMobilePhoneNumber1 != null && x.SecondaryMobilePhoneNumber1.Replace(" ", "").Contains(callHistory.From)) || (x.SecondaryMobilePhoneNumber2 != null && x.SecondaryMobilePhoneNumber2.Replace(" ", "").Contains(callHistory.From))).FirstOrDefault()?.PatientId; if (patientid == null) { patientid = _db.CallHistories.AsNoTracking().Where(x => x.To.Replace(" ", "").Contains(callHistory.From)).FirstOrDefault()?.PatientID; } } } if (patientid == null) { patientid = 0; } } catch (Exception ex) { } var callHistory1 = new VoiceMailHistory() { StartTime = call?.StartTime, EndTime = call?.EndTime, Status = call?.Status.ToString(), Duration = call?.StartTime != null && call.EndTime != null ? (TimeSpan)(call.EndTime - call.StartTime) : TimeSpan.Zero, To = callHistory?.To, From = callHistory?.From, PatientID = patientid.Value, TwilioCallId = call?.ParentCallSid, LiaisonId = callHistory.LiaisonId }; try { callHistory1.RecordingURL = recordingurl; var src = recordingurl; var fullSource = src + ".mp3"; WebClient webClient = new WebClient(); string physicalPath = Server.MapPath("~/VoiceMails/" + callHistory1.TwilioCallId + ".mp3"); webClient.DownloadFile(fullSource, physicalPath); } catch (Exception ex) { WriteErrorLog(ex); } _db.voiceMailHistories.Add(callHistory1); _db.SaveChanges(); } } } catch (Exception ex) { WriteErrorLog(ex); } var response = new VoiceResponse(); response.Append(new Hangup()); return(TwiML(response)); }
public async Task <string> CallMeetingAsync(string mNum) { Console.WriteLine(">\tDialing into webex meeting with access code " + mNum); TwilioClient.Init(_accountSid, _authToken); // call in number and call from number const string vancouverTollNum = "+14084189388"; const string twilioAccNum = "+15046366992"; // this is the webex call vancouver toll number var to = new PhoneNumber(vancouverTollNum); // This is the twilio number linked to our account var from = new PhoneNumber(twilioAccNum); Console.WriteLine(">\tCalling In..."); // makes the call resource to send var call = CallResource.Create(to, from, //method: Twilio.Http.HttpMethod.Get, sendDigits: FormatDigits(mNum) + "wwww#", // Records the outgoing call record: true, // default message that plays from the url url: new Uri("http://lonelycompany.ca/test.xml") ); var callSid = call.Sid; // set status to default value CallResource.StatusEnum status = CallResource.StatusEnum.Queued; // for checking what Sid was used in the loop CallResource finishedCall = null; // wait for meeting to finish while (status != CallResource.StatusEnum.Completed) { // return all the call resources for account user var calls = CallResource.ReadAsync().Result; // find element in list finishedCall = calls.First(record => record.Sid == callSid); var pendingStatus = finishedCall.Status; if (pendingStatus == CallResource.StatusEnum.Ringing) { Console.WriteLine(">\tRinging..."); status = pendingStatus; } else if (pendingStatus == CallResource.StatusEnum.InProgress && status == pendingStatus) { Console.WriteLine(">\tCalled in!"); status = pendingStatus; } // check if the record is if (pendingStatus == CallResource.StatusEnum.Completed) { Console.WriteLine(">\tCall Completed"); // if the call has been completed return the completed status status = pendingStatus; } else if (status == CallResource.StatusEnum.Canceled || status == CallResource.StatusEnum.Failed || status == CallResource.StatusEnum.NoAnswer) { Console.Error.WriteLine(">\tThe call was not completed."); break; } } //var subresourceUri = finishedCall.SubresourceUris; // retrieve 10 most recent recordings var recordings = RecordingResource.Read(limit: 10); var resultRecording = recordings.First(recording => recording.CallSid == callSid); //Console.WriteLine("The recording call sid is: " + resultRecording.CallSid); //Console.WriteLine("The recording sid is: " + resultRecording.Sid); return(resultRecording.Sid); }