How to Curl for text messaging

How to Curl for text messaging

Postby reinaldocrespo » Mon Feb 22, 2021 12:45 pm

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

Re: How to Curl for text messaging

Postby Maurizio » Mon Feb 22, 2021 1:33 pm

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

Maurizio


Code: Select all  Expand view
#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
Maurizio
 
Posts: 796
Joined: Mon Oct 10, 2005 1:29 pm

Re: How to Curl for text messaging

Postby reinaldocrespo » Mon Feb 22, 2021 7:42 pm

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

Re: How to Curl for text messaging

Postby nageswaragunupudi » Tue Feb 23, 2021 4:44 am

Mr. Reinaldo

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

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10248
Joined: Sun Nov 19, 2006 5:22 am
Location: India


Re: How to Curl for text messaging

Postby anserkk » Tue Feb 23, 2021 9:20 am

Many Many Happy Returns of the day, dear Reinaldo. :)
User avatar
anserkk
 
Posts: 1329
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: How to Curl for text messaging

Postby reinaldocrespo » Tue Feb 23, 2021 8:22 pm

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  Expand view

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

Re: How to Curl for text messaging

Postby Maurizio » Wed Feb 24, 2021 7:40 am

Happy Birthday Reinaldo ,

have you tried to use the service for WhatsApp?

Maurizio
User avatar
Maurizio
 
Posts: 796
Joined: Mon Oct 10, 2005 1:29 pm

Re: How to Curl for text messaging

Postby reinaldocrespo » Wed Feb 24, 2021 12:45 pm

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

Re: How to Curl for text messaging

Postby Otto » Thu Jul 15, 2021 12:05 pm

wassenger.com
Dear Antonio,
Do I understand this in the right manner.
You practically have to buy a service from wassengr.com that costs around 70 € / month to be able to use their API interface.
Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6005
Joined: Fri Oct 07, 2005 7:07 pm

Re: How to Curl for text messaging

Postby reinaldocrespo » Thu Jul 15, 2021 12:47 pm

Hello Everyone;

I ended up writing a windows service with Harbour. It runs 24/7 sending messages. To receive messages back, I wrote a short modharbour script that catches POST messages, which is how you can catch text messages sent to your number.

I'm using Twilio.com to send and receive text messages. I'm consuming about 300 outbound MSMs and about 150 inbound per day. I'm paying about $50 per month. The cost depends on the number of messages sent and received.

Twilio works worldwide and so far, it is working great for me.

And now that I see Otto's message here, I'm reminded I promised to share the code. Please send back any improvements you make on it. TY.

Code: Select all  Expand view

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

Re: How to Curl for text messaging

Postby Otto » Fri Jul 16, 2021 8:09 am

Dear Reinaldo,
Thank you for your source code and help.
I will start with using WhatsApp as a channel for communication in our company.
Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6005
Joined: Fri Oct 07, 2005 7:07 pm

Re: How to Curl for text messaging

Postby reinaldocrespo » Fri Jul 16, 2021 12:31 pm

Dear Otto;

I know Twilio also has an API to communicate via WhatApp, however I wonder, how do you plan on using WhatsApp? Is there a route you have tested and can recommend? or are you planning on using Twilio as well?

Thank you,
User avatar
reinaldocrespo
 
Posts: 972
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: How to Curl for text messaging

Postby Otto » Fri Jul 16, 2021 1:00 pm

Dear Reinaldo,
I'm brand new to WhatsApp. I only see that everyone here is using WhatsApp.
I thought it would be useful for our support, if I just have to say, take a picture of the screen. I then know exactly where the user is clicking, etc.

Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6005
Joined: Fri Oct 07, 2005 7:07 pm

Re: How to Curl for text messaging

Postby Patrick Mast » Wed Jul 20, 2022 3:06 pm

Hey guys,

What LIB's do I need to include to use CURL.
Do I also need CURL.exe in the same folder as my .exe?

Thanks!

Patrick
User avatar
Patrick Mast
 
Posts: 246
Joined: Sat Mar 03, 2007 8:42 pm

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 89 guests