How to Curl for text messaging

Post Reply
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

How to Curl for text messaging

Post by reinaldocrespo »

Hello everyone;

I pretend to use hb_curl() to send SMS messages with Twilio. Here is an example they provide on how to send a message using command line:

curl -X POST https://api.twilio.com/2010-04-01/Accou ... sages.json \
--data-urlencode "From=+15017122661" \
--data-urlencode "Body=body" \
--data-urlencode "To=+15558675310" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

Can someone show how to execute the same curl command but using Harbour hb_curl? Or is there a better way not using Curl?

Thank you.

Reinaldo.
User avatar
Maurizio
Posts: 705
Joined: Mon Oct 10, 2005 1:29 pm
Contact:

Re: How to Curl for text messaging

Post by Maurizio »

Ciao Reinaldo
I use this for send SMS with mobyt.it via cURL

Maurizio

Code: Select all

#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
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: How to Curl for text messaging

Post by reinaldocrespo »

Hello Maurizio;

Thank you for the code. It helps a lot.

How much guitar are you playing lately?

I decided to take the time everyday to practice. I'd love to see you play again and steal some of your songs. :-)

Reinaldo.
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: How to Curl for text messaging

Post by nageswaragunupudi »

Mr. Reinaldo

We all wish you a Happy Birthday and many happy returns of the day.
Regards

G. N. Rao.
Hyderabad, India
User avatar
anserkk
Posts: 1280
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: How to Curl for text messaging

Post by anserkk »

Many Many Happy Returns of the day, dear Reinaldo. :)
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: How to Curl for text messaging

Post by reinaldocrespo »

Thank you very very very much.

I hope to see you all this time around the sun.

Below is short sample on how to send text messages with Twilio using curl.

Code: Select all

#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
 
Twilio sends any replies as an http post web hook. So you would need to have an http server capable of receiving posts. After writing this whole project with Java, I decided I did not want to keep maintaining Java code. So I rewrote the whole thing with harbour and for the http server I was able to have it work with pure mod_harbor under Apache. With this combination you can setup a full automated text and reply responses using Twilio.

My implementation sends medical appointment reminder texts to patients cell numbers. The patient can reply 1 to confirm or 2 to cancel. The sending texts program is a harbour service. The replies receiving app is all mod_harbour running under Apache.

Hope it helps.

Reinaldo.

Reinaldo.
User avatar
Maurizio
Posts: 705
Joined: Mon Oct 10, 2005 1:29 pm
Contact:

Re: How to Curl for text messaging

Post by Maurizio »

Happy Birthday Reinaldo ,

have you tried to use the service for WhatsApp?

Maurizio
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: How to Curl for text messaging

Post by reinaldocrespo »

Maurizio;

I have not used it for whatsApp but the only thing that changes is the url. Same for voice or automated Chat. With my sample all you would have to change is the url to consume any of their other services via Curl.

They have samples showing how to use their services using Node Js, PHP, Ruby, C#, Python, and Java. All their samples require that you install their SDK. By using Curl you don't have to be concerned with installing any SDK.

I initially wrote the whole thing using Java because ADS has a driver to connect to my dbfs and adts. But I grew frustrated with Java and all its dependencies. Maintaining a fine running Java environment is too much work. That's the reason I re-wrote the client with Harbour using hbcurl and with Mod_harbour for the server side that receives replies.

I will take some time later today to write and post here a small sample of the server side written with mod_harbor under Apache to receive replies.


Reinaldo.
Post Reply