Twilio example app

After a year of getting major sidetracked, I’m trying to get up and running on Xojo. I have a customer I built an Excel workbook for that sends out SMS texts and it works fine, although I can’t get them to stop opening read-only copies when someone has the workbook open (resulting in copy of and copy of copy of etc.)

I’ve been trying to recreate the app in Xojo, and the window and controls are working as they should. But for the actual code to send the SMS text, I’ve been unsuccessful in getting the example app to work (Examples\Communication\Internet\Web Services\Twilio\TwilioSMS). I have my own Twilio Account ID, Auth Token and From phone number, and they work in my Excel workbook. But whether I use those or grab the test credentials from Twilio (per the notes in the Xojo app), the results from Twilio are the same:

{“code”:20003,“message”:“Authentication Error - No credentials provided”,“more_info”:“https://www.twilio.com/docs/errors/20003","status”:401}

Anybody have any experience using this sample app? I know my credentials are good, since my Excel app works fine, so if the example app does work correctly under normal circumstances, then I’ll continue troubleshooting.

This sounds like previous problems I’ve seen when using the URLConnection.AuthenticationRequested event to handle credentials. (1, 2)

The solution in those cases is to set the credentials manually before sending the request:

// in SendButton.Pressed()
TwilioSocket.RequestHeader("Authorization") = "Basic " + EncodeBase64(AccountIDField.Text + ":" + AuthTokenField.Text)

Thanks, that got me further along, but it crashes on the last line:
TwilioSocket.Send(“POST”, url)

This is my first real go-around with API’s so I think I need to read up a little more on this to get familiar with it. Thanks.

Do you get a crash or an exception? Give us more information.

Sorry, I guess it was an exception. ErrorNumber 87, according to the debugger (coming mostly from VBA, I’m still getting used to the Xojo IDE). The actual Pressed event after inserting Andrew’s code is:

Sub Pressed()
Var accountID As String = AccountIDField.Text

Var params() As String
params.Add(“From=” + EncodeURLComponent(FromPhoneNumberField.Text))
params.Add(“To=” + EncodeURLComponent(ToPhoneNumberField.Text))
params.Add(“Body=” + EncodeURLComponent(SMSTextArea.Text))
Var textParams As String = String.FromArray(params, “&”)

// Assign to the Request’s Content
TwilioSocket.SetRequestContent(textParams, “application/x-www-form-urlencoded”)

// Set the URL
Var url As String = “https://api.twilio.com/2010-04-01/Accounts/” + accountID + “/Messages.json”

TwilioSocket.RequestHeader(“Authorization”) = "Basic " + EncodeBase64(AccountIDField.Text + “:” + AuthTokenField.Text)

// Send Request, results are in TwilioSocket.PageReceived event handler
TwilioSocket.Send(“POST”, url)

End Sub

When I resume the code execution, a message box displays that says "An exception of class RuntimeException was not handled. The application must shut down. Exception Error Number: 87

Hi, sorry again, I think I replied to Andrew’s message instead of yours.

The docs link to this page: Windows Sockets Error Codes (Winsock2.h) - Win32 apps | Microsoft Learn

Which says:

**WSA_INVALID_PARAMETER**
87 One or more parameters are invalid.
An application used a Windows Sockets function which directly maps to a Windows function. The Windows function is indicating a problem with one or more parameters.

I find those API calls intensely frustrating because they need to be exactly right or nothing works.

Thank you, I’ll take a look at that. My Excel application involved VBA code that I used from some sample code and it’s been working perfectly after a few tweaks. The main snippet below:

For i = 0 To RecipientListCount
    If ActiveCell.Offset(i, 1).Value <> "" Then
        Application.Wait (Now + TimeValue("0:00:01"))                                                   ' One second loop between submissions
        Recipient_Number = ActiveCell.Offset(i, 1).Value                                                ' Collect recipient number
        Set Request = CreateObject("MSXML2.ServerXMLHTTP.6.0")                                          ' Use XML HTTP
        url = "https://api.twilio.com/2010-04-01/Accounts/" & Account_SID & "/Messages.json"            ' Specify URL
        Request.Open "POST", url, False, Account_SID, Auth_Token                                        ' Open Request
        Request.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"                    ' Request Header
        postData = "From=" & Sender_Number & "&To=" & Recipient_Number & "&Body=" & BodyText            ' Concatenate PostData
        Request.send postData                                                                           ' Send the PostData
        Status_Response = "Status Code: " & Request.Status & " | Status Text: " & Request.responseText  ' Get response status code & text
        ActiveCell.Offset(i, 3).Value = Format(Now, "mm/dd/yyyy HH:mm:ss") & "| " & Status_Response     ' Return status to worksheet
    End If
Next i

I’m sure the syntax is a bit different in Xojo versus the Excel reference, so I just need to play around with it a little more to get it working. I get the feeling it’s almost there. Thanks.