PureBasic
PureBasic
CallRail API - Create an Outbound Phone Call
See more CallRail Examples
Initiates an outbound call from the target account.Chilkat PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
; 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
; Implements the following CURL command:
; curl -H "Authorization: Token token={api_token}" \
; -X POST \
; -H "Content-Type: application/json" \
; -d '{
; "caller_id": "+17703334455",
; "business_phone_number": "+14045556666",
; "customer_phone_number": "+14044442233",
; "recording_enabled": true,
; "outbound_greeting_recording_url": "http://www.test.com/greeting.mp3",
; "outbound_greeting_text": "These are not the droids you are looking for."
; }' \
; "https://api.callrail.com/v3/a/{account_id}/calls.json"
; Use the following online tool to generate HTTP code from a CURL command
; Convert a cURL Command to HTTP Source Code
; Use this online tool to generate code from sample JSON:
; Generate Code to Create JSON
; The following JSON is sent in the request body.
; {
; "caller_id": "+17703334455",
; "business_phone_number": "+14045556666",
; "customer_phone_number": "+14044442233",
; "recording_enabled": true,
; "outbound_greeting_recording_url": "http://www.test.com/greeting.mp3",
; "outbound_greeting_text": "These are not the droids you are looking for."
; }
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"caller_id","+17703334455")
CkJsonObject::ckUpdateString(json,"business_phone_number","+14045556666")
CkJsonObject::ckUpdateString(json,"customer_phone_number","+14044442233")
CkJsonObject::ckUpdateBool(json,"recording_enabled",1)
CkJsonObject::ckUpdateString(json,"outbound_greeting_recording_url","http://www.test.com/greeting.mp3")
CkJsonObject::ckUpdateString(json,"outbound_greeting_text","These are not the droids you are looking for.")
CkHttp::ckSetRequestHeader(http,"Authorization","Token token={api_token}")
CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpJson(http,"POST","https://api.callrail.com/v3/a/{account_id}/calls.json",json,"application/json",resp)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(json)
CkHttpResponse::ckDispose(resp)
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)
respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
Debug "Response Status Code = " + Str(respStatusCode)
If respStatusCode >= 400
Debug "Response Header:"
Debug CkHttpResponse::ckHeader(resp)
Debug "Failed."
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(json)
CkHttpResponse::ckDispose(resp)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndIf
; Sample JSON response:
; (Sample code for parsing the JSON response is shown below)
; {
; "answered": null,
; "business_phone_number": "+19044567899",
; "customer_city": "Atlanta",
; "customer_country": "US",
; "customer_name": null,
; "customer_phone_number": "+14703444700",
; "customer_state": "GA",
; "direction": "outbound",
; "duration": null,
; "id": "CAL8154748ae6bd4e278a7cddd38a662f4f",
; "recording": null,
; "recording_duration": null,
; "start_time": "2017-02-22T15:02:24.916-05:00",
; "tracking_phone_number": "+19044567899",
; "voicemail": false
; }
; Sample code for parsing the JSON response...
; Use the following online tool to generate parsing code from sample JSON:
; Generate Parsing Code from JSON
answered.s = CkJsonObject::ckStringOf(jResp,"answered")
business_phone_number.s = CkJsonObject::ckStringOf(jResp,"business_phone_number")
customer_city.s = CkJsonObject::ckStringOf(jResp,"customer_city")
customer_country.s = CkJsonObject::ckStringOf(jResp,"customer_country")
customer_name.s = CkJsonObject::ckStringOf(jResp,"customer_name")
customer_phone_number.s = CkJsonObject::ckStringOf(jResp,"customer_phone_number")
customer_state.s = CkJsonObject::ckStringOf(jResp,"customer_state")
direction.s = CkJsonObject::ckStringOf(jResp,"direction")
duration.s = CkJsonObject::ckStringOf(jResp,"duration")
id.s = CkJsonObject::ckStringOf(jResp,"id")
recording.s = CkJsonObject::ckStringOf(jResp,"recording")
recording_duration.s = CkJsonObject::ckStringOf(jResp,"recording_duration")
start_time.s = CkJsonObject::ckStringOf(jResp,"start_time")
tracking_phone_number.s = CkJsonObject::ckStringOf(jResp,"tracking_phone_number")
voicemail.i = CkJsonObject::ckBoolOf(jResp,"voicemail")
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(json)
CkHttpResponse::ckDispose(resp)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndProcedure