Xbasic

twilio_send_sms Function

Syntax

L result = twilio_send_sms(pResult as p, to as c, message as c, namedResource as c [, from = "" , accountSID = "" , authCode = ""])

Arguments

pResultPointer

An object that contains return information provided by Twilio. For example, if the SMS fails ( result = .f.) the pResult object will have a property explaining why the SMS failed.

headersCharacter

Headers returned by the call.

contentsCharacter

The result of the call to Twilio. This may be a JSON object that contains additional details about the outcome of sending an sms message. For example:

dim to as c
to = "+15551111"
dim message as c = "Sent from Xbasic at: " + now()

dim namedResource as c = "twilio"
dim pResult as p
dim flag as l
flag = twilio_send_sms(pResult,to,message,namedResource)

? flag
= .F.

? pResult.contents
= {"code": 21211, "message": "The 'To' number +15551111 is not a valid phone number.", "more_info": "https://www.twilio.com/docs/errors/21211", "status": 400}
errorsCharacter

If an error occurs while trying to send an sms using twilio, contains information about the error. Note that sending the sms may fail and errors may not exist. Check the information returned in contents for more information if twilio_send_sms() fails but no errors variable exists for pResult.

toCharacter

The number to send the SMS message.

messageCharacter

The message to send.

namedResourceCharacter

The Named Resource that defines your Twilio settings.

Set to an empty string if you want to explicitly set the account SID, authorization token, and from arguments.

fromCharacter

The phone number sending the message. Required if namedResource is an empty string.

accountSIDCharacter

Your Twilio account SID. Required if namedResource is an empty string.

authCodeCharacter

Your Twilio authorization token. Required if namedResource is an empty string.

Returns

resultLogical

Returns .t. if the operation succeeds, otherwise .f.. twilion_send_sms() will return false if the Twilio endpoint cannot be reached or if Twilio reports an error. The pResult argument will contain additional details if an error occurs.

Description

Send SMS messages from Xbasic to a mobile phone using Twilio. Requires a Twilio account and a phone number authorized to send SMS messages.

Discussion

The twilio_send_sms() function sends an sms to a phone number using the Twilio messaging service.

Before you can use this function, you must first set up an account with Twilio. You must also own a phone number that is authorized to send SMS messages. Go to www.twilio.com to setup your Twilio account and learn more.

Once you have a Twilio account and phone number, Twilio will give you the following items that are needed to use the twilio_send_sms() function:

  • AccountSID
  • Authorization Token

When you use the twilio_send_sms() function, you can either pass in the values for the account SID, the authorization token, and 'From' phone number explicitly, or you can create an entry in the Named Providers property in Project Properties to store these values and pass the name of the stored settings to the function (the named resource).

Using Named Resources is the preferred method because you can redefine any of the parameter values in the stored setting when you define a publishing profile.

If you explicitly set the accountSID, authCode and from arguments, set the namedResource argument must be set to an empty string.

pResult is an object that contains return information provided by Twilio. For example, if the SMS fails (twilio_send_sms() returns .f.) the pResult object will have a property explaining why the SMS failed.

See Resource Providers to learn how to create a named resource provider.

dim to as c
to = "+15035556802"
dim message as c = "Sent from Xbasic at: " + now()

dim namedResource as c = "twilio"
dim pResult as p
dim flag as l
flag = twilio_send_sms(pResult,to,message,namedResource)

? flag
= .T.

? pResult
= contents = {"body": "Sent from your Twilio trial account - Sent from Xbasic at: 10/20/2022 05:19:26 92 pm", "num_segments": "1", "direction": "outbound-api", "from": "+18586820213", "date_updated": "Thu, 20 Oct 2022 21:19:27 +0000", "price": null, "error_message": null, "uri": "/2010-04-01/Accounts/ACe133cc37dd06eed6f8a329a18184c718/Messages/SM5bf9d0e3a123a04366d97b5bfaaba37e.json", "account_sid": "ACe133cc37dd06eed6f8a329a18184c718", "num_media": "0", "to": "+15035556802", "date_created": "Thu, 20 Oct 2022 21:19:27 +0000", "status": "queued", "sid": "SM5bf9d0e3a123a04366d97b5bfaaba37e", "date_sent": null, "messaging_service_sid": null, "error_code": null, "price_unit": "USD", "api_version": "2010-04-01", "subresource_uris": {"media": "/2010-04-01/Accounts/ACe133cc37dd06eed6f8a329a18184c718/Messages/SM5bf9d0e3a123a04366d97b5bfaaba37e/Media.json"}}
headers = ""

See Also