protected override void Execute(NativeActivityContext context) { try { Init(context); var text = From.Get(context); var text2 = To.Get(context); var dateSentBefore = DateSentBefore.Get(context); var dateSent = DateSent.Get(context); var dateSentAfter = DateSentAfter.Get(context); var num = Limit.Get(context); ReadMessageOptions readMessageOptions = new ReadMessageOptions(); if (!string.IsNullOrWhiteSpace(text)) { readMessageOptions.From = text; } if (!string.IsNullOrWhiteSpace(text2)) { readMessageOptions.To = text2; } readMessageOptions.DateSentBefore = dateSentBefore; readMessageOptions.DateSent = dateSent; readMessageOptions.DateSentAfter = dateSentAfter; readMessageOptions.Limit = ((num < 1) ? 1 : num); ResourceSet <MessageResource> resourceSet = MessageResource.Read(readMessageOptions); var dataTable = CriarDataTable(); foreach (MessageResource item in resourceSet) { dataTable.Rows.Add(item.DateCreated, item.DateUpdated, item.DateSent, item.Direction.ToString(), item.ErrorCode, item.ErrorMessage, item.From, item.Sid, item.Status.ToString(), item.To, item.Body); } Sucess.Set(context, value: true); Result.Set(context, dataTable); } catch (Exception ex) { Sucess.Set(context, value: false); ErrorMessage.Set(context, ex.Message); } }
public static async Task <ResourceSet <MessageResource> > GetMessagesAsync(ITwilioRestClient client, string from = null, string to = null, string accountSid = null, DateTime?dateSent = null, DateTime?dateSentAfter = null, DateTime?dateSentBefore = null, long?limit = null) { var fromNumber = from != null ? new PhoneNumber(from) : null; var toNumber = to != null ? new PhoneNumber(to) : null; var options = new ReadMessageOptions { PathAccountSid = accountSid, DateSent = dateSent, DateSentAfter = dateSentAfter, DateSentBefore = dateSentBefore, From = fromNumber, Limit = limit, To = toNumber, PageSize = null }; return(await MessageResource.ReadAsync(options, client)); }
static void Main(string[] args) { var dateToRun = args.Length >= 1 ? args[0] : DateTime.Now.AddDays(-1).ToShortDateString(); var fileName = $"twilio-logs.csv"; Console.WriteLine($"Fetching calls for {dateToRun}"); var path = $"./{fileName}"; if (!System.IO.File.Exists(path)) { using (System.IO.FileStream fs = System.IO.File.Create(path)){ Console.WriteLine("File Written"); } } using (var writer = new StreamWriter(fileName)) using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) { TwilioClient.Init(accountSid, authToken); var opts = new ReadMessageOptions() { PageSize = 100000, DateSentAfter = DateTime.Parse($"{dateToRun} 00:00:00") }; var messages = MessageResource.Read(opts); var exportRecords = messages.Select(x => new TwilioExportRecord { datecreated = x.DateCreated.ToString(), datesent = x.DateSent.ToString(), lastupdated = x.DateUpdated.ToString(), sid = x.Sid, status = x.Status.ToString(), url = x.Uri.ToString(), to = x.To.ToString() }); csv.WriteRecords(exportRecords); } }
public List <MessageResource> GetMessagesFromTwilio(PluginConfig pluginConfig, int secondsAgo) { this.Log.LogDebug("Starting GetMessagesFromTwilio"); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; TwilioClient.Init(pluginConfig.AccountSID, pluginConfig.AuthToken); ReadMessageOptions options = new ReadMessageOptions { To = new PhoneNumber(pluginConfig.FromNumber), DateSentAfter = DateTime.Now.AddSeconds(secondsAgo * -1) }; ResourceSet <MessageResource> messages = MessageResource.Read(options); foreach (MessageResource message in messages) { string from = message.From.ToString(); string body = message.Body; string created = message.DateCreated.GetValueOrDefault(DateTime.MinValue).ToLongDateString(); this.Log.LogDebug(string.Format("From: {0}, created: {1}, body: {2}", from, created, body)); } return(messages.Where((message) => message.DateCreated.GetValueOrDefault(DateTime.MinValue).CompareTo(options.DateSentAfter) >= 0).ToList()); }