Writes bounced users to file
This commit is contained in:
		
							parent
							
								
									29b6e62494
								
							
						
					
					
						commit
						36aa06499c
					
				
							
								
								
									
										69
									
								
								Program.cs
								
								
								
								
							
							
						
						
									
										69
									
								
								Program.cs
								
								
								
								
							| 
						 | 
					@ -11,23 +11,11 @@ using RestSharp.Authenticators;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
internal class Program
 | 
					internal class Program
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						private static string ListID = "3bb5109cdf3265eac26b3fa34ffae379";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	static void Main(string[] args)
 | 
						static void Main(string[] args)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		Console.Write("Date to start at (leave blank for all entries): ");
 | 
							GetBouncedUsers();
 | 
				
			||||||
 | 
					 | 
				
			||||||
		// 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);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -46,6 +34,7 @@ internal class Program
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static JsonNode? GetOptOuts(string fromDate)
 | 
						private static JsonNode? GetOptOuts(string fromDate)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
							// Just outputs, need to revise possibly? who knows?
 | 
				
			||||||
		var client = InitializeAndGetClient();
 | 
							var client = InitializeAndGetClient();
 | 
				
			||||||
		var request = new RestRequest();
 | 
							var request = new RestRequest();
 | 
				
			||||||
		if (fromDate != "")
 | 
							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();
 | 
							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
 | 
							// Get total number of bounces so we know how many pages (of 10k entries) to export
 | 
				
			||||||
		// and POST this url: https://api.createsend.com/api/v3.3/subscribers/3bb5109cdf3265eac26b3fa34ffae379/import.json
 | 
							var request = new RestRequest(string.Format("api/v3.3/lists/{0}/stats.json", ListID));
 | 
				
			||||||
		var request = new RestRequest("api/v3.3/subscribers/3bb5109cdf3265eac26b3fa34ffae379.json");
 | 
							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);
 | 
							// Iterate over all bounces and write to file
 | 
				
			||||||
		var response = client.ExecutePost(request);
 | 
							var pageCount = count / 1000;
 | 
				
			||||||
 | 
							for (int pagesLeft = 1 + count / 1000; pagesLeft > 0; pagesLeft--)
 | 
				
			||||||
		return JsonSerializer.Deserialize<JsonNode>(response.Content!)!;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	private static void UnsubscribeUser(string fileName)
 | 
					 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
		// TODO: change this to a file
 | 
								var currentPageNumber = pageCount - pagesLeft + 1;
 | 
				
			||||||
		string unsub_request_params = "{ \"EmailAddress\": \"joshdeck@keymotive.net\" }";
 | 
								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);
 | 
				
			||||||
		var client = InitializeAndGetClient();
 | 
								//string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
 | 
				
			||||||
		var request = new RestRequest("api/v3.3/subscribers/3bb5109cdf3265eac26b3fa34ffae379/unsubscribe.json");
 | 
								using (StreamWriter outputFile = new StreamWriter(Path.Combine("Output", string.Format("BouncedUsersPage_{0}", "" + currentPageNumber))))
 | 
				
			||||||
		request.AddBody(unsub_request_params);
 | 
								{
 | 
				
			||||||
		var response = client.ExecutePost(request);
 | 
										outputFile.WriteLine(response.Content);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		Console.WriteLine(response.Content + "Done unsubscribing");
 | 
							}
 | 
				
			||||||
 | 
							
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue