Reputation: 21
Good day all, I'm in the process of trying to retrieve call data from Twilio using their C# SDK. Using the StartTime seems to pull all records for that given date which is not what I want to achieve. Ultimately I want to only pull data within a time range of the given date. I have looked on the internet and have seen mention of using the StartTimeBefore and StartTimeAfter. This does not appear to work either. Any thoughts as to how I can achieve this? Below is some code that I have attempted that does not seem to work.
NOTE: I have also tried some different permutations with EndTimeAfter to put bounds on it that way as well, but again that did not work.
public class RetrieveTwilioCalls
{
private readonly string _accountSid;
private readonly string _authToken;
public RetrieveTwilioCalls(IConfigurationRoot configuration)
{
_accountSid = configuration["TWILIO_ACCOUNT_SID"];
_authToken = configuration["TWILIO_AUTH_TOKEN"];
}
public async Task ExecuteAsync()
{
TwilioClient.Init(_accountSid, _authToken);
var startDateTimeUTC = new DateTime(2022, 2, 8, 15, 15, 00).ToUniversalTime();
var endDateTimeUTC = new DateTime(2022, 2, 8, 16, 18, 01).ToUniversalTime();
var call1 = await CallResource.ReadAsync(new ReadCallOptions { StartTimeBefore = startDateTimeUTC, EndTimeAfter = endDateTimeUTC, PageSize = 100 });
Console.WriteLine(call1.ElementAt(0).To);
}
}
Upvotes: 2
Views: 457
Reputation: 73027
Twilio developer evangelist here.
If you check out the documentation for listing calls you will see the StartTime
and EndTime
filters do not work down to the granularity level of time.
StartTime
Only include calls that started on this date. Specify a date as
YYYY-MM-DD
in GMT, for example: 2009-07-06, to read only calls that started on this date. You can also specify an inequality, such asStartTime<=YYYY-MM-DD
, to read calls that started on or before midnight of this date, andStartTime>=YYYY-MM-DD
to read calls that started on or after midnight of this date.
If you need to specify things down to times, then I recommend you fetch the calls based on the date and then filter the calls to the right time in your own code.
Upvotes: 0
Reputation: 2576
If you want to get all calls between two dates, you need to use the StartTimeAfter and StartTimeBefore properties.
StartTimeAfter is the date and time you want to be the start of your time span. StartTimeBefore is the date and time you want to be the end of your time span.
public async Task ExecuteAsync()
{
TwilioClient.Init(_accountSid, _authToken);
var startDateTimeUTC = new DateTime(2021, 11, 17, 15, 15, 00).ToUniversalTime();
var endDateTimeUTC = new DateTime(2021, 11, 19, 16, 18, 01).ToUniversalTime();
var call1 = await CallResource.ReadAsync(
new ReadCallOptions
{
StartTimeAfter = startDateTimeUTC,
StartTimeBefore = endDateTimeUTC,
PageSize = 100
});
foreach (var cr in call1)
{
Console.WriteLine($"{cr.Direction} ${cr.DateCreated} ${cr.CallerName} ${cr.Duration}");
}
}
This will print out a list of all calls inbound or outbound between 2021-11-17 15:15:00 and 2021-11-19 16:18:01 UTC
Upvotes: 1