Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.0.0+

List Twilio message history

See more Twilio Examples

List inbound and outbound messages, newest sent date first. Print the Message SID, direction, and status so you can distinguish incoming messages from outbound submissions and see their latest state.

Each response contains a messages array and a next_page_uri. Follow that URI until it is null. PageSize limits one response. Use credentials for the account whose history you want to read.

Twilio Messages API reference

Chilkat Tcl Downloads

Tcl

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
#  Load the owning Account SID and an API key from application configuration.
set accountSid "YOUR_ACCOUNT_SID"
CkHttp_put_Login $http "YOUR_API_KEY_SID"
CkHttp_put_Password $http "YOUR_API_KEY_SECRET"
set url [new_CkStringBuilder]

CkStringBuilder_Append $url "https://api.twilio.com/2010-04-01/Accounts/"
CkStringBuilder_Append $url $accountSid
CkStringBuilder_Append $url "/Messages.json"
CkStringBuilder_Append $url "?PageSize=50"
set resp [new_CkHttpResponse]

set reply [new_CkJsonObject]

set total 0
set more 1
while {$more == 1} {
    set success [CkHttp_HttpNoBody $http "GET" [CkStringBuilder_getAsString $url] $resp]
    if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
        puts [CkHttp_lastErrorText $http]
        delete_CkHttp $http
        delete_CkStringBuilder $url
        delete_CkHttpResponse $resp
        delete_CkJsonObject $reply
        exit
    }

    set parsed [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
    if {expr [$success != 1]  ||  [[CkHttpResponse_get_StatusCode $resp] != 200]} 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_CkHttpResponse $resp
        delete_CkJsonObject $reply
        exit
    }

    if {$parsed != 1} then {
        puts "Expected a JSON response."
        delete_CkHttp $http
        delete_CkStringBuilder $url
        delete_CkHttpResponse $resp
        delete_CkJsonObject $reply
        exit
    }

    if {[CkJsonObject_JsonTypeOf $reply "messages"] != 4} then {
        puts "Expected a messages array."
        delete_CkHttp $http
        delete_CkStringBuilder $url
        delete_CkHttpResponse $resp
        delete_CkJsonObject $reply
        exit
    }

    set count [CkJsonObject_SizeOfArray $reply "messages"]
    set i 0
    while {$i < $count} {
        CkJsonObject_put_I $reply $i
        puts [CkJsonObject_stringOf $reply {messages[i].sid}]  [CkJsonObject_stringOf $reply {messages[i].direction}]  [CkJsonObject_stringOf $reply {messages[i].status}]
        set i [expr $i + 1]
    }
    set total [expr $total + $count]
    #  Twilio supplies a relative URI with the cursor and returns null on the last page.
    if {[CkJsonObject_IsNullOf $reply "next_page_uri"] == 1} then {
        set more 0
    }     else {
        if {[CkJsonObject_JsonTypeOf $reply "next_page_uri"] != 1} then {
            puts "Expected a next-page URI or null."
            delete_CkHttp $http
            delete_CkStringBuilder $url
            delete_CkHttpResponse $resp
            delete_CkJsonObject $reply
            exit
        }

        CkStringBuilder_SetString $url "https://api.twilio.com"
        CkStringBuilder_Append $url [CkJsonObject_stringOf $reply "next_page_uri"]
    }

}
puts "Messages found: $total"

delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $resp
delete_CkJsonObject $reply