How to Curl for text messaging

Re: How to Curl for text messaging

Postby TimStone » Wed Jul 20, 2022 4: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
User avatar
TimStone
 
Posts: 2944
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: How to Curl for text messaging

Postby NWKL » Sat Aug 13, 2022 11:31 am

Hi, i start with twilio, this code is working or have upgrades?
very thanks
NWKL
 
Posts: 26
Joined: Thu Aug 04, 2022 12:45 pm

Re: How to Curl for text messaging

Postby NWKL » 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: 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.
NWKL
 
Posts: 26
Joined: Thu Aug 04, 2022 12:45 pm

Re: How to Curl for text messaging

Postby byron.hopp » Tue Jul 16, 2024 7: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
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Re: How to Curl for text messaging

Postby reinaldocrespo » Tue Jul 16, 2024 7: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.
User avatar
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: How to Curl for text messaging

Postby byron.hopp » Tue Jul 16, 2024 8: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
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Re: How to Curl for text messaging

Postby reinaldocrespo » Tue Jul 16, 2024 9: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 [code][/code] otherwise it is hard to follow as you loose all indentation. Just my 2 cents.
User avatar
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: How to Curl for text messaging

Postby byron.hopp » Tue Jul 16, 2024 9: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
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Re: How to Curl for text messaging

Postby byron.hopp » Tue Jul 16, 2024 10:08 pm

Worked great, thank you my friend...
Thanks,
Byron Hopp
Matrix Computer Services
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Previous

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 86 guests