Chilkat Online Tools

DataFlex / Twilio Messaging / SMS / Send an SMS

Back to Collection Items

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Boolean iSuccess
    Variant vReq
    Handle hoReq
    Handle hoJsonParam2
    Handle hoJsonParam3
    Variant vResp
    Handle hoResp
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Handle hoJResp
    Integer iRespStatusCode
    Variant vDate_created
    Handle hoDate_created
    Variant vDate_sent
    Handle hoDate_sent
    Variant vDate_updated
    Handle hoDate_updated
    String sAccount_sid
    String sApi_version
    String sBody
    String sDirection
    Integer iError_code
    String sError_message
    String sFrom
    String sMessaging_service_sid
    String sNum_media
    String sNum_segments
    String sPrice
    String sPrice_unit
    String sSid
    String sStatus
    String sV_to
    String sUri
    String sTemp1
    Boolean bTemp1

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Set ComBasicAuth Of hoHttp To True
    Set ComLogin Of hoHttp To "{{TWILIO_ACCOUNT_SID}}"
    Set ComPassword Of hoHttp To "{{TWILIO_AUTH_TOKEN}}"

    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End
    Send ComAddParam To hoReq "Body" "Hello World!"

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonParam2
    If (Not(IsComObjectCreated(hoJsonParam2))) Begin
        Send CreateComObject of hoJsonParam2
    End
    Get ComEmit Of hoJsonParam2 To sTemp1
    Send ComAddParam To hoReq "To" sTemp1

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonParam3
    If (Not(IsComObjectCreated(hoJsonParam3))) Begin
        Send CreateComObject of hoJsonParam3
    End
    Get ComEmit Of hoJsonParam3 To sTemp1
    Send ComAddParam To hoReq "From" sTemp1

    Get pvComObject of hoReq to vReq
    Get ComPostUrlEncoded Of hoHttp "https://api.twilio.com/2010-04-01/Accounts/:AccountSid/Messages.json" vReq To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
    If (Not(IsComObjectCreated(hoJResp))) Begin
        Send CreateComObject of hoJResp
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
    Set ComEmitCompact Of hoJResp To False

    Showln "Response Body:"
    Get ComEmit Of hoJResp To sTemp1
    Showln sTemp1

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Header:"
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Send Destroy of hoResp
        Procedure_Return
    End

    Send Destroy of hoResp

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

    // {
    //   "account_sid": "ACF8",
    //   "api_version": "exercitation eiusmod amet laboris",
    //   "body": "consectetur exercitation officia magna",
    //   "date_created": "consectetur exercitation proident",
    //   "date_sent": "culpa sint labore",
    //   "date_updated": "proident",
    //   "direction": "outbound-call",
    //   "error_code": -64742646,
    //   "error_message": "nostrud sint Ut",
    //   "from": "Excepteur cillum aute",
    //   "messaging_service_sid": "MGDB",
    //   "num_media": "dolor veniam non",
    //   "num_segments": "et ut sint ut amet",
    //   "price": "Lorem reprehenderit",
    //   "price_unit": "Excepteur tempor i",
    //   "sid": "MM79",
    //   "status": "received",
    //   "subresource_uris": {},
    //   "to": "Ut non id",
    //   "uri": "magna sit esse"
    // }

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

    Get Create (RefClass(cComChilkatDtObj)) To hoDate_created
    If (Not(IsComObjectCreated(hoDate_created))) Begin
        Send CreateComObject of hoDate_created
    End
    Get Create (RefClass(cComChilkatDtObj)) To hoDate_sent
    If (Not(IsComObjectCreated(hoDate_sent))) Begin
        Send CreateComObject of hoDate_sent
    End
    Get Create (RefClass(cComChilkatDtObj)) To hoDate_updated
    If (Not(IsComObjectCreated(hoDate_updated))) Begin
        Send CreateComObject of hoDate_updated
    End

    Get ComStringOf Of hoJResp "account_sid" To sAccount_sid
    Get ComStringOf Of hoJResp "api_version" To sApi_version
    Get ComStringOf Of hoJResp "body" To sBody
    Get pvComObject of hoDate_created to vDate_created
    Get ComDtOf Of hoJResp "date_created" False vDate_created To iSuccess
    Get pvComObject of hoDate_sent to vDate_sent
    Get ComDtOf Of hoJResp "date_sent" False vDate_sent To iSuccess
    Get pvComObject of hoDate_updated to vDate_updated
    Get ComDtOf Of hoJResp "date_updated" False vDate_updated To iSuccess
    Get ComStringOf Of hoJResp "direction" To sDirection
    Get ComIntOf Of hoJResp "error_code" To iError_code
    Get ComStringOf Of hoJResp "error_message" To sError_message
    Get ComStringOf Of hoJResp "from" To sFrom
    Get ComStringOf Of hoJResp "messaging_service_sid" To sMessaging_service_sid
    Get ComStringOf Of hoJResp "num_media" To sNum_media
    Get ComStringOf Of hoJResp "num_segments" To sNum_segments
    Get ComStringOf Of hoJResp "price" To sPrice
    Get ComStringOf Of hoJResp "price_unit" To sPrice_unit
    Get ComStringOf Of hoJResp "sid" To sSid
    Get ComStringOf Of hoJResp "status" To sStatus
    Get ComStringOf Of hoJResp "to" To sV_to
    Get ComStringOf Of hoJResp "uri" To sUri


End_Procedure

Curl Command

curl -X POST
	-u '{{TWILIO_ACCOUNT_SID}}:{{TWILIO_AUTH_TOKEN}}'
	-H "Content-Type: application/x-www-form-urlencoded"
	--data-urlencode 'Body=Hello World!'
	--data-urlencode 'To={{MY_PHONE_NUMBER}}'
	--data-urlencode 'From={{TWILIO_PHONE_NUMBER}}'
https://api.twilio.com/2010-04-01/Accounts/:AccountSid/Messages.json

Postman Collection Item JSON

{
  "name": "Send an SMS",
  "request": {
    "auth": {
      "type": "basic",
      "basic": [
        {
          "key": "password",
          "value": "{{TWILIO_AUTH_TOKEN}}",
          "type": "string"
        },
        {
          "key": "username",
          "value": "{{TWILIO_ACCOUNT_SID}}",
          "type": "string"
        }
      ]
    },
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/x-www-form-urlencoded"
      }
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "Body",
          "value": "Hello World!",
          "description": "The text of the message you want to send. Can be up to 1,600 characters in length."
        },
        {
          "key": "To",
          "value": "{{MY_PHONE_NUMBER}}",
          "description": "The destination phone number in [E.164](https://www.twilio.com/docs/glossary/what-e164) format for SMS/MMS or [Channel user address](https://www.twilio.com/docs/sms/channels#channel-addresses) for other 3rd-party channels."
        },
        {
          "key": "From",
          "value": "{{TWILIO_PHONE_NUMBER}}",
          "description": "A Twilio phone number in [E.164](https://www.twilio.com/docs/glossary/what-e164) format, an [alphanumeric sender ID](https://www.twilio.com/docs/sms/send-messages#use-an-alphanumeric-sender-id), or a [Channel Endpoint address](https://www.twilio.com/docs/sms/channels#channel-addresses) that is enabled for the type of message you want to send. Phone numbers or [short codes](https://www.twilio.com/docs/sms/api/short-code) purchased from Twilio also work here. You cannot, for example, spoof messages from a private cell phone number. If you are using `messaging_service_sid`, this parameter must be empty."
        },
        {
          "key": "AddressRetention",
          "value": "<string>",
          "description": "Determines if the address can be stored or obfuscated based on privacy settings (This can only be one of retain)",
          "disabled": true
        },
        {
          "key": "ApplicationSid",
          "value": "<string>",
          "description": "The SID of the application that should receive message status. We POST a `message_sid` parameter and a `message_status` parameter with a value of `sent` or `failed` to the [application](https://www.twilio.com/docs/usage/api/applications)'s `message_status_callback`. If a `status_callback` parameter is also passed, it will be ignored and the application's `message_status_callback` parameter will be used.",
          "disabled": true
        },
        {
          "key": "Attempt",
          "value": "<integer>",
          "description": "Total number of attempts made ( including this ) to send out the message regardless of the provider used",
          "disabled": true
        },
        {
          "key": "ContentRetention",
          "value": "<string>",
          "description": "Determines if the message content can be stored or redacted based on privacy settings (This can only be one of retain)",
          "disabled": true
        },
        {
          "key": "ForceDelivery",
          "value": "<boolean>",
          "description": "Reserved",
          "disabled": true
        },
        {
          "key": "MaxPrice",
          "value": "<number>",
          "description": "The maximum total price in US dollars that you will pay for the message to be delivered. Can be a decimal value that has up to 4 decimal places. All messages are queued for delivery and the message cost is checked before the message is sent. If the cost exceeds `max_price`, the message will fail and a status of `Failed` is sent to the status callback. If `MaxPrice` is not set, the message cost is not checked.",
          "disabled": true
        },
        {
          "key": "MediaUrl",
          "value": "[\"<uri>\",\"<uri>\"]",
          "description": "The URL of the media to send with the message. The media can be of type `gif`, `png`, and `jpeg` and will be formatted correctly on the recipient's device. The media size limit is 5MB for supported file types (JPEG, PNG, GIF) and 500KB for [other types](https://www.twilio.com/docs/sms/accepted-mime-types) of accepted media. To send more than one image in the message body, provide multiple `media_url` parameters in the POST request. You can include up to 10 `media_url` parameters per message. You can send images in an SMS message in only the US and Canada.",
          "disabled": true
        },
        {
          "key": "MessagingServiceSid",
          "value": "<string>",
          "description": "The SID of the [Messaging Service](https://www.twilio.com/docs/sms/services#send-a-message-with-copilot) you want to associate with the Message. Set this parameter to use the [Messaging Service Settings and Copilot Features](https://www.twilio.com/console/sms/services) you have configured and leave the `from` parameter empty. When only this parameter is set, Twilio will use your enabled Copilot Features to select the `from` phone number for delivery.",
          "disabled": true
        },
        {
          "key": "PersistentAction",
          "value": "[\"<string>\",\"<string>\"]",
          "description": "Rich actions for Channels Messages.",
          "disabled": true
        },
        {
          "key": "ProvideFeedback",
          "value": "<boolean>",
          "description": "Whether to confirm delivery of the message. Set this value to `true` if you are sending messages that have a trackable user action and you intend to confirm delivery of the message using the [Message Feedback API](https://www.twilio.com/docs/sms/api/message-feedback-resource). This parameter is `false` by default.",
          "disabled": true
        },
        {
          "key": "SmartEncoded",
          "value": "<boolean>",
          "description": "Whether to detect Unicode characters that have a similar GSM-7 character and replace them. Can be: `true` or `false`.",
          "disabled": true
        },
        {
          "key": "StatusCallback",
          "value": "<uri>",
          "description": "The URL we should call using the `status_callback_method` to send status information to your application. If specified, we POST these message status changes to the URL: `queued`, `failed`, `sent`, `delivered`, or `undelivered`. Twilio will POST its [standard request parameters](https://www.twilio.com/docs/sms/twiml#request-parameters) as well as some additional parameters including `MessageSid`, `MessageStatus`, and `ErrorCode`. If you include this parameter with the `messaging_service_sid`, we use this URL instead of the Status Callback URL of the [Messaging Service](https://www.twilio.com/docs/sms/services/api). URLs must contain a valid hostname and underscores are not allowed.",
          "disabled": true
        },
        {
          "key": "ValidityPeriod",
          "value": "<integer>",
          "description": "How long in seconds the message can remain in our outgoing message queue. After this period elapses, the message fails and we call your status callback. Can be between 1 and the default value of 14,400 seconds. After a message has been accepted by a carrier, however, we cannot guarantee that the message will not be queued after this period. We recommend that this value be at least 5 seconds.",
          "disabled": true
        }
      ]
    },
    "url": {
      "raw": "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}/2010-04-01/Accounts/:AccountSid/Messages.json",
      "host": [
        "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}"
      ],
      "path": [
        "2010-04-01",
        "Accounts",
        ":AccountSid",
        "Messages.json"
      ],
      "variable": [
        {
          "key": "AccountSid",
          "value": "{{TWILIO_ACCOUNT_SID}}",
          "description": "(Required) The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that will create the resource."
        }
      ]
    },
    "description": "Send a message from the account used to make the request with the phone number you purchased from Twilio in the previous step or with any other Twilio phone number you have associated in your account.\n\nRead more about it in the [Twilio Docs](https://www.twilio.com/docs/sms/api/message-resource#create-a-message-resource)"
  },
  "response": [
    {
      "name": "Created",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": "Added as a part of security scheme: basic",
            "key": "Authorization",
            "value": "Basic <credentials>"
          }
        ],
        "body": {
          "mode": "urlencoded",
          "urlencoded": [
            {
              "key": "To",
              "value": "<string>",
              "description": "The destination phone number in [E.164](https://www.twilio.com/docs/glossary/what-e164) format for SMS/MMS or [Channel user address](https://www.twilio.com/docs/sms/channels#channel-addresses) for other 3rd-party channels."
            },
            {
              "key": "AddressRetention",
              "value": "<string>",
              "description": "Determines if the address can be stored or obfuscated based on privacy settings (This can only be one of retain)"
            },
            {
              "key": "ApplicationSid",
              "value": "<string>",
              "description": "The SID of the application that should receive message status. We POST a `message_sid` parameter and a `message_status` parameter with a value of `sent` or `failed` to the [application](https://www.twilio.com/docs/usage/api/applications)'s `message_status_callback`. If a `status_callback` parameter is also passed, it will be ignored and the application's `message_status_callback` parameter will be used."
            },
            {
              "key": "Attempt",
              "value": "<integer>",
              "description": "Total number of attempts made ( including this ) to send out the message regardless of the provider used"
            },
            {
              "key": "Body",
              "value": "<string>",
              "description": "The text of the message you want to send. Can be up to 1,600 characters in length."
            },
            {
              "key": "ContentRetention",
              "value": "<string>",
              "description": "Determines if the message content can be stored or redacted based on privacy settings (This can only be one of retain)"
            },
            {
              "key": "ForceDelivery",
              "value": "<boolean>",
              "description": "Reserved"
            },
            {
              "key": "From",
              "value": "<string>",
              "description": "A Twilio phone number in [E.164](https://www.twilio.com/docs/glossary/what-e164) format, an [alphanumeric sender ID](https://www.twilio.com/docs/sms/send-messages#use-an-alphanumeric-sender-id), or a [Channel Endpoint address](https://www.twilio.com/docs/sms/channels#channel-addresses) that is enabled for the type of message you want to send. Phone numbers or [short codes](https://www.twilio.com/docs/sms/api/short-code) purchased from Twilio also work here. You cannot, for example, spoof messages from a private cell phone number. If you are using `messaging_service_sid`, this parameter must be empty."
            },
            {
              "key": "MaxPrice",
              "value": "<number>",
              "description": "The maximum total price in US dollars that you will pay for the message to be delivered. Can be a decimal value that has up to 4 decimal places. All messages are queued for delivery and the message cost is checked before the message is sent. If the cost exceeds `max_price`, the message will fail and a status of `Failed` is sent to the status callback. If `MaxPrice` is not set, the message cost is not checked."
            },
            {
              "key": "MediaUrl",
              "value": "[\"<uri>\",\"<uri>\"]",
              "description": "The URL of the media to send with the message. The media can be of type `gif`, `png`, and `jpeg` and will be formatted correctly on the recipient's device. The media size limit is 5MB for supported file types (JPEG, PNG, GIF) and 500KB for [other types](https://www.twilio.com/docs/sms/accepted-mime-types) of accepted media. To send more than one image in the message body, provide multiple `media_url` parameters in the POST request. You can include up to 10 `media_url` parameters per message. You can send images in an SMS message in only the US and Canada."
            },
            {
              "key": "MessagingServiceSid",
              "value": "<string>",
              "description": "The SID of the [Messaging Service](https://www.twilio.com/docs/sms/services#send-a-message-with-copilot) you want to associate with the Message. Set this parameter to use the [Messaging Service Settings and Copilot Features](https://www.twilio.com/console/sms/services) you have configured and leave the `from` parameter empty. When only this parameter is set, Twilio will use your enabled Copilot Features to select the `from` phone number for delivery."
            },
            {
              "key": "PersistentAction",
              "value": "[\"<string>\",\"<string>\"]",
              "description": "Rich actions for Channels Messages."
            },
            {
              "key": "ProvideFeedback",
              "value": "<boolean>",
              "description": "Whether to confirm delivery of the message. Set this value to `true` if you are sending messages that have a trackable user action and you intend to confirm delivery of the message using the [Message Feedback API](https://www.twilio.com/docs/sms/api/message-feedback-resource). This parameter is `false` by default."
            },
            {
              "key": "SmartEncoded",
              "value": "<boolean>",
              "description": "Whether to detect Unicode characters that have a similar GSM-7 character and replace them. Can be: `true` or `false`."
            },
            {
              "key": "StatusCallback",
              "value": "<uri>",
              "description": "The URL we should call using the `status_callback_method` to send status information to your application. If specified, we POST these message status changes to the URL: `queued`, `failed`, `sent`, `delivered`, or `undelivered`. Twilio will POST its [standard request parameters](https://www.twilio.com/docs/sms/twiml#request-parameters) as well as some additional parameters including `MessageSid`, `MessageStatus`, and `ErrorCode`. If you include this parameter with the `messaging_service_sid`, we use this URL instead of the Status Callback URL of the [Messaging Service](https://www.twilio.com/docs/sms/services/api). URLs must contain a valid hostname and underscores are not allowed."
            },
            {
              "key": "ValidityPeriod",
              "value": "<integer>",
              "description": "How long in seconds the message can remain in our outgoing message queue. After this period elapses, the message fails and we call your status callback. Can be between 1 and the default value of 14,400 seconds. After a message has been accepted by a carrier, however, we cannot guarantee that the message will not be queued after this period. We recommend that this value be at least 5 seconds."
            }
          ]
        },
        "url": {
          "raw": "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}/2010-04-01/Accounts/:AccountSid/Messages.json",
          "host": [
            "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}"
          ],
          "path": [
            "2010-04-01",
            "Accounts",
            ":AccountSid",
            "Messages.json"
          ],
          "variable": [
            {
              "key": "AccountSid"
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"account_sid\": \"ACF8\",\n \"api_version\": \"exercitation eiusmod amet laboris\",\n \"body\": \"consectetur exercitation officia magna\",\n \"date_created\": \"consectetur exercitation proident\",\n \"date_sent\": \"culpa sint labore\",\n \"date_updated\": \"proident\",\n \"direction\": \"outbound-call\",\n \"error_code\": -64742646,\n \"error_message\": \"nostrud sint Ut\",\n \"from\": \"Excepteur cillum aute\",\n \"messaging_service_sid\": \"MGDB\",\n \"num_media\": \"dolor veniam non\",\n \"num_segments\": \"et ut sint ut amet\",\n \"price\": \"Lorem reprehenderit\",\n \"price_unit\": \"Excepteur tempor i\",\n \"sid\": \"MM79\",\n \"status\": \"received\",\n \"subresource_uris\": {},\n \"to\": \"Ut non id\",\n \"uri\": \"magna sit esse\"\n}"
    }
  ]
}