Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Twilio Send SMS (using Chilkat HTTP)Send an outgoing SMS message. For more information, see https://www.twilio.com/docs/sms/api/message-resource#create-a-message-resource
IncludeFile "CkHttpResponse.pb" IncludeFile "CkHttp.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkHttpRequest.pb" Procedure ChilkatExample() ; This example assumes the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i ; Implements the following CURL command: ; (See information about using test credentials and phone numbers: https://www.twilio.com/docs/iam/test-credentials) ; curl -X POST https://api.twilio.com/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json \ ; --data-urlencode "From=+15005550006" \ ; --data-urlencode "Body=body" \ ; --data-urlencode "To=+15005551212" \ ; -u TWILIO_ACCOUNT_SID:TWILIO_AUTH_TOKEN ; Use the following online tool to generate HTTP code from a CURL command ; Convert a cURL Command to HTTP Source Code CkHttp::setCkLogin(http, "TWILIO_ACCOUNT_SID") CkHttp::setCkPassword(http, "TWILIO_AUTH_TOKEN") req.i = CkHttpRequest::ckCreate() If req.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttpRequest::setCkHttpVerb(req, "POST") CkHttpRequest::setCkPath(req, "/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json") CkHttpRequest::setCkContentType(req, "application/x-www-form-urlencoded") CkHttpRequest::ckAddParam(req,"From","+15005550006") CkHttpRequest::ckAddParam(req,"Body","body") CkHttpRequest::ckAddParam(req,"To","+15005551212") resp.i = CkHttp::ckPostUrlEncoded(http,"https://api.twilio.com/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json",req) If CkHttp::ckLastMethodSuccess(http) = 0 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) ProcedureReturn EndIf sbResponseBody.i = CkStringBuilder::ckCreate() If sbResponseBody.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttpResponse::ckGetBodySb(resp,sbResponseBody) jResp.i = CkJsonObject::ckCreate() If jResp.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckLoadSb(jResp,sbResponseBody) CkJsonObject::setCkEmitCompact(jResp, 0) Debug "Response Body:" Debug CkJsonObject::ckEmit(jResp) ; A 201 status code indicates success. respStatusCode.i = CkHttpResponse::ckStatusCode(resp) Debug "Response Status Code = " + Str(respStatusCode) If respStatusCode >= 400 Debug "Response Header:" Debug CkHttpResponse::ckHeader(resp) Debug "Failed." CkHttpResponse::ckDispose(resp) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) ProcedureReturn EndIf CkHttpResponse::ckDispose(resp) ; Sample JSON response: ; (Sample code for parsing the JSON response is shown below) ; { ; "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", ; "api_version": "2010-04-01", ; "body": "body", ; "date_created": "Thu, 30 Jul 2015 20:12:31 +0000", ; "date_sent": "Thu, 30 Jul 2015 20:12:33 +0000", ; "date_updated": "Thu, 30 Jul 2015 20:12:33 +0000", ; "direction": "outbound-api", ; "error_code": null, ; "error_message": null, ; "from": "+15017122661", ; "messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", ; "num_media": "0", ; "num_segments": "1", ; "price": null, ; "price_unit": null, ; "sid": "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", ; "status": "sent", ; "subresource_uris": { ; "media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json" ; }, ; "to": "+15558675310", ; "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json" ; } ; Sample code for parsing the JSON response... ; Use the following online tool to generate parsing code from sample JSON: ; Generate Parsing Code from JSON account_sid.s = CkJsonObject::ckStringOf(jResp,"account_sid") api_version.s = CkJsonObject::ckStringOf(jResp,"api_version") body.s = CkJsonObject::ckStringOf(jResp,"body") date_created.s = CkJsonObject::ckStringOf(jResp,"date_created") date_sent.s = CkJsonObject::ckStringOf(jResp,"date_sent") date_updated.s = CkJsonObject::ckStringOf(jResp,"date_updated") direction.s = CkJsonObject::ckStringOf(jResp,"direction") error_code.s = CkJsonObject::ckStringOf(jResp,"error_code") error_message.s = CkJsonObject::ckStringOf(jResp,"error_message") from.s = CkJsonObject::ckStringOf(jResp,"from") messaging_service_sid.s = CkJsonObject::ckStringOf(jResp,"messaging_service_sid") num_media.s = CkJsonObject::ckStringOf(jResp,"num_media") num_segments.s = CkJsonObject::ckStringOf(jResp,"num_segments") price.s = CkJsonObject::ckStringOf(jResp,"price") price_unit.s = CkJsonObject::ckStringOf(jResp,"price_unit") sid.s = CkJsonObject::ckStringOf(jResp,"sid") status.s = CkJsonObject::ckStringOf(jResp,"status") subresource_urisMedia.s = CkJsonObject::ckStringOf(jResp,"subresource_uris.media") v_to.s = CkJsonObject::ckStringOf(jResp,"to") uri.s = CkJsonObject::ckStringOf(jResp,"uri") CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.