Mailchimp APIs and Xojo

Hello !

i’m trying to automate our mailing list subscriptions with Xojo. I got some goals but still i’m not able to manage a single email registration…

My code looks like this…


Socket.Secure = True

Dim emailData As New Dictionary
emailData.Value("email") = "theNewEmail@gmail.com"

Dim addEmailRequest As New Dictionary
addEmailRequest.Value("apikey") = "myAPIKey"
addEmailRequest.Value("id") = "myListId"
addEmailRequest.Value("email") = emailData

Socket.SetFormData(addEmailRequest)
Socket.Post("https://us3.api.mailchimp.com/2.0/lists/subscribe/")

i use another dictionary for the email fields as Mailchimp needs it to be an associative array (API returned error says “Please enter a struct/associative array”) but
in this way i get a TypeMismatchException when trying to set the form data…

Anyone had luck in this and willing to share some ideas ?

Thanks !

Hi Matteo,

Did you get this to work?

I am about to attempt updating Mailchimp from Xojo as at the moment I use a another piece of software.

Cheers,

Paul.

Sorry Paul, I gave up on that.

Have you guys thought of using services like Temboo ?

Just a heads up for those that don’t know we have open sourced TembooKit: GitHub - 1701software/TembooKit: Xojo modules for using Temboo.com.

It has a great foundation for interacting with Temboo. It doesn’t support every service they support but its extremely easy to add new services on. If you add additional services please issue a pull request!

I wrote a mail chimp integration back in the day for a product we still occasionally sell. I’ll flag this post and see what I can share tomorrow - I’m off to the gym now.

Thanks for the responses.

I will have a look at the team boo and the temboo kit but if you get a chance Brock, any examples of basic integration would be great.

Cheers,

Paul

Same here ! And thanks so much to both Phillip and Patrick for sharing that informations :slight_smile:

The problem is that the Email Data has to be a JSON item - not a Xojo dictionary
Probably more like (I’d hard code one to start with)

[code]Socket.Secure = True

// note this REALLY probably should be a JSONitem … but lets hard code this to prove it works at all
Dim addEmailRequest As String = "{ ““apikey””: ““myAPIKey””, ““id””: ““myListId””, ““email””: { ““email””: ““theNewEmail@gmail.com”” } } "

Socket.SetRequestContent(addEmailRequest)
Socket.Post(“https://us3.api.mailchimp.com/2.0/lists/subscribe/”)[/code]

[quote=130269:@Paul Stevenson]Hi Matteo,

Did you get this to work?

I am about to attempt updating Mailchimp from Xojo as at the moment I use a another piece of software.

Cheers,

Paul.[/quote]

I have gotten Mandrill (the transactional API from Mailchimp) working. It isnt that hard once you understand their REST API.

Thanks @Norman Palardy !

I assume that worked ?
So now you can compose the data as JSON and you’re good to go :slight_smile:

My project is really old but this is what I had working:

[code] Socket1.Secure=True
form = New Dictionary
form.value(“apikey”) = apikey
form.value(“id”) = listid
form.value(“email_address”)= email
form.value(“merge_vars[FNAME]”)= firstName
form.value(“merge_vars[LNAME]”)= lastName
dim temp as string = birthdaySQLDateString
form.Value(“merge_vars[Birthday]”)=temp.mid(5,2)+"/"+temp.Right(2)+"/"+temp.Left(4)
form.value(“output”)=“json”

socket1.setFormData form
socket1.post(“http://”+region+".api.mailchimp.com/1.3/?method=listSubscribe")
[/code]

[quote=130506:@Norman Palardy]I assume that worked ?
So now you can compose the data as JSON and you’re good to go :)[/quote]

Honestly i still didn’t tried :slight_smile: But i’m quite sure it will work ![quote=130516:@Brock Nash]My project is really old but this is what I had working:

[code] Socket1.Secure=True
form = New Dictionary
form.value(“apikey”) = apikey
form.value(“id”) = listid
form.value(“email_address”)= email
form.value(“merge_vars[FNAME]”)= firstName
form.value(“merge_vars[LNAME]”)= lastName
dim temp as string = birthdaySQLDateString
form.Value(“merge_vars[Birthday]”)=temp.mid(5,2)+"/"+temp.Right(2)+"/"+temp.Left(4)
form.value(“output”)=“json”

socket1.setFormData form
socket1.post(“http://”+region+".api.mailchimp.com/1.3/?method=listSubscribe")
[/code][/quote]

Thanks a lot for sharing @Brock Nash !

You guys just fill my weekend with this :slight_smile:

HI guys,

Thanks for the contributions.

I had a look at Temboo and tembookit and they lost me a bit (I am a very part-time developer) so I looked at Brock’s code and it sort of worked for me but although I now know a lot more about dictionaries than I did I still had trouble with the JSON stuff so I tried the basic build a url method with the code below.

It works (and in my head makes sense) but is there a reason why I should not do it this way (security etc>)?

[code]Socket.Secure = True

Dim strinfo As String
Dim result As String
Dim url As String

// Build the string to send to MailChimp
strinfo = “apikey=” + txtAPI.Text

// Check for the api option selected
Select Case cmbType.text
Case “Subscribe”
// Basic required data
strinfo = strinfo + “&id=” + txtList.text
strinfo = strinfo + “&email[email]=” + txtEmail.text
// Add some optional data
'If txtFName.text <> “” Then
strinfo = strinfo + “&merge_vars[FNAME]=” + txtFName.text
'End If
If txtLName.text <> “” Then
strinfo = strinfo +"&merge_vars[LNAME]=" + txtLName.text
End If
// Add the optional double optin and update settings
strinfo = strinfo + “&double_optin=false&update_existing=true”
// Create the full URL with the base web address, the selected API option and string info
url = txtURL.text + “lists/subscribe.xml?” + strinfo
Case “List Lists”
url = txturl.text + “lists/list.xml?”+ strinfo
End Select

// an example text string = “https://us2.api.mailchimp.com/2.0/lists/subscribe.xml?apikey=YOURAPI&id=YOURLISTID&email[email]=YOUREMAIL&double_optin=false&update_existing=true

// show the complete string on the form (for checking).
txtOutput.text = url

// Post the url and update the results field
result = socket.post(url,30)
txtReturn.text = result[/code]

Cheers,

Paul

[quote=130954:@Paul Stevenson]HI guys,

Thanks for the contributions.

I had a look at Temboo and tembookit and they lost me a bit (I am a very part-time developer) so I looked at Brock’s code and it sort of worked for me but although I now know a lot more about dictionaries than I did I still had trouble with the JSON stuff so I tried the basic build a url method with the code below.

It works (and in my head makes sense) but is there a reason why I should not do it this way (security etc>)?[/quote]

Always happy to help the community :slight_smile:

The only problem I see with your method is that you’re not calling EncodeURLComponent. If there is anything remotely special in your customer’s data then it will fail. (This function encodes special characters. In addition, it encodes the following characters: , / ? : @ & = + $ #)