Chilkat Online Tools

SQL Server / Twilio Messaging / SMS / List all messages sent from your Twilio number

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    EXEC sp_OASetProperty @http, 'BasicAuth', 1
    EXEC sp_OASetProperty @http, 'Login', '{{TWILIO_ACCOUNT_SID}}'
    EXEC sp_OASetProperty @http, 'Password', '{{TWILIO_AUTH_TOKEN}}'

    DECLARE @queryParams int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @queryParams OUT

    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'From', '{{TWILIO_PHONE_NUMBER}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'QuickRequestParams', @resp OUT, 'GET', 'https://api.twilio.com/2010-04-01/Accounts/:AccountSid/Messages.json', @queryParams
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @queryParams
        RETURN
      END

    DECLARE @sbResponseBody int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @queryParams
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "end": 51142961,
    --   "first_page_uri": "https://ZvltuXKxENEyvG.fnbzDIxrccIwHE6GxsyHjMd",
    --   "messages": [
    --     {
    --       "account_sid": "AC9E",
    --       "api_version": "commodo sint sunt eu",
    --       "body": "ut cillum",
    --       "date_created": "fugiat ipsum enim quis",
    --       "date_sent": "o",
    --       "date_updated": "laborum id esse elit",
    --       "direction": "outbound-reply",
    --       "error_code": 89665305,
    --       "error_message": "deserunt",
    --       "from": "anim esse",
    --       "messaging_service_sid": "MGC0",
    --       "num_media": "enim et",
    --       "num_segments": "quis qui ipsum",
    --       "price": "exercitation Duis",
    --       "price_unit": "consequat qu",
    --       "sid": "SMe5",
    --       "status": "scheduled",
    --       "subresource_uris": {},
    --       "to": "nulla",
    --       "uri": "cillum ut in irure"
    --     },
    --     {
    --       "account_sid": "ACD4",
    --       "api_version": "incididunt in non",
    --       "body": "deserunt incididunt irure eiusmod",
    --       "date_created": "sint ea",
    --       "date_sent": "Ut ut consectetur",
    --       "date_updated": "non dolore",
    --       "direction": "outbound-api",
    --       "error_code": -69524035,
    --       "error_message": "dolore Ut officia",
    --       "from": "reprehenderit",
    --       "messaging_service_sid": "MGEe",
    --       "num_media": "voluptate fugiat eiusmod qui Ut",
    --       "num_segments": "ea",
    --       "price": "consequat",
    --       "price_unit": "fugiat aliquip",
    --       "sid": "MM38",
    --       "status": "received",
    --       "subresource_uris": {},
    --       "to": "in tempor labore eiusmod",
    --       "uri": "elit non dol"
    --     }
    --   ],
    --   "next_page_uri": "http://fSikGpthYZQbOt.sncsPW4TzW44TkKnuDgx8f2FJz.",
    --   "page": -43897926,
    --   "page_size": 12121047,
    --   "previous_page_uri": "https://lSrGpSrECCMzKudcyRlwJdfhlnZEaAQ.hytgI,rCvOIy,x1C6vbgTvc9p6X2hkAdrqkd5jx8JKwJPiCrpsg2w",
    --   "start": -23084704,
    --   "uri": "https://USSzIQwA.hvxpmaT2476DlQGIi"
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @date_created int
    -- Use "Chilkat_9_5_0.DtObj" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.DtObj', @date_created OUT

    DECLARE @date_sent int
    -- Use "Chilkat_9_5_0.DtObj" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.DtObj', @date_sent OUT

    DECLARE @date_updated int
    -- Use "Chilkat_9_5_0.DtObj" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.DtObj', @date_updated OUT

    DECLARE @account_sid nvarchar(4000)

    DECLARE @api_version nvarchar(4000)

    DECLARE @body nvarchar(4000)

    DECLARE @direction nvarchar(4000)

    DECLARE @error_code int

    DECLARE @error_message nvarchar(4000)

    DECLARE @from nvarchar(4000)

    DECLARE @messaging_service_sid nvarchar(4000)

    DECLARE @num_media nvarchar(4000)

    DECLARE @num_segments nvarchar(4000)

    DECLARE @price nvarchar(4000)

    DECLARE @price_unit nvarchar(4000)

    DECLARE @sid nvarchar(4000)

    DECLARE @status nvarchar(4000)

    DECLARE @v_to nvarchar(4000)

    DECLARE @v_end int
    EXEC sp_OAMethod @jResp, 'IntOf', @v_end OUT, 'end'
    DECLARE @first_page_uri nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @first_page_uri OUT, 'first_page_uri'
    DECLARE @next_page_uri nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @next_page_uri OUT, 'next_page_uri'
    DECLARE @page int
    EXEC sp_OAMethod @jResp, 'IntOf', @page OUT, 'page'
    DECLARE @page_size int
    EXEC sp_OAMethod @jResp, 'IntOf', @page_size OUT, 'page_size'
    DECLARE @previous_page_uri nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @previous_page_uri OUT, 'previous_page_uri'
    DECLARE @start int
    EXEC sp_OAMethod @jResp, 'IntOf', @start OUT, 'start'
    DECLARE @uri nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @uri OUT, 'uri'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'messages'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @account_sid OUT, 'messages[i].account_sid'
        EXEC sp_OAMethod @jResp, 'StringOf', @api_version OUT, 'messages[i].api_version'
        EXEC sp_OAMethod @jResp, 'StringOf', @body OUT, 'messages[i].body'
        EXEC sp_OAMethod @jResp, 'DtOf', @success OUT, 'messages[i].date_created', 0, @date_created
        EXEC sp_OAMethod @jResp, 'DtOf', @success OUT, 'messages[i].date_sent', 0, @date_sent
        EXEC sp_OAMethod @jResp, 'DtOf', @success OUT, 'messages[i].date_updated', 0, @date_updated
        EXEC sp_OAMethod @jResp, 'StringOf', @direction OUT, 'messages[i].direction'
        EXEC sp_OAMethod @jResp, 'IntOf', @error_code OUT, 'messages[i].error_code'
        EXEC sp_OAMethod @jResp, 'StringOf', @error_message OUT, 'messages[i].error_message'
        EXEC sp_OAMethod @jResp, 'StringOf', @from OUT, 'messages[i].from'
        EXEC sp_OAMethod @jResp, 'StringOf', @messaging_service_sid OUT, 'messages[i].messaging_service_sid'
        EXEC sp_OAMethod @jResp, 'StringOf', @num_media OUT, 'messages[i].num_media'
        EXEC sp_OAMethod @jResp, 'StringOf', @num_segments OUT, 'messages[i].num_segments'
        EXEC sp_OAMethod @jResp, 'StringOf', @price OUT, 'messages[i].price'
        EXEC sp_OAMethod @jResp, 'StringOf', @price_unit OUT, 'messages[i].price_unit'
        EXEC sp_OAMethod @jResp, 'StringOf', @sid OUT, 'messages[i].sid'
        EXEC sp_OAMethod @jResp, 'StringOf', @status OUT, 'messages[i].status'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_to OUT, 'messages[i].to'
        EXEC sp_OAMethod @jResp, 'StringOf', @uri OUT, 'messages[i].uri'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @queryParams
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp
    EXEC @hr = sp_OADestroy @date_created
    EXEC @hr = sp_OADestroy @date_sent
    EXEC @hr = sp_OADestroy @date_updated


END
GO

Curl Command

curl -G -d "From=%7B%7BTWILIO_PHONE_NUMBER%7D%7D"
	-u '{{TWILIO_ACCOUNT_SID}}:{{TWILIO_AUTH_TOKEN}}'
https://api.twilio.com/2010-04-01/Accounts/:AccountSid/Messages.json

Postman Collection Item JSON

{
  "name": "List all messages sent from your Twilio number",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "var template = `",
          "    <table bgcolor=\"#FFFFFF\">",
          "        <tr>",
          "            <th>To</th>",
          "            <th>Message Body</th>",
          "            <th>Sent at</th>",
          "        </tr>",
          "",
          "        {{#each messages}}",
          "            <tr>",
          "                <td>{{to}}</td>",
          "                <td>{{body}}</td>",
          "                <td>{{date_sent}}</td>",
          "            </tr>",
          "        {{/each}}",
          "    </table>",
          "`;",
          "",
          "// Set visualizer",
          "pm.visualizer.set(template, {",
          "    // Pass the response body parsed as JSON as `data`",
          "    messages: pm.response.json().messages",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "basic",
      "basic": [
        {
          "key": "password",
          "value": "{{TWILIO_AUTH_TOKEN}}",
          "type": "string"
        },
        {
          "key": "username",
          "value": "{{TWILIO_ACCOUNT_SID}}",
          "type": "string"
        }
      ]
    },
    "method": "GET",
    "header": [
    ],
    "url": {
      "raw": "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}/2010-04-01/Accounts/:AccountSid/Messages.json?From={{TWILIO_PHONE_NUMBER}}",
      "host": [
        "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}"
      ],
      "path": [
        "2010-04-01",
        "Accounts",
        ":AccountSid",
        "Messages.json"
      ],
      "query": [
        {
          "key": "To",
          "value": "<string>",
          "description": "Read messages sent to only this phone number.",
          "disabled": true
        },
        {
          "key": "From",
          "value": "{{TWILIO_PHONE_NUMBER}}",
          "description": "Read messages sent from only this phone number or alphanumeric sender ID."
        },
        {
          "key": "DateSent",
          "value": "<dateTime>",
          "description": "The date of the messages to show. Specify a date as `YYYY-MM-DD` in GMT to read only messages sent on this date. For example: `2009-07-06`. You can also specify an inequality, such as `DateSent<=YYYY-MM-DD`, to read messages sent on or before midnight on a date, and `DateSent>=YYYY-MM-DD` to read messages sent on or after midnight on a date.",
          "disabled": true
        },
        {
          "key": "DateSent<",
          "value": "<dateTime>",
          "description": "The date of the messages to show. Specify a date as `YYYY-MM-DD` in GMT to read only messages sent on this date. For example: `2009-07-06`. You can also specify an inequality, such as `DateSent<=YYYY-MM-DD`, to read messages sent on or before midnight on a date, and `DateSent>=YYYY-MM-DD` to read messages sent on or after midnight on a date.",
          "disabled": true
        },
        {
          "key": "DateSent>",
          "value": "<dateTime>",
          "description": "The date of the messages to show. Specify a date as `YYYY-MM-DD` in GMT to read only messages sent on this date. For example: `2009-07-06`. You can also specify an inequality, such as `DateSent<=YYYY-MM-DD`, to read messages sent on or before midnight on a date, and `DateSent>=YYYY-MM-DD` to read messages sent on or after midnight on a date.",
          "disabled": true
        },
        {
          "key": "PageSize",
          "value": "<integer>",
          "description": "How many resources to return in each list page. The default is 50, and the maximum is 1000.",
          "disabled": true
        }
      ],
      "variable": [
        {
          "key": "AccountSid",
          "value": "{{TWILIO_ACCOUNT_SID}}",
          "description": "(Required) The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Message resources to read."
        }
      ]
    },
    "description": "Retrieve a list of messages belonging to the account used to make the request. You can filter for example for only the messages that were sent from a specific phone number.\n\nRead more about it in the [Twilio Docs](https://www.twilio.com/docs/sms/api/message-resource#read-multiple-message-resources)"
  },
  "response": [
    {
      "name": "OK",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "description": "Added as a part of security scheme: basic",
            "key": "Authorization",
            "value": "Basic <credentials>"
          }
        ],
        "url": {
          "raw": "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}/2010-04-01/Accounts/:AccountSid/Messages.json?To=<string>&From=<string>&DateSent=<dateTime>&DateSent<=<dateTime>&DateSent>=<dateTime>&PageSize=<integer>",
          "host": [
            "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}"
          ],
          "path": [
            "2010-04-01",
            "Accounts",
            ":AccountSid",
            "Messages.json"
          ],
          "query": [
            {
              "key": "To",
              "value": "<string>"
            },
            {
              "key": "From",
              "value": "<string>"
            },
            {
              "key": "DateSent",
              "value": "<dateTime>"
            },
            {
              "key": "DateSent<",
              "value": "<dateTime>"
            },
            {
              "key": "DateSent>",
              "value": "<dateTime>"
            },
            {
              "key": "PageSize",
              "value": "<integer>"
            }
          ],
          "variable": [
            {
              "key": "AccountSid"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"end\": 51142961,\n \"first_page_uri\": \"https://ZvltuXKxENEyvG.fnbzDIxrccIwHE6GxsyHjMd\",\n \"messages\": [\n  {\n   \"account_sid\": \"AC9E\",\n   \"api_version\": \"commodo sint sunt eu\",\n   \"body\": \"ut cillum\",\n   \"date_created\": \"fugiat ipsum enim quis\",\n   \"date_sent\": \"o\",\n   \"date_updated\": \"laborum id esse elit\",\n   \"direction\": \"outbound-reply\",\n   \"error_code\": 89665305,\n   \"error_message\": \"deserunt\",\n   \"from\": \"anim esse\",\n   \"messaging_service_sid\": \"MGC0\",\n   \"num_media\": \"enim et\",\n   \"num_segments\": \"quis qui ipsum\",\n   \"price\": \"exercitation Duis\",\n   \"price_unit\": \"consequat qu\",\n   \"sid\": \"SMe5\",\n   \"status\": \"scheduled\",\n   \"subresource_uris\": {},\n   \"to\": \"nulla\",\n   \"uri\": \"cillum ut in irure\"\n  },\n  {\n   \"account_sid\": \"ACD4\",\n   \"api_version\": \"incididunt in non\",\n   \"body\": \"deserunt incididunt irure eiusmod\",\n   \"date_created\": \"sint ea\",\n   \"date_sent\": \"Ut ut consectetur\",\n   \"date_updated\": \"non dolore\",\n   \"direction\": \"outbound-api\",\n   \"error_code\": -69524035,\n   \"error_message\": \"dolore Ut officia\",\n   \"from\": \"reprehenderit\",\n   \"messaging_service_sid\": \"MGEe\",\n   \"num_media\": \"voluptate fugiat eiusmod qui Ut\",\n   \"num_segments\": \"ea\",\n   \"price\": \"consequat\",\n   \"price_unit\": \"fugiat aliquip\",\n   \"sid\": \"MM38\",\n   \"status\": \"received\",\n   \"subresource_uris\": {},\n   \"to\": \"in tempor labore eiusmod\",\n   \"uri\": \"elit non dol\"\n  }\n ],\n \"next_page_uri\": \"http://fSikGpthYZQbOt.sncsPW4TzW44TkKnuDgx8f2FJz.\",\n \"page\": -43897926,\n \"page_size\": 12121047,\n \"previous_page_uri\": \"https://lSrGpSrECCMzKudcyRlwJdfhlnZEaAQ.hytgI,rCvOIy,x1C6vbgTvc9p6X2hkAdrqkd5jx8JKwJPiCrpsg2w\",\n \"start\": -23084704,\n \"uri\": \"https://USSzIQwA.hvxpmaT2476DlQGIi\"\n}"
    }
  ]
}