We help IT Professionals succeed at work.
Get Started

Need to send Text Messages from PowerBuilder using a Proxy Server

110 Views
Last Modified: 2020-03-07
I had this question after viewing Looking for a new SMS provider for my PowerBuilder application.

I have been using Twilio's SMS messaging service since I originally asked the question I'm referencing.  Now my issue is that my corporate client is requiring that I deploy my application on their internal network and servers.  While my PowerBuilder code which has worked fine while running on my own server, fails to send text messages when deployed on the corporate servers, specifically because they block access to the internet and the only solution they've suggested would require the use of their PROXY server.  I know NOTHING about proxy servers and I'm looking for some help in making the required program changes to be able to utilize a proxy server to permit access to Twilio (api.twilio.com) in order to maintain this feature in my application which has become very popular with my users.

So, besides the PowerBuilder powerscript code, I had to incorporate a third party's APIs (Chilkat Software) in order to send the SMS messages.  My existing code follows:

        loo_Glob             =       create oleobject
      li_rc                   =       loo_Glob.ConnectToNewObject( struct_1.tw_global_object )
      if li_rc < 0 then
            as_response = "Connect to New GLOB Object Error: " + loo_Rest.LastErrorText
            wf_send_warning_email( as_response )      
            destroy loo_Glob
            return false
      end if

      li_Success       =       loo_Glob.UnlockBundle( struct_1.tw_global_unlock )
      if li_Success <> 1 then
            as_response = "GLOB Unlock Bundle Error: " + loo_Rest.LastErrorText
            wf_send_warning_email( as_response )      
            destroy loo_Glob
            return false
      end if
                        
      li_Status             =       loo_Glob.UnlockStatus
      
      destroy loo_Glob
      
      //  Demonstrates how to use Basic Authentication in a REST API call for Twilio.
      //  Sends an SMS text message..
      
      //  This example requires the Chilkat API to have been previously unlocked.
      //  See Global Unlock Sample for sample code.
      
      loo_Rest             =       Create oleobject
      li_rc                   =       loo_Rest.ConnectToNewObject(struct_1.tw_rest_object)
      if li_rc < 0 then
            as_response = "Connect to New REST Object Error: " + loo_Rest.LastErrorText
            wf_send_warning_email( as_response )      
            destroy loo_Rest
            return false
      end if
      
      //  Use Basic Authentication.
      //  Your Twilio Account SID is the username.
      //  Your Twilio Auth Token is the password.
      li_Success       =       loo_Rest.SetAuthBasic( ls_LiveAcctSID,ls_LiveAuthToken)
      If gb_show_debug_msgs = TRUE then
            li_Success       =       loo_Rest.SetAuthBasic( ls_TestAcctSID,ls_TestAuthToken)
      End If
      
      //  Make the initial connection (without sending a request yet) to Twilio.

      li_BTls                               =       struct_1.tw_connect_bti
      li_Port                               =       struct_1.tw_connect_port
      li_BAutoReconnect       =       struct_1.tw_auto_reconnect
      li_Success                         =       loo_Rest.Connect( struct_1.tw_rest_url, li_Port, li_BTls, li_BAutoReconnect)
      if li_Success <> 1 then
            as_response = "Connect to Twilio FAILED... " + "~h0A~h0D"
            as_response =      as_response      +      " Twilio Connect URL: " + struct_1.tw_rest_url      +      "~h0A~h0D"
            as_response =      as_response      +      " Twilio Connect BTI: " + String(li_BTls)      +      "~h0A~h0D"
            as_response =      as_response      +      " Twilio Connect Port: " + String(li_Port)      +      "~h0A~h0D"
            as_response =      as_response      +      " Twilio AutoReconnect: " + String(li_Port)      +      "~h0A~h0D"
            as_response =      as_response      +      " Twilio Connect Success Return Code: " + String(li_BAutoReconnect)      +      "~h0A~h0D"
            as_response =      as_response      +      " Error Text: " + loo_Rest.LastErrorText
            wf_send_warning_email( as_response )      
            destroy loo_Rest
            return false
      end if
      
      //  Provide the information for the SMS text message:
      li_Success       =       loo_Rest.AddQueryParam( "To", as_PhoneNbr )
      li_Success       =       loo_Rest.AddQueryParam( "From", is_From )
      li_Success       =       loo_Rest.AddQueryParam( "Body", as_message )
      //li_Success      =      loo_Rest.AddQueryParam( "AlphaSender", "OVERTIME" )      //Not available in USA....
      
      //  Send the SMS text message.
      //  Your Twilio Account SID is part of the URI path:
      If gb_show_debug_msgs = TRUE then
            ls_ResponseJson       =       loo_Rest.FullRequestFormUrlEncoded( struct_1.tw_test_rest_cmd, struct_1.tw_test_rest_url )
      Else
            ls_ResponseJson       =       loo_Rest.FullRequestFormUrlEncoded( struct_1.tw_live_rest_cmd, struct_1.tw_live_rest_url )
      End If
      if loo_Rest.LastMethodSuccess <> 1 then
            as_response = "Send Error: " + loo_Rest.LastErrorText
            wf_send_warning_email( as_response )      
            destroy loo_Rest
            return false
      end if
      
      //  When successful, the response status code will equal 201.
      if loo_Rest.ResponseStatusCode <> 201 then
            as_response = "Response Status NOT equal to 201... " + "~h0A~h0D"
            as_response =      as_response      +      " Target Phone Number: " + as_phoneNbr      +      "~h0A~h0D"
            as_response =      as_response      +      " Department: " +      g_name      +      "~h0A~h0D"
            as_response =      as_response       +       " Employee: " + Trim(txtmsg_firstname) + " "      + Trim(txtmsg_mi) + " " + Trim(txtmsg_lastname) +      "~h0A~h0D"
            as_response =      as_response      +      " Error Text: " + loo_Rest.LastErrorText

            wf_send_warning_email( as_response )      
            destroy loo_Rest
            return false
      end if

Any "struct_1" references in the code represent values that I maintain in a SQL Server database.  All the "Chilkat-specific" calls work until the call that actually attempts to "Send the SMS text message" (reference that wording in the commented code above).

As always any help would be greatly appreciated....and anyone else who works with the Twilio service and needs to utilize a "proxy server" (whether or not you're using PowerBuilder), I suspect could provide valuable insight into my current situation.  TIA, Jim Klocksin
Comment
Watch Question
Owner, Data Architects
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE