Chilkat Online Tools

PowerBuilder / Twilio Messaging / SMS / Send an SMS

Back to Collection Items

integer li_rc
oleobject loo_Http
integer li_Success
oleobject loo_Req
oleobject loo_JsonParam2
oleobject loo_JsonParam3
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
oleobject loo_Date_created
oleobject loo_Date_sent
oleobject loo_Date_updated
string ls_Account_sid
string ls_Api_version
string ls_Body
string ls_Direction
integer li_Error_code
string ls_Error_message
string ls_From
string ls_Messaging_service_sid
string ls_Num_media
string ls_Num_segments
string ls_Price
string ls_Price_unit
string ls_Sid
string ls_Status
string ls_V_to
string ls_Uri

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat_9_5_0.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Http.BasicAuth = 1
loo_Http.Login = "{{TWILIO_ACCOUNT_SID}}"
loo_Http.Password = "{{TWILIO_AUTH_TOKEN}}"

loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat_9_5_0.HttpRequest")

loo_Req.AddParam("Body","Hello World!")

loo_JsonParam2 = create oleobject
li_rc = loo_JsonParam2.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_Req.AddParam("To",loo_JsonParam2.Emit())

loo_JsonParam3 = create oleobject
li_rc = loo_JsonParam3.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_Req.AddParam("From",loo_JsonParam3.Emit())

loo_Resp = loo_Http.PostUrlEncoded("https://api.twilio.com/2010-04-01/Accounts/:AccountSid/Messages.json",loo_Req)
if loo_Http.LastMethodSuccess = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_JsonParam2
    destroy loo_JsonParam3
    return
end if

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

loo_Resp.GetBodySb(loo_SbResponseBody)

loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Resp.Header
    Write-Debug "Failed."
    destroy loo_Resp
    destroy loo_Http
    destroy loo_Req
    destroy loo_JsonParam2
    destroy loo_JsonParam3
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

destroy loo_Resp

// 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

loo_Date_created = create oleobject
li_rc = loo_Date_created.ConnectToNewObject("Chilkat_9_5_0.DtObj")

loo_Date_sent = create oleobject
li_rc = loo_Date_sent.ConnectToNewObject("Chilkat_9_5_0.DtObj")

loo_Date_updated = create oleobject
li_rc = loo_Date_updated.ConnectToNewObject("Chilkat_9_5_0.DtObj")

ls_Account_sid = loo_JResp.StringOf("account_sid")
ls_Api_version = loo_JResp.StringOf("api_version")
ls_Body = loo_JResp.StringOf("body")
loo_JResp.DtOf("date_created",0,loo_Date_created)
loo_JResp.DtOf("date_sent",0,loo_Date_sent)
loo_JResp.DtOf("date_updated",0,loo_Date_updated)
ls_Direction = loo_JResp.StringOf("direction")
li_Error_code = loo_JResp.IntOf("error_code")
ls_Error_message = loo_JResp.StringOf("error_message")
ls_From = loo_JResp.StringOf("from")
ls_Messaging_service_sid = loo_JResp.StringOf("messaging_service_sid")
ls_Num_media = loo_JResp.StringOf("num_media")
ls_Num_segments = loo_JResp.StringOf("num_segments")
ls_Price = loo_JResp.StringOf("price")
ls_Price_unit = loo_JResp.StringOf("price_unit")
ls_Sid = loo_JResp.StringOf("sid")
ls_Status = loo_JResp.StringOf("status")
ls_V_to = loo_JResp.StringOf("to")
ls_Uri = loo_JResp.StringOf("uri")


destroy loo_Http
destroy loo_Req
destroy loo_JsonParam2
destroy loo_JsonParam3
destroy loo_SbResponseBody
destroy loo_JResp
destroy loo_Date_created
destroy loo_Date_sent
destroy loo_Date_updated

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}"
    }
  ]
}