From time to time, people ask me if it’s possible to send text messages from their apps. As is the case with most things, the answer is “Yes, if you know how” 🙂 One of the easiest ways to send text messages is to use a web service called Twilio.
Twilio has many types of services related to telephones, including making and receiving phone calls and text messages. Twilio is not a free service but you can get a free developer account with a credit to test it out.
A standard text message is sent using either SMS or MMS. To send someone a text you simply need their mobile phone number. Remember, some mobile carriers charge to send and receive text messages so keep this in mind when planning to text via your app. Create a free developer account at Twilio.com. I’ll wait for you to come back…
Twilio has a lot of documentation on how to use their various services. To send an SMS message, you want to take a look at their SMS REST API which is here: https://www.twilio.com/docs/api/rest/sending-messages
On this page you will see some sample code on the right. Click the drop-down to switch to the sample code for CURL, which is what I’ll use to translate to Xojo code.
Essentially, I’m using a Xojo.Net.HTTPSocket to send call the Twilio web service to send the SMS message.
To start, I created a new Desktop project. On Window1, I added two Constants for the Account SID and Auth Token:
- kAccountSID As Text = “<YourAccountSID>”
- kAuthToken As Text = “<YourAuthToken>”
(Grab those values from your Twilio dashboard.)
I then dragged a “Generic Object” onto the Window layout and changed these properties:
- Name: MySocket
- Super: Xojo.Net.HTTPSocket
To test sending a message, I dragged a button to the Window. The CURL sample code translated to Xojo code looks like this, which I just put on the Button’s Action event handler:
Const kFromPhone = "<TwilioPhone#>" // Use your Twilio-assigned phone number here Const kToPhone = "<ToPhone#>" // A phone number to send the test message to Dim params() As Text params.Append("From=" + EncodeURLComponent(kFromPhone).ToText) params.Append("To=" + EncodeURLComponent(kToPhone).ToText) params.Append("Body=" + EncodeURLComponent("This Is the ship that made the Kessel Run In fourteen parsecs?").ToText) params.Append("MediaUrl=https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg") Dim textParams As Text = Text.Join(params, "&") // Convert Text to MemoryBlock Dim data As Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(textParams) // Assign to the Request's Content MySocket.SetRequestContent(data, "application/x-www-form-urlencoded") // Set the URL Dim url As Text = "https://api.twilio.com/2010-04-01/Accounts/" + kAccountSID + "/Messages.json" // Send Request, results (including any errors) are in MySocket.PageReceived event handler MySocket.Send("POST", url)
Lastly, you need to provide the authorization. Add the AuthenticationRequired event to MySocket with this code:
Name = kAccountSID Password = kAuthToken Return True
After you’ve filled in the AccountSID, AuthToken and phone numbers you can run the project. This is the text I received:
A more robust version of this is included with your Xojo examples: Examples/Communication/Internet/TwilioSMS
Have fun sending texts using Xojo and Twilio!