On Aug 5, 2019, at 2:00 PM, Jim Labos wrote:

> Hi Tim (or anyone else that could help),
> 
> I am looking for a quick and simple SMS solution and came upon your (Tim's) 
> message that you possibly had gotten some HTTP code snippets to send SMS via
> 4D using Twilio.
> 
> I tried and was unsuccessful probably because of syntax so I gave up and
> moved on. I am trying Clickatell soon.
> 
> If you have a working example for Twilio I would much appreciate looking at
> it.
> 
> Thanks in advance.

Hi Jim,

I got some code from Justin Leavens that David Adams had created using NTK long 
before we had 4D’s HTTP Request command. I modified and adapted it to use HTTP 
Request since my project did not use NTK. 

There are 2 utility methods. StripBadCharacters makes sure the message text has 
not “bad” characters. Twilio_ConvertDateToDTS converts the human readable date 
time to a normally formatted DTS. 

  // ===========================================
  // PROJECT METHOD: Twilio_SendSMS

  // PARAMETERS: $0 = object with results
  // $1 = to phone number
  // $2 = message text

  // DESCRIPTION: Send an SMS via the Twilio service.

  // 
https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxx1ee6776/SMS/Messages.json

  // CREATED BY: David Adams (provided to me by Justin Leavens)
  // DATE: Unknown
  // LAST MODIFIED: 8/31/18 changed it from using NTK to 4D HTTP Request
  // made it conform to my programming style
  // ============================================

C_OBJECT($0;$result_o)
C_TEXT($1;$toPhoneNumber_t)
C_TEXT($2;$message_t)
$toPhoneNumber_t:=$1
$message_t:=$2

  // declare local variables
C_TEXT($errorMessage_t;$fromPhoneNumber_t;$toPhoneNumber_t;$message_t;$toPhoneDialingCode_t)
C_TEXT($twilioAccountSID_t;$twilioAuthToken_t;$requestURL_t;$content_t;$fromPhoneNumber_t)
C_LONGINT($statusCode_l)
C_OBJECT($response_o)
ARRAY TEXT($headerName_at;0)
ARRAY TEXT($headerValue_at;0)

  // remove any bad characters from the message
StripBadCharacters (->$message_t)  // Control characters can mess up Twilio as 
it likes things XML safe. 

  // Encode & characters (all the other special URL characters seem to be OK 
with Twilio)
$message_t:=Replace string($message_t;"&";"%26")

  // use basic authentication
$fromPhoneNumber_t:=<>twilioPhoneNumber_t
$twilioAccountSID_t:=<>twilioAccountSID_t
$twilioAuthToken_t:=<>twilioAuthToken_t
HTTP AUTHENTICATE($twilioAccountSID_t;$twilioAuthToken_t;HTTP basic)

  // strip any phone number formatting
$toPhoneNumber_t:=Replace string($toPhoneNumber_t;"(";"")  // strip "("
$toPhoneNumber_t:=Replace string($toPhoneNumber_t;")";"")  // strip ")"
$toPhoneNumber_t:=Replace string($toPhoneNumber_t;"-";"")  // strip "-"
$toPhoneNumber_t:=Replace string($toPhoneNumber_t;" ";"")  // no spaces

  // build URL
$requestURL_t:="https://api.twilio.com/2010-04-01/Accounts/"+$twilioAccountSID_t+"/SMS/Messages.json";

  // build headers (content won't parse correctly on the Twilio side without 
this)
APPEND TO ARRAY($headerName_at;"Content-Type")
APPEND TO ARRAY($headerValue_at;"application/x-www-form-urlencoded")

  // build content
$content_t:="From="+$fromPhoneNumber_t+"&"
$content_t:=$content_t+"To="+$toPhoneNumber_t+"&"
$content_t:=$content_t+"Body="+$message_t

  // send the message
$statusCode_l:=HTTP Request(HTTP post 
method;$requestURL_t;$content_t;$response_o;$headerName_at;$headerValue_at)

  // seup response object
Case of 
   : ($statusCode_l=201)  // successful sent SMS
      OB SET($result_o;"error";False)
      OB SET($result_o;"sid";OB Get($response_o;"sid"))
        // can't use "date_sent" because it could be queued
      OB SET($result_o;"sentDTS";Twilio_ConvertDateToDTS (OB 
Get($response_o;"date_created")))

   Else   // problem
      $errorMessage_t:="ERROR in sending SMS Message"+<>CR+<>CR+\
         "Status = "+String($statusCode_l)+<>CR+<>CR+\
         JSON Stringify($response_o;*)
      OB SET($result_o;"error";True)
      OB SET($result_o;"errorMessage";$errorMessage_t)
End case 

  // return any error message
$0:=$result_o


  // ===========================================
  // PROJECT METHOD: StripBadCharacters

  // PARAMETERS: $1 = pointer to text
  // $2 = flag to allow carriage returns (optional)

  // DESCRIPTION: Checks a text variable or alpha field or text field
  // for any characters that are "invisible" an removes them. These
  // are generally low ASCII value characters like Char(0) and Char(1).

  // This should eliminate the problem of doing a copy/paste from an
  // email or from a Word document that later causes problems when 
  // trying to do an Outlook email.

  // Line Feed and Tab characters are stripped in all cases.

  // It strips Carriage Returns unless you want to allow them, like for
  // text fields.

  // CREATED BY: Tim Nevels, Innovative Solutions ©2018
  // DATE: 2/22/18
  // LAST MODIFIED: 
  // ============================================

C_POINTER($1;$theText_p)
C_TEXT($2)
$theText_p:=$1
If (Count parameters>=2)
   $allowCR_b:=True
End if 

C_BOOLEAN($allowCR_b)
C_TEXT($theText_t;$newText_t)
C_LONGINT($i;$characterCode;$theTextLength_l)

  // copy text to local variable
$theText_t:=$theText_p->
$theTextLength_l:=Length($theText_t)

  // check each character for problems
For ($i;1;$theTextLength_l)
   $characterCode:=Character code($theText_t[[$i]])
   Case of 
      : (($characterCode=Carriage return) & $allowCR_b)  // 13 Carriage Return, 
allow
         $newText_t:=$newText_t+$theText_t[[$i]]
      : ($characterCode=DEL ASCII code)  // 127 DEL is invalid, delete
      : ($characterCode<Space)  // below 32, invalid low value
      Else   // OK value, allow
         $newText_t:=$newText_t+$theText_t[[$i]]
   End case 
End for 

  // update the field if it changed
If ($theTextLength_l#Length($newText_t))  // something was stripped
     // return the updated value
   $theText_p->:=$newText_t
End if 


  // ===========================================
  // PROJECT METHOD: Twilio_ConvertDateToDTS

  // PARAMETERS: $0 = DTS
  // $1 = Twilio Date

  // DESCRIPTION: Converts a date from Twilio to a normal DTS at GMT.
  // All Twilio are dates are in GMT, so we can ignore the "+0000" offset

  // Example: Sat, 06 Oct 2018 23:20:04 +0000

  // CREATED BY: Tim Nevels, Innovative Solutions ©2018
  // DATE: 10/12/18
  // LAST MODIFIED: 
  // ============================================

C_TEXT($0)
C_TEXT($1;$twilioDate_t)
$twilioDate_t:=$1

  // declare local variables
C_TEXT($day_t;$monthName_t;$year_t;$time_t;$month_t)

  // get date and time parts
$day_t:=Substring($twilioDate_t;6;2)
$monthName_t:=Substring($twilioDate_t;9;3)
$year_t:=Substring($twilioDate_t;13;4)
$time_t:=Substring($twilioDate_t;18;8)

  // convert month name to month number
Case of 
   : ($monthName_t="Jan")
      $month_t:="01"
   : ($monthName_t="Feb")
      $month_t:="02"
   : ($monthName_t="Mar")
      $month_t:="03"
   : ($monthName_t="Apr")
      $month_t:="04"
   : ($monthName_t="May")
      $month_t:="05"
   : ($monthName_t="Jun")
      $month_t:="06"
   : ($monthName_t="Jul")
      $month_t:="07"
   : ($monthName_t="Aug")
      $month_t:="08"
   : ($monthName_t="Sep")
      $month_t:="09"
   : ($monthName_t="Oct")
      $month_t:="10"
   : ($monthName_t="Nov")
      $month_t:="11"
   : ($monthName_t="Dec")
      $month_t:="12"
End case 

  // returne DTS
$0:=$year_t+"-"+$month_t+"-"+$day_t+"T"+$time_t+"Z"


Tim

*****************************************
Tim Nevels
Innovative Solutions
785-749-3444
[email protected]
*****************************************

**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[email protected]
**********************************************************************

Reply via email to