Tcl Requires Chilkat v11.0.0+
Tcl
SendGrid: Save and verify a topic opt-out
See more SendGrid Examples
Use this when a signed-in reader turns off Product updates. Look up the configured group before submitting: SendGrid warns that a nonexistent or deleted group can cause a global suppression instead. Keep the group active while processing preferences. Deleting a marketing contact is not a substitute for recording an opt-out.
Add group suppressions ยท Check specific addresses
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0
set http [new_CkHttp]
CkHttp_put_RequireSslCertVerify $http 1
# Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
CkHttp_put_AuthToken $http "SENDGRID_API_KEY"
set resp [new_CkHttpResponse]
# Take the address from the authenticated account and the group ID from configuration.
set address "reader@example.com"
CkHttp_SetUrlVar $http "group" "12345"
set success [CkHttp_HttpNoBody $http "GET" "https://api.sendgrid.com/v3/asm/groups/{$group}" $resp]
if {$success != 1} then {
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
puts [CkHttp_lastErrorText $http]
} else {
puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
}
delete_CkHttp $http
delete_CkHttpResponse $resp
exit
}
if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
delete_CkHttp $http
delete_CkHttpResponse $resp
exit
}
set groupInfo [new_CkJsonObject]
set success [CkJsonObject_Load $groupInfo [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
puts "Unexpected response: expected JSON."
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $groupInfo
exit
}
if {[CkJsonObject_StringOfEquals $groupInfo "name" "Product updates" 1] != 1} then {
puts "Unexpected unsubscribe group; no preference was changed."
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $groupInfo
exit
}
set addresses [new_CkJsonObject]
CkJsonObject_UpdateString $addresses "recipient_emails[0]" $address
set success [CkHttp_HttpJson $http "POST" "https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions" $addresses "application/json" $resp]
if {$success != 1} then {
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
puts [CkHttp_lastErrorText $http]
} else {
puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
}
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $groupInfo
delete_CkJsonObject $addresses
exit
}
if {[CkHttpResponse_get_StatusCode $resp] != 201} then {
puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $groupInfo
delete_CkJsonObject $addresses
exit
}
set success [CkHttp_HttpJson $http "POST" "https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search" $addresses "application/json" $resp]
if {$success != 1} then {
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
puts [CkHttp_lastErrorText $http]
} else {
puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
}
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $groupInfo
delete_CkJsonObject $addresses
exit
}
if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $groupInfo
delete_CkJsonObject $addresses
exit
}
set matches [new_CkJsonArray]
set success [CkJsonArray_Load $matches [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
puts "Unexpected response: expected JSON."
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $groupInfo
delete_CkJsonObject $addresses
delete_CkJsonArray $matches
exit
}
if {[CkJsonArray_get_Size $matches] == 1} then {
puts "Confirmed opt-out: [CkJsonArray_stringAt $matches 0]"
} else {
puts "Opt-out not confirmed. Recheck this group."
}
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $groupInfo
delete_CkJsonObject $addresses
delete_CkJsonArray $matches