I am trying to test out the Message Store Export (kinda new at this) and am running into some errors. I am using the code from the documentation here. I already replaced the depreciated WebClient with HTTP but am still getting this error:
Error CS1061 'Index' does not contain a definition for 'List' and no accessible extension method 'List' accepting a first argument of type 'Index' could be found (are you missing a using directive or an assembly reference?)
I do have the RingCentral SDK installed. Any help would be appreciated!
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using RingCentral;
namespace Export_MessageStore
{
class Program
{
static RestClient restClient;
static HttpClient httpClient = new HttpClient();
static async Task Main(string[] args)
{
try
{
// Instantiate the SDK
restClient = new RestClient("SANDBOX-APP-CLIENTID", "SANDBOX-APP-CLIENTSECRET", "https://platform.devtest.ringcentral.com");
// Authenticate a user using a personal JWT token
await restClient.Authorize("SANDBOX_JWT");
await CreateMessageStoreReport();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static private async Task CreateMessageStoreReport()
{
var bodyParams = new CreateMessageStoreReportRequest
{
dateFrom = "2023-03-01T00:00:00.000Z",
dateTo = "2023-03-31T23:59:59.999Z"
};
var response = await restClient.Restapi().Account().MessageStoreReport().Post(bodyParams);
await GetMessageStoreReportTask(response.id);
}
static private async Task GetMessageStoreReportTask(string taskId)
{
bool isCompleted = false;
while (!isCompleted)
{
var response = await restClient.Restapi().Account().MessageStoreReport(taskId).Get();
Console.WriteLine("Task creation status: " + response.status);
switch (response.status)
{
case "Completed":
isCompleted = true;
await GetMessageStoreReportArchive(taskId);
break;
case "AttemptFailed":
case "Failed":
case "Cancelled":
Console.WriteLine("Export message store failed.");
return;
default:
await Task.Delay(10000); // Use non-blocking delay
break;
}
}
}
static private async Task GetMessageStoreReportArchive(string taskId)
{
Console.WriteLine("Getting report uri ...");
var resp = await restClient.Restapi().Account().MessageStoreReport(taskId).Archive().List();
var dateStr = DateTime.Now.ToString("yyyy-MM-dd-HH_mm");
for (var i = 0; i < resp.records.Length; i++)
{
var fileName = $"message_store_content_{dateStr}_{i}.zip";
var contentUrl = $"{resp.records[i].uri}?access_token={restClient.token.access_token}";
// Use HttpClient for downloading the file
var response = await httpClient.GetAsync(contentUrl);
if (response.IsSuccessStatusCode)
{
using (var stream = await response.Content.ReadAsStreamAsync())
using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
await stream.CopyToAsync(fileStream);
}
Console.WriteLine($"{fileName} file is saved to the local machine.");
}
else
{
Console.WriteLine($"Failed to download file: {fileName}");
}
}
}
}
}