CkPython Requires Chilkat v11.0.0+
CkPython
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.
Chilkat CkPython Downloads
import sys
import chilkat
success = False
# Assumes Chilkat is unlocked. See Global Unlock Sample.
http = chilkat.CkHttp()
http.put_RequireSslCertVerify(True)
http.put_BasicAuth(True)
# Load the owning Account SID and an API key from application configuration.
accountSid = "YOUR_ACCOUNT_SID"
http.put_Login("YOUR_API_KEY_SID")
http.put_Password("YOUR_API_KEY_SECRET")
url = chilkat.CkStringBuilder()
url.Append("https://api.twilio.com/2010-04-01/Accounts/")
url.Append(accountSid)
url.Append("/Messages.json")
url.Append("?PageSize=50")
resp = chilkat.CkHttpResponse()
reply = chilkat.CkJsonObject()
total = 0
more = True
while (more == True) :
success = http.HttpNoBody("GET",url.getAsString(),resp)
if (resp.get_StatusCode() == 0):
print(http.lastErrorText())
sys.exit()
parsed = reply.Load(resp.bodyStr())
if ((success != True) or (resp.get_StatusCode() != 200)):
print("HTTP " + str(resp.get_StatusCode()))
if ((parsed == True) and (reply.HasMember("code") == True)):
print("Twilio error " + str(reply.IntOf("code")) + ": " + reply.stringOf("message"))
sys.exit()
if (parsed != True):
print("Expected a JSON response.")
sys.exit()
if (reply.JsonTypeOf("messages") != 4):
print("Expected a messages array.")
sys.exit()
count = reply.SizeOfArray("messages")
i = 0
while (i < count) :
reply.put_I(i)
print(reply.stringOf("messages[i].sid") + " " + reply.stringOf("messages[i].direction") + " " + reply.stringOf("messages[i].status"))
i = i + 1
total = total + count
# Twilio supplies a relative URI with the cursor and returns null on the last page.
if (reply.IsNullOf("next_page_uri") == True):
more = False
else:
if (reply.JsonTypeOf("next_page_uri") != 1):
print("Expected a next-page URI or null.")
sys.exit()
url.SetString("https://api.twilio.com")
url.Append(reply.stringOf("next_page_uri"))
print("Messages found: " + str(total))