Sample code for 30+ languages & platforms
DataFlex 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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    String sAccountSid
    Handle hoUrl
    Variant vResp
    Handle hoResp
    Handle hoReply
    Integer iTotal
    Boolean iMore
    Boolean iParsed
    Integer iCount
    Integer i
    String sTemp1
    String sTemp2
    String sTemp3
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    Set ComBasicAuth Of hoHttp To True
    //  Load the owning Account SID and an API key from application configuration.
    Move "YOUR_ACCOUNT_SID" To sAccountSid
    Set ComLogin Of hoHttp To "YOUR_API_KEY_SID"
    Set ComPassword Of hoHttp To "YOUR_API_KEY_SECRET"
    Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
    If (Not(IsComObjectCreated(hoUrl))) Begin
        Send CreateComObject of hoUrl
    End
    Get ComAppend Of hoUrl "https://api.twilio.com/2010-04-01/Accounts/" To iSuccess
    Get ComAppend Of hoUrl sAccountSid To iSuccess
    Get ComAppend Of hoUrl "/Messages.json" To iSuccess
    Get ComAppend Of hoUrl "?PageSize=50" To iSuccess
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoReply
    If (Not(IsComObjectCreated(hoReply))) Begin
        Send CreateComObject of hoReply
    End
    Move 0 To iTotal
    Move True To iMore
    While (iMore = True)
        Get ComGetAsString Of hoUrl To sTemp1
        Get pvComObject of hoResp to vResp
        Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 = 0) Begin
            Get ComLastErrorText Of hoHttp To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoReply sTemp1 To iParsed
        Get ComStatusCode Of hoResp To iTemp1
        If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
            Get ComStatusCode Of hoResp To iTemp1
            Showln "HTTP " iTemp1
            Get ComHasMember Of hoReply "code" To bTemp1
            If ((iParsed = True) And (bTemp1 = True)) Begin
                Get ComIntOf Of hoReply "code" To iTemp1
                Get ComStringOf Of hoReply "message" To sTemp1
                Showln "Twilio error " iTemp1 ": " sTemp1
            End

            Procedure_Return
        End

        If (iParsed <> True) Begin
            Showln "Expected a JSON response."
            Procedure_Return
        End

        Get ComJsonTypeOf Of hoReply "messages" To iTemp1
        If (iTemp1 <> 4) Begin
            Showln "Expected a messages array."
            Procedure_Return
        End

        Get ComSizeOfArray Of hoReply "messages" To iCount
        Move 0 To i
        While (i < iCount)
            Set ComI Of hoReply To i
            Get ComStringOf Of hoReply "messages[i].sid" To sTemp1
            Get ComStringOf Of hoReply "messages[i].direction" To sTemp2
            Get ComStringOf Of hoReply "messages[i].status" To sTemp3
            Showln sTemp1 "  " sTemp2 "  " sTemp3
            Move (i + 1) To i
        Loop

        Move (iTotal + iCount) To iTotal
        //  Twilio supplies a relative URI with the cursor and returns null on the last page.
        Get ComIsNullOf Of hoReply "next_page_uri" To bTemp1
        If (bTemp1 = True) Begin
            Move False To iMore
        End
        Else Begin
            Get ComJsonTypeOf Of hoReply "next_page_uri" To iTemp1
            If (iTemp1 <> 1) Begin
                Showln "Expected a next-page URI or null."
                Procedure_Return
            End

            Get ComSetString Of hoUrl "https://api.twilio.com" To iSuccess
            Get ComStringOf Of hoReply "next_page_uri" To sTemp1
            Get ComAppend Of hoUrl sTemp1 To iSuccess
        End

    Loop

    Showln "Messages found: " iTotal


End_Procedure