#include "hbcurl.ch"
#include "common.ch"
function SMS()
local aHeader := {}
local hCurl
local cUrl := ''
local cError, cResponse ,nResponseCode := 0
Local cTxt
TEXT INTO cTxt
{
"message_type": "N",
"message": "Hello world!",
"recipient": [
"+393896112878"
],
"sender": "Nipeservice",
"scheduled_delivery_time": "20200304143810",
"order_id": "123456789",
"returnCredits": true
}
ENDTEXT
//-------------------------------------------------------------------------------------------
hCurl := curl_easy_init()
if ! empty(hCurl)
//# Access token example
cUrl := "https://app.mobyt.it/API/v1.0/REST/token?username=MY_USERNAME&password=MY_PASSWORD"
curl_easy_setopt( hCurl, HB_CURLOPT_URL, cURL )
curl_easy_setopt( hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
curl_easy_setopt( hCurl, HB_CURLOPT_TRANSFERTEXT, .T. )
curl_easy_setopt( hCurl, HB_CURLOPT_FAILONERROR, .T. )
curl_easy_setopt( hCurl, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( hCurl, HB_CURLOPT_CONNECTTIMEOUT, 10 ) //20
curl_easy_perform( hCurl )
cResponse := curl_easy_dl_buff_get( hCurl )
nResponseCode := curl_easy_getinfo( hCurl, HB_CURLINFO_RESPONSE_CODE )
? nResponseCode
else
? "No handle"
endif
//-------------------------------------------------------------------------------------------
hCurl := curl_easy_init()
if ! empty(hCurl)
aHeader := {}
aadd(aHeader,"user_key: KEY_USER")
aadd(aHeader,"Access_token: KEY_ACCESS" )
aadd(aHeader,"Content-Type: application/json" )
cUrl := "https://app.mobyt.it/API/v1.0/REST/sms"
curl_easy_setopt(hCurl, HB_CURLOPT_URL, cUrl)
curl_easy_setopt(hCurl, HB_CURLOPT_POST, .T.)
curl_easy_setopt(hCurl, HB_CURLOPT_POSTFIELDS, cTxt)
curl_easy_setopt(hCurl, HB_CURLOPT_HTTPHEADER, aHeader)
curl_easy_setopt(hCurl, HB_CURLOPT_FRESH_CONNECT, .T.)
curl_easy_setopt(hCurl, HB_CURLOPT_FORBID_REUSE, .T.)
curl_easy_setopt(hCurl, HB_CURLOPT_VERBOSE, .F. )
curl_easy_setopt(hCurl, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. ) // per ssl
curl_easy_setopt( hCurl, HB_CURLOPT_TRANSFERTEXT, .T. )
curl_easy_setopt( hCurl, HB_CURLOPT_FAILONERROR, .T. )
curl_easy_setopt( hCurl, HB_CURLOPT_DL_BUFF_SETUP )
cError := curl_easy_perform( hCurl )
if empty(cError)
cResponse := curl_easy_dl_buff_get( hCurl )
MsgGet( 'Attenzione', 'Descrizione ',@cResponse )
else
? "Errore..:" + curl_easy_strerror(cError)
endif
else
? "No handle"
endif
if !empty(hCurl)
curl_global_cleanup( hCurl )
else
? "Error hCurl"
endif
if empty(cResponse)
? "Error, no response"
else
? cResponse
endif
return NIL
#include "hbcurl.ch"
#define ACCOUNT_SID "XxxxxxxxXxXXXX"
#define AUTH_TOKEN "Twilio-Acc-Token
#define TEL_FROM "YourTwilioNumber"
#define TEL_TO "555-555-5555"
function main()
Local hCurl
Local nError, httpcode
Local cUrl := "https://api.twilio.com/2010-04-01/Accounts/" + ACCOUNT_SID + "/Messages.json"
Local cPostFields := 'To=' + TEL_TO +;
"&From=" + TEL_FROM +;
"&Body=" + Time() + " SMS From TestTwilio.exe"
logdata( "trace.log", Replicate( "-", 80 ) )
logdata( "Trace.log", curl_version() )
logdata( "Trace.log", cPostFields )
curl_global_init()
if ! empty( hCurl := curl_easy_init() )
curl_easy_setopt( hCurl, HB_CURLOPT_POST, 1 )
curl_easy_setopt( hCurl, HB_CURLOPT_URL, cUrl )
curl_easy_setopt( hcurl, HB_CURLOPT_POSTFIELDS, cPostFields )
curl_easy_setopt( hCurl, HB_CURLOPT_USERPWD, ACCOUNT_SID + ':' + AUTH_TOKEN )
curl_easy_setopt( hCurl, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( hCurl, HB_CURLOPT_VERBOSE, 1 )
curl_easy_setopt( hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
nError := curl_easy_perform( hCurl )
curl_easy_getinfo( hCurl, HB_CURLINFO_RESPONSE_CODE, @httpcode )
logdata( "trace.log", { "nError = ", nError, ValType( nError ), "httpcode =", httpcode, ValType( httpcode ) } )
if nError = HB_CURLE_OK
logdata( "Trace.log", "SMS sent succesffully" )
logdata( "Trace.log", curl_easy_dl_buff_get( hCurl ) )
elseif httpcode != 200 .AND. httpcode != 201
logdata( "Trace.log", { "SMS send failed, HTTP Status Code ", httpcode } )
else
logdata( "Trace.log", curl_easy_strerror( nError ) )
endif
endif
curl_global_cleanup()
RETURN NIl
/*----------------------------------------------------------------------------
Faxes with Twilio
https://www.twilio.com/docs/fax/receive ... with-twiml
Sending emails using Twilio SendGrid
https://sendgrid.com/docs/for-developer ... g-started/
----------------------------------------------------------------------------*/
#INCLUDE "hbClass.ch"
#include "hbcurl.ch"
#define ACCOUNT_SID "xxxxxxxxxx$#B<SHJGHKxxxx"
#define AUTH_TOKEN "x@#$F&^%DEE"
#define TEL_FROM "555-555-55555"
CLASS TTwilioSMS
DATA hCurl
DATA httpcode
DATA cLogFile INIT "Twilio.log"
DATA cResponse
DATA cDestinationNum
DATA cSMSText
DATA cUrl INIT "https://api.twilio.com/2010-04-01/Accounts/" + ACCOUNT_SID + "/Messages.json"
DATA cVoiceUrl INIT "https://api.twilio.com/2010-04-01/Accounts/" + ACCOUNT_SID + "/Calls.json"
DATA nError INIT 0
DATA nMaxLogSize INIT 32768
DATA cDateStart, cDateEnd
DATA cvoiceMail, cReplyEmail
DATA lDebug INIT .F.
METHOD New()
METHOD End()
METHOD Send()
METHOD Reset()
METHOD MakePhoneCall()
METHOD GetMessagesLog()
ENDCLASS
//------------------------------------------------------------------------------------------------
METHOD New() CLASS TTwilioSMS
::hCurl := curl_easy_init()
RETURN Self
//------------------------------------------------------------------------------------------------
METHOD Send() CLASS TTwilioSMS
Local cPostFields
Local httpcode
curl_easy_setopt( ::hCurl, HB_CURLOPT_POST, 1 )
curl_easy_setopt( ::hCurl, HB_CURLOPT_URL, ::cUrl )
curl_easy_setopt( ::hCurl, HB_CURLOPT_USERPWD, ACCOUNT_SID + ':' + AUTH_TOKEN )
curl_easy_setopt( ::hCurl, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( ::hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
cPostFields := 'To=' + ::cDestinationNum + ; // Line[ 3 ] +;
'&From=' + TEL_FROM +;
'&Body=' + curl_easy_escape( ::hCurl, AllTrim( ::cSMSText ) )
If ::lDebug
LogFile( ::cLogFile, { cPostFields } )
endif
curl_easy_setopt( ::hcurl, HB_CURLOPT_POSTFIELDS, cPostFields )
::nError := curl_easy_perform( ::hCurl )
curl_easy_getinfo( ::hCurl, HB_CURLINFO_RESPONSE_CODE, @httpcode )
::httpcode := httpcode
IF ::nError = HB_CURLE_OK
::cResponse = curl_easy_dl_buff_get( ::hCurl )
Else
LogData( ::cLogFile, { "Twilio error sending SMS. Details below:" }, ::nMaxLogSize )
LogData( ::cLogFile, { "To", ::cDestinationNum, "SMS Text:", ::cSMSText }, ::nMaxLogSize )
LogData( ::cLogFile, { "Error Num:", ::nError, "Httpcode:", ::httpcode }, ::nMaxLogSize )
LogData( ::cLogFile, curl_easy_strerror( ::nError ), ::nMaxLogSize )
ENDIF
return NIL
//------------------------------------------------------------------------------------------------
METHOD Reset() CLASS TTwilioSMS
curl_easy_reset( ::hCurl )
return NIL
//------------------------------------------------------------------------------------------------
METHOD End() CLASS TTwilioSMS
curl_easy_cleanup( ::hCurl )
::hCurl := Nil
hb_gcAll( .t. )
return NIL
//------------------------------------------------------------------------------------------------
//must define ::cDateStart and ::cDateEnd before calling this method.
METHOD GetMessagesLog() CLASS TTwilioSMS
Local cparms := '?DateSent%3E=' + ::cDateStart + "&DateSent%3C=" + ::cDateEnd + "&PageSize=600"
Local httpcode
curl_easy_setopt( ::hCurl, HB_CURLOPT_HTTPGET, 1 )
curl_easy_setopt( ::hCurl, HB_CURLOPT_URL, ::cUrl + cParms )
curl_easy_setopt( ::hCurl, HB_CURLOPT_USERPWD, ACCOUNT_SID + ':' + AUTH_TOKEN )
curl_easy_setopt( ::hCurl, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( ::hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
::nError := curl_easy_perform( ::hCurl )
curl_easy_getinfo( ::hCurl, HB_CURLINFO_RESPONSE_CODE, @httpcode )
::httpcode := httpcode
IF ::nError = HB_CURLE_OK
::cResponse = curl_easy_dl_buff_get( ::hCurl )
::cResponse := StrTran( ::cResponse, Chr(10), "" )
Else
LogData( ::cLogFile, { "Twilio error sending SMS. Details below:" }, ::nMaxLogSize )
LogData( ::cLogFile, { "To", ::cDestinationNum, "SMS Text:", ::cSMSText }, ::nMaxLogSize )
LogData( ::cLogFile, { "Error Num:", ::nError, "Httpcode:", ::httpcode }, ::nMaxLogSize )
LogData( ::cLogFile, curl_easy_strerror( ::nError ), ::nMaxLogSize )
ENDIF
//MemoWrit( "Twilio.log", ::cResponse )
RETURN NIL
//------------------------------------------------------------------------------------------------
/*sample makeing a call using url to fetch parameters
https://www.twilio.com/docs/voice/api/call-resource
EXCLAMATION_MARK='!'
curl -X POST https://api.twilio.com/2010-04-01/Accou ... Calls.json \
--data-urlencode "Twiml=<Response><Say>Ahoy there$EXCLAMATION_MARK</Say></Response>" \
--data-urlencode "To=+15558675310" \
--data-urlencode "From=+15552223214" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
*/
//Twilml sample parameter "Twiml=<Response><Say>Ahoy there$EXCLAMATION_MARK</Say></Response>"
METHOD MakePhoneCall() CLASS TTwilioSMS
Local cPostFields
Local httpcode
curl_easy_setopt( ::hCurl, HB_CURLOPT_POST, 1 )
curl_easy_setopt( ::hCurl, HB_CURLOPT_URL, ::cVoiceUrl )
curl_easy_setopt( ::hCurl, HB_CURLOPT_USERPWD, ACCOUNT_SID + ':' + AUTH_TOKEN )
curl_easy_setopt( ::hCurl, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( ::hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
cPostFields := 'To=' + ::cDestinationNum + ; // Line[ 3 ] +;
'&From=' + TEL_FROM +;
'&CallReason=' + "medical appointment reminder" +;
'&Twiml=<Say voice="alice" language="es-MX">' + ::cVoiceMail + '</Say>' +;
'&CallerId=' + ::cReplyEmail
curl_easy_setopt( ::hcurl, HB_CURLOPT_POSTFIELDS, cPostFields )
::nError := curl_easy_perform( ::hCurl )
curl_easy_getinfo( ::hCurl, HB_CURLINFO_RESPONSE_CODE, @httpcode )
::httpcode := httpcode
IF ::nError = HB_CURLE_OK
::cResponse = curl_easy_dl_buff_get( ::hCurl )
Else
LogData( ::cLogFile, { "Twilio error making voice call. Details below:" }, ::nMaxLogSize )
LogData( ::cLogFile, { "To", ::cDestinationNum, "Message:", ::cVoiceMail }, ::nMaxLogSize )
LogData( ::cLogFile, { "Error Num:", ::nError, "Httpcode:", ::httpcode }, ::nMaxLogSize )
LogData( ::cLogFile, curl_easy_strerror( ::nError ), ::nMaxLogSize )
ENDIF
RETURN NIL
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: Google [Bot], Marc Venken and 83 guests