Writes bounced users to file

This commit is contained in:
Josh Deck 2024-11-11 11:27:08 -05:00
parent 29b6e62494
commit 36aa06499c
1 changed files with 31 additions and 44 deletions

View File

@ -11,23 +11,11 @@ using RestSharp.Authenticators;
internal class Program
{
private static string ListID = "3bb5109cdf3265eac26b3fa34ffae379";
static void Main(string[] args)
{
Console.Write("Date to start at (leave blank for all entries): ");
// Add to list testing
//var data = AddNewSubscribers("");
//Console.WriteLine(data);
// Unsubscribe testing
//UnsubscribeUser("");
// Opt Out Testing
string date = Console.ReadLine();
var data = GetOptOuts(date);
// Output the data; TODO replace with writing to a service or something
Console.WriteLine(data);
GetBouncedUsers();
}
@ -46,6 +34,7 @@ internal class Program
private static JsonNode? GetOptOuts(string fromDate)
{
// Just outputs, need to revise possibly? who knows?
var client = InitializeAndGetClient();
var request = new RestRequest();
if (fromDate != "")
@ -60,36 +49,34 @@ internal class Program
private static JsonNode? AddNewSubscribers(string fileName)
// GET ALL BOUNCED PEOPLE AT ONCE; https://www.campaignmonitor.com/api/v3-3/lists/#bounced-subscribers-2
// Output files are sent to {CurrentWorkingDirectory}\OptOutFetcher\bin\Debug\net8.0\Output
private static void GetBouncedUsers()
{
// TODO: for testing we just use a dummy list, but once we actually merge the two lists we are going to want to read from file/service
string add_request_params = "{ \"EmailAddress\": \"joshdeck@keymotive.net\", \"Name\": \"Joshua Deck\",\"MobileNumber\":\"+8884227390\",\"CustomFields\": [ ], \"Resubscribe\": true,\"RestartSubscriptionBasedAutoresponders\": true, \"ConsentToTrack\": \"No\", \"ConsentToSendSms\":\"No\"}";
var client = InitializeAndGetClient();
// This is only adding one. To add multiple, format body as: https://www.campaignmonitor.com/api/v3-3/subscribers/#importing-many-subscribers
// and POST this url: https://api.createsend.com/api/v3.3/subscribers/3bb5109cdf3265eac26b3fa34ffae379/import.json
var request = new RestRequest("api/v3.3/subscribers/3bb5109cdf3265eac26b3fa34ffae379.json");
// Get total number of bounces so we know how many pages (of 10k entries) to export
var request = new RestRequest(string.Format("api/v3.3/lists/{0}/stats.json", ListID));
var response = client.ExecuteGet(request);
var dict = JsonSerializer.Deserialize<Dictionary<string, dynamic>>(response.Content);
JsonElement countJson = dict["TotalBounces"];
var count = -1;
countJson.TryGetInt32(out count);
Console.WriteLine(count);
request.AddBody(add_request_params);
var response = client.ExecutePost(request);
// Iterate over all bounces and write to file
var pageCount = count / 1000;
for (int pagesLeft = 1 + count / 1000; pagesLeft > 0; pagesLeft--)
{
var currentPageNumber = pageCount - pagesLeft + 1;
request = new RestRequest(string.Format("https://api.createsend.com/api/v3.3/lists/{0}/bounced.json?page={1}&pagesize=1000&orderfield=date&orderdirection=asc", ListID, "" + currentPageNumber));
response = client.ExecuteGet(request);
//string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
using (StreamWriter outputFile = new StreamWriter(Path.Combine("Output", string.Format("BouncedUsersPage_{0}", "" + currentPageNumber))))
{
outputFile.WriteLine(response.Content);
}
}
return JsonSerializer.Deserialize<JsonNode>(response.Content!)!;
}
private static void UnsubscribeUser(string fileName)
{
// TODO: change this to a file
string unsub_request_params = "{ \"EmailAddress\": \"joshdeck@keymotive.net\" }";
var client = InitializeAndGetClient();
var request = new RestRequest("api/v3.3/subscribers/3bb5109cdf3265eac26b3fa34ffae379/unsubscribe.json");
request.AddBody(unsub_request_params);
var response = client.ExecutePost(request);
Console.WriteLine(response.Content + "Done unsubscribing");
}
}