DataFlex Requires Chilkat v11.0.0+
DataFlex
Send an SMS with Twilio
See more Twilio Examples
Submit an SMS with the sender, recipient, and text as form fields. This example starts with Twilio test credentials and its successful test sender. Twilio simulates the submission without sending or billing for a message. Test credentials and test numbers.
For live SMS, use the live Account SID in the URL, authenticate with an API key SID and secret, and use an SMS-capable Twilio sender and a recipient in E.164 format. HTTP 201 creates the message. Save its SID to check delivery status later.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
String sAccountSid
Handle hoUrl
Variant vReq
Handle hoReq
Variant vResp
Handle hoResp
Handle hoReply
Boolean iParsed
String sTemp1
Integer iTemp1
Integer iTemp2
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
// Use the test Account SID and test Auth Token from the Twilio Console.
Move "YOUR_TEST_ACCOUNT_SID" To sAccountSid
Set ComLogin Of hoHttp To sAccountSid
Set ComPassword Of hoHttp To "YOUR_TEST_AUTH_TOKEN"
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
// Add raw field values. Chilkat form-encodes them, including the + in a phone number.
Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
If (Not(IsComObjectCreated(hoReq))) Begin
Send CreateComObject of hoReq
End
Set ComHttpVerb Of hoReq To "POST"
Set ComContentType Of hoReq To "application/x-www-form-urlencoded"
Send ComAddParam To hoReq "From" "+15005550006"
Send ComAddParam To hoReq "To" "+12025550123"
Send ComAddParam To hoReq "Body" "Hello from Twilio!"
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
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoReq to vReq
Get pvComObject of hoResp to vResp
Get ComHttpReq Of hoHttp sTemp1 vReq 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 <> 201)) 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 "sid" To iTemp1
Get ComJsonTypeOf Of hoReply "status" To iTemp2
If ((iTemp1 <> 1) Or (iTemp2 <> 1)) Begin
Showln "The create response is missing the message SID or status."
Procedure_Return
End
Get ComStringOf Of hoReply "sid" To sTemp1
Showln "Message SID: " sTemp1
Get ComStringOf Of hoReply "status" To sTemp1
Showln "Initial status: " sTemp1
End_Procedure