-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
78 lines (67 loc) · 3.87 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using MailChimpSyncerSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MailChimpSyncerExampleApp
{
class Program
{
static async Task Main(string[] args)
{
//Create some new contacts to sync
Contact leslie = new Contact("Leslie", "Knope", "leslie.knope@parksdept.com");
List<Contact> contactsToSync = new List<Contact>
{
leslie,
new Contact("April", "Ludgate", "april.ludgate@parksdept.com"),
new Contact("Ron", "Swanson", "ron.swanson@parksdept.com"),
new Contact("Andy", "Dwyer", "andy.dwyer@parksdept.com"),
new Contact("Ann", "Perkins", "ann.perkins@parksdept.com"),
new Contact("Tom", "Haverford", "tom.haverford@parksdept.com"),
};
string tagName = "TestTag";
string listName = "LIST NAME GOES HERE";
//To create an instance of MailChimpSyncer, you need to pass in an API Key for your account, which can be generated
//through MailChimp (Account > Extras > API Keys)
MailChimpSyncer mailChimpSyncer = new MailChimpSyncer("API KEY GOES HERE", (message, isError) => Output(message, isError));
//Add the new contacts
Output("Updating MailChimp...");
await mailChimpSyncer.UpdateMailChimp(contactsToSync, tagName, listName);
//Now make some changes to the list of contacts and resync. MailChimpSyncer will verify that these changes have been made before
//returning successfully
contactsToSync.Remove(leslie); //This contact will be untagged
contactsToSync.Single(x => x.LastName == "Ludgate").LastName = "Ludgate-Dwyer"; //This contact will remain tagged, and have its last name updated
Output("Updating MailChimp again...");
await mailChimpSyncer.UpdateMailChimp(contactsToSync, tagName, listName);
//Finally, re-add a contact to check that we can add tags to existing contacts
contactsToSync.Add(leslie);
contactsToSync.Add(new Contact("Burt", "Macklin", "hdsjkfnd@sjknjkadd")); //Also try and add an invalid email address
Output("Updating one more time...");
MailChimpUpdateReport report = await mailChimpSyncer.UpdateMailChimp(contactsToSync, tagName, listName);
//Note that MailChimpSyncer also returns a report about which email addresses were invalid, which emails already existed in MailChimp but
//were unsubscribed, and which emails had previously been permanently deleted from the MailChimp audience and so can not be re-added. For
//example, we would expect there to have been 1 invalid email in the last sync:
Output($"The last sync contained {report.InvalidEmails.Count} invalid email that was ignored by MailChimp");
Output("");
Output("Done!");
Output("Press enter to exit...");
Console.ReadLine();
//Note it is also possible to set up additional fields in MailChimp and then set those fields through MailChimpSyncer. For
//example, if you've set up additional text fields in Mailchimp with Merge Tags "CITY" and "JOB" you can set those fields
//when creating a Contact as follows:
//
//new Contact("Ben", "Wyatt", "ben.wyatt@parksdept.com", ("CITY", "Partridge"), ("JOB", "Mayor"));
}
static void Output(string message, bool isTerminal = false)
{
Console.WriteLine(message);
if (isTerminal)
{
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
Environment.Exit(0);
}
}
}
}