Lua Requires Chilkat v11.0.0+
Lua
Constant Contact V3: Restore a deleted contact to a list
See more Constant Contact Examples
Look up the deleted contact by email, then restore it by adding its existing ID to a chosen list. Constant Contact documents this membership activity as a restoration method. It changes only membership, preserves the saved profile, and makes no email-permission claim. Select the intended destination list explicitly; previous memberships were removed by deletion. Authorize the app first and use the same credential store.
Constant Contact developer guide
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local vault = chilkat.Secrets.new()
vault.location = "local_manager"
vault.auto_split = true
local entry = chilkat.JsonObject.new()
entry:update_string("appName", "ConstantContactV3")
entry:update_string("service", "OAuth2")
entry:update_string("username", "my-account")
local tokens = chilkat.JsonObject.new()
local loaded = vault:get_secret_json(entry, tokens)
if not loaded then
print("Run the desktop authorization example using this account and store.")
return
end
if tokens:json_type_of("access_token") ~= 1 then
print("The saved authorization has no access token.")
return
end
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.auth_token = tokens:string_of("access_token")
local resp = chilkat.HttpResponse.new()
local query = chilkat.HttpRequest.new()
query.http_verb = "GET"
query:add_param("email", "reader+newsletter@example.com")
query:add_param("status", "deleted")
local success = http:http_req("https://api.cc.email/v3/contacts", query, resp)
if (not success) or (resp.status_code ~= 200) then
print("Expected HTTP 200. Received " .. resp.status_code)
if resp.status_code == 401 then
print("Refresh the saved access token or authorize again.")
end
return
end
local matches = chilkat.JsonObject.new()
success = matches:load(resp.body_str)
if not success then
print("Expected a JSON response.")
return
end
if (matches:size_of_array("contacts") ~= 1) or (matches:json_type_of("contacts[0].deleted_at") ~= 1) then
print("Expected one deleted contact for this email. No restoration submitted.")
return
end
if (matches:json_type_of("contacts[0].contact_id") ~= 1) or (matches:string_of_equals("contacts[0].contact_id", "", true)) then
print("Missing contacts[0].contact_id in the response.")
return
end
local body = chilkat.JsonObject.new()
body:update_string("source.contact_ids[0]", matches:string_of("contacts[0].contact_id"))
body:update_string("list_ids[0]", "DESTINATION_LIST_ID")
success = http:http_json("POST", "https://api.cc.email/v3/activities/add_list_memberships", body, "application/json", resp)
if (not success) or ((resp.status_code < 200) or (resp.status_code >= 300)) then
print("Could not start the membership activity. HTTP " .. resp.status_code)
return
end
local job = chilkat.JsonObject.new()
success = job:load(resp.body_str)
if not success then
print("Expected a JSON response.")
return
end
if (job:json_type_of("activity_id") ~= 1) or (job:string_of_equals("activity_id", "", true)) then
print("Missing activity_id in the response.")
return
end
local activityId = job:string_of("activity_id")
print("Submitted activity ID: " .. activityId)
http:set_url_var("job", activityId)
-- Poll briefly. A longer-running job can be checked later using the same ID.
local attempt = 0
while attempt < 5 do
success = http:http_no_body("GET", "https://api.cc.email/v3/activities/{$job}", resp)
if (not success) or (resp.status_code ~= 200) then
print("Status check returned HTTP " .. resp.status_code .. ". Keep the activity ID and check again later.")
return
end
success = job:load(resp.body_str)
if (not success) or (job:json_type_of("state") ~= 1) then
print("Missing activity state. Keep the activity ID.")
return
end
local state = job:string_of("state")
if (state ~= "initialized") and (state ~= "processing") then
print("Activity state: " .. state)
local i = 0
while i < job:size_of_array("activity_errors") do
job.i = i
print(job:string_of("activity_errors[i]"))
i = i + 1
end
return
end
attempt = attempt + 1
if attempt < 5 then
http:sleep_ms(2000)
end
end
print("Still processing. Recheck the activity ID before submitting another request.")