FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How to Curl for text messaging
Posts: 3022
Joined: Fri Oct 07, 2005 01:45 PM
Re: How to Curl for text messaging
Posted: Wed Jul 20, 2022 04:47 PM

I find this interesting, but I'm curious about the receiving of text messages.

It appears I would need to setup a server to receive them. I actually want my clients to send and receive them independently from me. They do not have available servers.

Is there any further work done on this topic, and especially the receiving ?

Curl is not an issue for me. I already use it for JSON interfaces.

Tim

Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
Posts: 38
Joined: Thu Aug 04, 2022 12:45 PM
Re: How to Curl for text messaging
Posted: Sat Aug 13, 2022 11:31 AM

Hi, i start with twilio, this code is working or have upgrades?
very thanks

Posts: 38
Joined: Thu Aug 04, 2022 12:45 PM
Re: How to Curl for text messaging
Posted: Sat Aug 13, 2022 11:32 AM
Hi, i will start with twilio, this code is working? have upgrades? can you share?

very thanks

reinaldocrespo wrote: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 (fw): Select all Collapse
#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.
Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM
Re: How to Curl for text messaging
Posted: Tue Jul 16, 2024 07:15 PM

I have switched to Twilio, cut all ties with EZTexting and utilizing the code you all provided I can send a text. Thank you. However my program shuts down upon the return statement. What do I need in the Mak file for cUrl. I have currently LibCurl.lib, and hbcurl.lib in the .mak, I include hbcurl.ch in my program, and I have libcurl.dll in the application directory.

Thanks,

Byron Hopp

Matrix Computer Services
Posts: 990
Joined: Thu Nov 17, 2005 05:49 PM
Re: How to Curl for text messaging
Posted: Tue Jul 16, 2024 07:30 PM

If the program successfully sends texts via Twilio and then you get a runtime error when closing, the problem is most likely in your code and it has nothing to do with your make file. Try finding the exact line that causes de GPF or write and share a reduced sample code that replicates the runtime error.

Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM
Re: How to Curl for text messaging
Posted: Tue Jul 16, 2024 08:07 PM

Here is my code, modified yours a little. Had to use my logging function. Using the Twilio API seems to work great, it the crashing of the program I can't figure out. Thanks, in advance. Line number in the hb_out.log file leads me to the "Return nil" statement. Error 6005: Exception error:

Function TwilioText( cTel_To,cBody )

Local cLogFile := "TraceLog.txt"

Local hCurl

Local nError

Local Httpcode

Local cUrl := "https:" + "/" + "/" + "api.twilio.com/2010-04-01/Accounts/" + ACCOUNT_SID + "/Messages.json"

Local cPostFields := 'To=' + cTel_To + chr(38) + "From=" + TEL_FROM + chr(38) + "Body=" + Time() + " " + cBody

Logit( cLogFile, Replicate( "-", 80 ) )

Logit( cLogFile, curl_version() )

Logit( cLogFile, 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 )

  Logit( cLogFile, "HttpCode =", nAllTrim( HttpCode ) )

  if nError = HB_CURLE_OK

     Logit( cLogFile, "SMS sent succesffully" )

     Logit( cLogFile, curl_easy_dl_buff_get( hCurl ) )

  elseif Httpcode != 200 .AND. Httpcode != 201

     Logit( cLogFile, { "SMS send failed, HTTP Status Code ", Httpcode } )

  else

     Logit( cLogFile, curl_easy_strerror( nError ) )

  endif

endif

curl_global_cleanup()

Return nil

Thanks,

Byron Hopp

Matrix Computer Services
Posts: 990
Joined: Thu Nov 17, 2005 05:49 PM
Re: How to Curl for text messaging
Posted: Tue Jul 16, 2024 09:33 PM
curl_global_init() and curl_global_cleanup() should be called only once in the program. Throw them away from the function.

Have a main function as an entry point. For example: Main(). And from that Main() entry point call curl_global_init.... then call your function as many times as needed. Then before exiting main() func, call curl_global_cleanup().

When sharing code it works best if sent as otherwise it is hard to follow as you loose all indentation. Just my 2 cents.
Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM
Re: How to Curl for text messaging
Posted: Tue Jul 16, 2024 09:59 PM

Your 2 (two) cents is golden. I have been on this board for more years that I would like to admit and I never knew how to do that. I will try to remember. I am going to try your fix now. Thanks,

Thanks,

Byron Hopp

Matrix Computer Services
Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM
Re: How to Curl for text messaging
Posted: Tue Jul 16, 2024 10:08 PM

Worked great, thank you my friend...

Thanks,

Byron Hopp

Matrix Computer Services

Continue the discussion