Make Outbound Phone Calls with Delphi

January 10, 2017
Written by
Reviewed by
Paul Kamp
Twilion
Maylon Pedroso
Contributor
Opinions expressed by Twilio contributors are their own
Kat King
Twilion

outbound-calls-delphi

In this guide, we'll show you how to use Programmable Voice to make outbound phone calls from your Delphi applications. All you'll need is a voice-capable Twilio phone number, your account credentials, and five minutes to have a boatload of fun at your keyboard.

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!

In the Twilio console, search for and purchase an available phone number capable of making outbound calls. You'll use this phone number as the "From" phone number when you initiate an outbound call.

Search for voice capable numbers

Retrieve your Twilio account credentials

First, you'll need to get your Twilio account credentials. They consist of your AccountSid and your Auth Token. They can be found on the home page of the console.

Retrieve Your Twilio Credentials

Make an outbound phone call

Now we're ready to make an outbound phone call using the provided TTwilioClient.

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.

Editor: this is a migrated tutorial. Find the original code here: https://github.com/TwilioDevEd/twilio-delphi-demo

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.

There are a few key parameters to drill into when making the outbound call. 

  • "From" - the voice-enabled Twilio phone number you added to your account earlier
  • "To" - the person you'd like to call
  • "Twiml" - Instructions in the form TwiML that explains what should happen when the other party picks up the phone
  • "Url" - Optionally, instead of passing the Twiml parameter, you can provide a Url that returns TwiML Voice instructions.

What is TwiML?

TwiML is the Twilio Markup Language, which is just to say that it's an XML document with special tags defined by Twilio to help you build your SMS and voice applications. TwiML is easier shown than explained. Here's some TwiML you might use to respond to an incoming phone call:

And here's some TwiML you might use to respond to an incoming SMS message:

Every TwiML document will have the root <Response> element and within that can contain one or more verbs. Verbs are actions you'd like Twilio to take, such as <Say> a greeting to a caller, or send an SMS <Message> in reply to an incoming message. For a full reference on everything you can do with TwiML, refer to our TwiML API Reference.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say voice="Polly.Amy">Thanks for trying our documentation. Enjoy!</Say>
  <Play>https://demo.twilio.com/docs/classic.mp3</Play>
</Response>

Of course, the TwiML you use to make the outbound call doesn't need to be a static file like in this example. Server-side code that you control can dynamically render TwiML to use for the outbound call. Check out our inbound call guide to see an example of an ASP.NET server which generates TwiML.

Happy hacking!