Send SMS Messages in Delphi

July 29, 2016
Written by

delphi-sms

In this guide, we'll show you how to use Programmable SMS to send SMS messages in your Delphi application. The code snippets in this guide were written and tested using Delphi 10.2.3. You can view the complete sample project on GitHub.

Let's get started!

Outgoing SMS Diagram

Sign up for a Twilio Account

To use the Twilio REST API, you need an account. Signing up for a free Twilio account is easy. Once you've signed up, head over to your Console and grab your Account SID and your Auth Token. You will need those for the code samples below.

Purchase an SMS Capable Phone Number

Sending SMS messages requires an SMS capable phone number. You can browse the available phone numbers in the Console. Be sure that the phone number you buy is SMS capable. When you search, you can check the box to filter available numbers to those that are SMS capable:

Buy an SMS-capable Twilio Number

 

When viewing the search results, you can see the capability icons in the list of available numbers:

List of Twilio Number Capabilities

 

Armed with a Twilio phone number, you can now start sending messages to mobile devices.

Send an SMS Message via the REST API

Sending an outgoing SMS message requires sending an HTTP POST to the Messages resource URI. Using the provided TTwilioClient, you can post to the Message resource and specify the To, From, and Body parameters for your message.

This is a migrated tutorial. Clone the original code from https://github.com/TwilioDevEd/twilio-delphi-demo/

TTwilioClient is a sample class that encapsulates the HTTP calls that need to be made to the Twilio REST API. Feel free to extend it or rework it for your needs.

program TwilioDemo;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.Classes,
  System.SysUtils,
  System.JSON,
  System.Net.HttpClient,
  TwilioClient in 'TwilioClient.pas';

var
  client: TTwilioClient;
  allParams: TStringList;
  response: TTwilioClientResponse;
  json: TJSONValue;
  fromPhoneNumber: string;
  toPhoneNumber: string;

begin
  try
    // Create environment variables (named below) with your Twilio credentials
    client := TTwilioClient.Create(
      GetEnvironmentVariable('TWILIO_ACCOUNT_SID'),
      GetEnvironmentVariable('TWILIO_AUTH_TOKEN'));

    // Your Twilio phone number
    fromPhoneNumber := '+15017122661';

    // Your destination number (for trials, this needs to be your mobile #)
    toPhoneNumber := '+15558675310';

    // Make a phone call
    Writeln('----- Phone Call -----');
    
    allParams := TStringList.Create;
    allParams.Add('From=' + fromPhoneNumber);
    allParams.Add('To=' + toPhoneNumber);
    allParams.Add('Url=http://demo.twilio.com/docs/voice.xml');

    // POST to the Calls resource
    response := client.Post('Calls', allParams);

    if response.Success then
      Writeln('Call SID: ' + response.ResponseData.GetValue<string>('sid'))
    else if response.ResponseData <> nil then
      Writeln(response.ResponseData.ToString)
    else
      Writeln('HTTP status: ' + response.HTTPResponse.StatusCode.ToString);

    // Send a text message
    Writeln('----- SMS -----');

    allParams := TStringList.Create;
    allParams.Add('From=' + fromPhoneNumber);
    allParams.Add('To=' + toPhoneNumber);
    allParams.Add('Body=Never gonna give you up, Delphi');

    // POST to the Messages resource
    response := client.Post('Messages', allParams);
    if response.Success then
      Writeln('Message SID: ' + response.ResponseData.GetValue<string>('sid'))
    else if response.ResponseData <> nil then
      Writeln(response.ResponseData.ToString)
    else
      Writeln('HTTP status: ' + response.HTTPResponse.StatusCode.ToString);
  finally
    client.Free;
  end;

  Writeln('Press ENTER to exit.');
  Readln;
end.

If you want to send a message to several recipients, you could simply create an array of recipients and iterate through each phone number in that array. You can send as many messages as you like, as fast as you like and Twilio will queue them up for delivery at your prescribed rate limit. See our guide on how to Send Bulk SMS Messages for more tips.

For more information about sending SMS and MMS messages, see the REST API Reference.