Tcl Requires Chilkat v11.0.0+
Tcl
Send an SMS with Twilio
See more Twilio Examples
Submit an SMS with the sender, recipient, and text as form fields. This example starts with Twilio test credentials and its successful test sender. Twilio simulates the submission without sending or billing for a message. Test credentials and test numbers.
For live SMS, use the live Account SID in the URL, authenticate with an API key SID and secret, and use an SMS-capable Twilio sender and a recipient in E.164 format. HTTP 201 creates the message. Save its SID to check delivery status later.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set http [new_CkHttp]
CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_BasicAuth $http 1
# Use the test Account SID and test Auth Token from the Twilio Console.
set accountSid "YOUR_TEST_ACCOUNT_SID"
CkHttp_put_Login $http $accountSid
CkHttp_put_Password $http "YOUR_TEST_AUTH_TOKEN"
set url [new_CkStringBuilder]
CkStringBuilder_Append $url "https://api.twilio.com/2010-04-01/Accounts/"
CkStringBuilder_Append $url $accountSid
CkStringBuilder_Append $url "/Messages.json"
# Add raw field values. Chilkat form-encodes them, including the + in a phone number.
set req [new_CkHttpRequest]
CkHttpRequest_put_HttpVerb $req "POST"
CkHttpRequest_put_ContentType $req "application/x-www-form-urlencoded"
CkHttpRequest_AddParam $req "From" "+15005550006"
CkHttpRequest_AddParam $req "To" "+12025550123"
CkHttpRequest_AddParam $req "Body" "Hello from Twilio!"
set resp [new_CkHttpResponse]
set reply [new_CkJsonObject]
set success [CkHttp_HttpReq $http [CkStringBuilder_getAsString $url] $req $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
puts [CkHttp_lastErrorText $http]
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
set parsed [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
if {expr [$success != 1] || [[CkHttpResponse_get_StatusCode $resp] != 201]} then {
puts "HTTP [CkHttpResponse_get_StatusCode $resp]"
if {expr [$parsed == 1] && [[CkJsonObject_HasMember $reply "code"] == 1]} then {
puts "Twilio error [CkJsonObject_IntOf $reply code]: [CkJsonObject_stringOf $reply message]"
}
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
if {$parsed != 1} then {
puts "Expected a JSON response."
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
if {expr [[CkJsonObject_JsonTypeOf $reply "sid"] != 1] || [[CkJsonObject_JsonTypeOf $reply "status"] != 1]} then {
puts "The create response is missing the message SID or status."
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
puts "Message SID: [CkJsonObject_stringOf $reply sid]"
puts "Initial status: [CkJsonObject_stringOf $reply status]"
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkJsonObject $reply