OptOutFetcher/Program.cs

95 lines
3.1 KiB
C#

using RestSharp;
using System.Text.Json.Nodes;
using System.Text.Json;
using System.Net;
using RestSharp.Authenticators;
/*
Campaign Monitor API reference: https://www.campaignmonitor.com/api/v3-3/getting-started/
Dobbs API key/client ID available from Josh in \\KM202401\c$\Users\joshdeck.KEYMTV\Documents\Misc\OptOutFetcher
*/
internal class Program
{
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);
}
private static RestClient InitializeAndGetClient()
{
var cookieJar = new CookieContainer();
var options = new RestClientOptions("https://api.createsend.com")
{
Authenticator = new HttpBasicAuthenticator("7cb8625ed34fbc298521f66b9e491ec3df53859a892c92d3", "x")
};
return new RestClient(options);
}
private static JsonNode? GetOptOuts(string fromDate)
{
var client = InitializeAndGetClient();
var request = new RestRequest();
if (fromDate != "")
request = new RestRequest("api/v3.3/lists/3bb5109cdf3265eac26b3fa34ffae379/unsubscribed.json?date=" + fromDate);
else
request = new RestRequest("api/v3.3/lists/3bb5109cdf3265eac26b3fa34ffae379/unsubscribed.json");
var response = client.ExecuteGet(request);
return JsonSerializer.Deserialize<JsonNode>(response.Content!)!;
}
private static JsonNode? AddNewSubscribers(string fileName)
{
// 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");
request.AddBody(add_request_params);
var response = client.ExecutePost(request);
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");
}
}