Using TelAPI to create applications that interact with the telephone network is simple. Dead simple. Despite its simplicity, TelAPI is a powerful tool with a range of practical uses for startups. The best way for us to prove this, is by giving you sample code and example apps to backup our claim and get you on your way to setting up your first TelAPI application.
We've tried to make this post as simple as possible, by doing a few examples just in XML, but we've also taken the time to create other examples in PHP, Python, and Ruby. (Note: The PHP, Python, and Ruby examples utilize our TelAPI helper libraries for that language)
When you sign up for TelAPI, you'll also receive $25 in free credit to test these uses — enough to get you 2500 SMS, 2500 voice minutes, or a US phone number for 25 months!
1. A Business Phone Number That Forwards To Your Cell Phone
Entrepreneurs and small business owners want their customers and clients to have access to them at all times, but they don't always want to give out or publicize their direct personal phone numbers. To solve this problem, TelAPI phone numbers can be used to receive calls and forward those calls to another number — anywhere in the world. The best part is, this can be done very easily, with just a few lines of XML.
It gets even easier though! If you use TelAPI's InboundXML Editor, we'll host the XML code for you, so you don't even need a web server or your own hosting to get this up and running.
<!-- Ensure the "Voice Request URL" for your TelAPI number points to wherever this XML document is hosted -->
<!-- An InboundXML document is made up of various XML elements nested in the response element -->
<Response>
<!-- The <Dial> element starts an outgoing call. Replace this number with the number of the phone you would like to receive the call -->
<Dial>15555555555</Dial>
</Response>
2. A Simple Conference Room For Conference Calls
There are countless cases where businesses may need to quickly setup conference calls for impromptu meetings. This is typically costly and/or burdensome, and in many cases inconveniences employees. No one actually likes conferencing services that give out a dial-in number plus another 10 digit room number to dial. It's almost impossible to use a conference room setup like this if the dial-in number is in an email on your smartphone and you don't have a pen handy! With TelAPI, businesses can easily instantiate one, consistent conference call number to share with participants.
<Response>
<Dial>
<Conference startConferenceOnEnter="true" maxParticipants="10">
Conference Call Example Room
</Conference>
</Dial>
</Response>
3. Record Incoming Calls And Have Them Transcribed To Text
Recording incoming calls and having those conversations transcribed to easily readable and searchable text is extremely valuable. Many businesses and individuals are doing this these days, as a means to capture important information and reference it at a later date. Accomplishing this with TelAPI can be done very easily in just 2 steps. The first step simply involves crafting an inbound XML document that receives the incoming phone call on a TelAPI number and records the conversation. The second step is receiving the resulting transcription in a callback script.
To show you an example of this, we've broken the code down into the two necessary files. You'll notice that part 2, which is the callback script, simply receives the transcription and echos it. However, you could easily expand upon this example. For instance, the transcription could be emailed to participants on the call, stored in a searchable database or even shaved via SMS. The possibilites are endless.
<?php
// Save the following file as start_recording.php
require_once('library/TelApi/InboundXML.php');
$inbound_xml = new TelApi_InboundXML();
// Use 'Say' element to acknowledge that the call is being recorded
$inbound_xml->say('The following call is being recorded.');
// Use 'Record' element to record the call
$inbound_xml->record(array(
'action' => 'http://yourdomain.com/record-my-call-action-example',
'method' => 'POST',
'playBeep' => 'true',
'timeout' => '30',
'transcribe' => 'true',
// URL where the transcription text is sent for further processing
'transcribeCallback' => 'http://yourdomain.com/transcribe_call.php'
));
echo $inbound_xml;
# Save the following as start_recording.py
from flask import Flask
from telapi import inboundxml
app = Flask(__name__)
@app.route("/start_recording")
def start_recording():
print "RECORDING STARTED"
return "%s" % inboundxml.Response(
inboundxml.Say(
# Let user know that the following call is being recorded
'The following call is being recroded' % (caller_name)
),
inboundxml.Record(
action = 'http://yourdomain.com/record-my-call-action-example',
method = 'POST',
finishOnKey = '#',
transribe = 'true',
transcribeCallback = 'http://yourdomain.com/records/transcribe_call'
)
)
if __name__ == "__main__":
app.run()
class RecordsController < ApplicationController def record_incoming_calls xml = Telapi::InboundXml.new do # Let user know that the following call is being recorded Say('The following call is being recorded.', :voice => 'man')
Record(
# In this example, this channel is being used for debugging HTTP requests
:action => 'http://yourdomain.com/record-my-call-action-example',
:method => 'POST',
:playBeep => 'true',
:timeout => '30',
:transcribe => 'true',
# This is the URL where the transcription is sent for further processing
:transcribeCallback => 'http://blog-post.herokuapp.com/records/transcribe_call',
:finishOnKey => '#')
end
respond_to do |format|
format.xml { render :xml => xml.response }
end
end
<?php
// Save the following file as transcribe_call.php
// Assign the transcription text to a variable
$transcription = $_GET['TranscriptionText'];
// Output the the transcription text
echo "This is the transcription text: {$transcription}";
# Save the following file as transcribe_call.py
from flask import Flask, flask
from telapi import inboundxml
app = Flask(__name__)
@app.route("/transcribe_call", methods=['GET', 'POST'])
def transcribe_call():
# Assign the transcription text to a variable
if request.method == 'POST':
transcription = request.form.get("TranscriptionText")
else:
transcription = request.args.get("TranscriptionText")
return 'This is the transcription text: %s' % (transcription)
if __name__ == "__main__":
app.run()
# Action URL where the transcription is sent
def transcribe_call
render :nothing => true
# Assigning the transcription text to a parameter
transcription = params["TranscriptionText"]
# Outputting the transcription text
puts = "The following is the text from the transcription."
puts transcription
end
end
4. A Professional Sounding IVR Menu With Departments
IVRs are ubiquitous in today's enterprise business environment. Many small and medium sized businesses would undoubtedly find value in having such a system in place, but they may lack the resources or funds necessary to build one. TelAPI changes that. Now any developer can buy a toll free number and build a high quality IVR in minutes.
Below is a sample IVR that offers your callers the option to be transfered to three different departments. From here, you could setup an "ivr-user-response" script on your server to do whatever you'd like for each extension, such as routing the calls to a voicemail box or to another individual's phone.
require_once('library/TelApi/InboundXML.php');
// TelAPI inbound XML document
$inbound_xml = new TelApi_InboundXML();
$gather = $inbound_xml->gather(array(
// URL where the gathered digits will be sent
'action' => 'http://yourdomain.com/ivr-user-response.php',
// HTTP method used to request the action URL
'method' => 'GET',
// The maximum number of digits that will be gathered
'numDigits' => 1,
// The key a caller can press to finish the gather
'finishOnKey' => '#',
// The number of seconds Gather will wait for user input
'timeout' => '10'
));
// Gather is used to obtain the digits callers submit
$gather -> say('If you would like to reach sales, press 1')
say('If you would like to reach accounting, press 2.')
say('If you would like to reach legal, press 3.');
echo $inbound_xml;
from flask import Flask
from somewhere import ivr
app = Flask(__name__)
@app.route('/ivr/intro')
def ivr_intro():
response = ivr.Gather(
Say('If you would like to reach sales, press 1.'),
Say('If you would like to reach accounting, press 2.'),
Say('If you would like to reach legal, press 3.'),
action = 'http://yourdomain.com/ivr-user-response.py',
method = 'GET',
numDigits = 1,
finishOnKey = '#',
timeout = '10'
)
return "%s" % response
if __name__ == '__main__':
app.run(debug=True)
def ivr
xml = Telapi::InboundXml.new do
# Gather is used to obtain the digits callers submit
Gather(
# URL where the gathered digits will be sent
:action => "http://yourdomain.com/ivr-user-response.rb",
# HTTP method used to request the action URL
:method => "GET",
# The maximum number of digits that will be gathered
:numDigits => "1",
# The key a caller can press to finish the gather
:finishOnKey => "#",
# The number of seconds Gather should wait for the user input
:timeout => "10")
{
Say("If you would like to reach sales, press 1.")
Say("If you would like to reach accounting, press 2.")
Say("If you would like to reach legal, press 3.")
}
end
# Ruby code used to generate an XML response
respond_to do |format|
format.xml { render :xml => xml.response }
end
end
5. SMS Reminders
There are many instances in which businesses could benefit from providing an SMS reminder service to their customers or clients. For instance, if a doctor has a client with an appointment scheduled for tomorrow afternoon, his office could send that client an SMS reminder so they won't forget about their appointment. Practical and useful!
require_once 'library/TelApi.php';
// Always use singleton design pattern
$telapi = TelApi::getInstance();
// Set TelAPI credentials
$telapi -> setOptions(array(
'account_sid' => 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'auth_token' => 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
));
$dateToRemind = 'DESIRED DATE HERE';
if ($dateToRemind == date('Y-m-d'))
{
$sms_message = $telapi->create('sms_messages', array(
// Make sure the 'From" number is one of your TelAPI DIDs
'From' => '5555555555',
'To' => '5555555555',
'Body' => 'This is a reminder of your upcoming appointment.'
));
}
else
{
echo "It's not time to remind them yet.";
}
from telapi import rest
from datetime import datetime
account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
client = rest.Client(account_sid, auth_token)
account = client.accounts[client.account_sid]
if dateToRemind == datetime.now():
sms_message = account.sms_messages.create(
# Make sure the 'From" number is one of your TelAPI DIDs
from_number='5555555555'
to_number='5555555555'
body='This is a reminder of your upcoming appointment.'
)
else:
print "It's not time to remind them yet."
def reminder
require 'telapi'
Telapi.config do |config|
config.account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
config.auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
end
if dateToRemind == Time.now
# Send SMS via TelAPI's REST API
# Make sure the 'From" number is one of your TelAPI DIDs
Telapi::Message.create(number_to_send_to,
number_to_send_from,
'This is a reminder of your upcoming appointment.' )
else
puts "It's not time to remind them yet."
end
end
6. Verifying A Number Is A Mobile Number
Many companies these days prefer using SMS over email to send out personalized offers or information to their customers. But are you sure the numbers in your list are valid mobile numbers? Have you thought about checking the numbers before they get added to your lists? For these types of campaigns, it's important for a company to verify their lists only contain mobile numbers capable of receiving SMS. TelAPI's Carrier Lookup endpoint and its 'IsMobile' property can do just that.
require_once 'library/TelApi.php';
// Always use singleton design pattern
$telapi = TelApi::getInstance();
// Set TelAPI credentials
$telapi -> setOptions(array(
'account_sid' => 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'auth_token' => 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
));
// Make a request to TelAPI's Carrier Lookup
$carrier_resource = $telapi->get('carrier', array(
'PhoneNumber' => '15555555555'
));
// Assign the IsMobile boolean value to the is_mobile variable
$is_mobile = $_GET['IsMobile'];
if($is_mobile == "True")
{
echo "This number is a mobile number. SMS enabled.";
}
else
{
echo "This number is not a mobile number. Not SMS enabled.";
}
from flask import Flask, request
from telapi import rest
account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
client = rest.Client(account_sid, auth_token)
account = client.accounts[client.account_sid]
@app.route('/call/check-mobile')
def check_mobile():
# Make a call to TelAPI's REST endpoint for Carrier Lookups
carrier_details = account.carrier.create(
'phone_number' = '5555555555'
)
# Assign the IsMobile request param to the is_mobile variable
is_mobile = request.args.get("IsMobile")
if is_mobile == "True":
print "This number is a mobile number. SMS enabled."
else:
print "This number is not a mobile number. Not SMS enabled."
require 'telapi'
Telapi.config do |config|
config.account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
config.auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
end
# Make a request to TelAPI's Carrier Lookup
Telapi::Carrier.lookup(phone_number)
# Assign the IsMobile boolean value to the is_mobile variable
is_mobile = params["IsMobile"]
if is_mobile == "True"
puts "This number is a mobile number. SMS enabled."
else
puts "This number is not a mobile number. Not SMS enabled."
end
end
7. Customized Caller ID Greetings To Callers
To offer the best service possible, many businesses find it advantageous to offer personal greetings to incoming callers. This can easily be implemented by obtaining the 'CallerName' attribute in the request parameters.
require_once('library/TelApi/InboundXML.php');
// TelAPI inbound XML document
$inbound_xml = new TelApi_InboundXML();
// Assign the CallerName request param to the caller_name variable
$caller_name = $_GET['CallerName'];
$inbound_xml->say(
// Greet the incoming caller with their name
"Hello {$caller_name}! Thanks for calling in!", array('voice' => 'man')
);
echo $inbound_xml;
from flask import Flask
from telapi import inboundxml
app = Flask(__name__)
@app.route('/caller/intro')
def ivr_intro():
# Assign the CallerName request param to the caller_name variable
caller_name = request.args.get("CallerName")
return '%s' % inboundxml.Response(
inboundxml.Say(
# Greet the incoming caller with their name
'Hello %s! Thanks for calling in!' % (caller_name)
))
if __name__ == '__main__':
app.run(debug=True)
def greeting
# Assign the CallerName request param to the caller_name
caller_name = params["CallerName"]
# Generate XML response
xml = Telapi::InboundXml.new do
# Greet the incoming caller with their name
Say("Hello #{caller_name}!", :voice => "man")
Say("Thanks for calling in!", :voice => "man")
end
respond_to do |format|
format.xml { render :xml => xml.response }
end
end
8. Screening Incoming Calls
Businesses can find added value in using call screening software to determine who incoming calls are coming from. For instance, if an important client is trying to get through, they likely don't want to miss that call. However, if it is a telemarketer or something else that is inconsequential, a business's employees would likely prefer to ignore those callers. With TelAPI, building call screening software can be done very easily. This, of course, is just one simple example, but could be expanded upon to create a more comprehensive system.
require_once('library/TelApi/InboundXML.php');
$inbound_xml = new TelApi_InboundXML();
// A banned number assigned to a variable
$banned_number = "+18005551234";
// Assign the CallerName request param to the caller_name
$call_from = $_GET['From'];
//Assign the CallerName request variable to caller_name
$caller_name = $_GET['CallerName'];
// If the number comes from a banned number, then reject it
if($call_from == $banned_number)
{
$inbound_xml->reject(array( 'reason' => 'rejected' ));
}
// If number is not banned, answer call and greet caller
else
{
$inbound_xml->say(
'Hello $caller_name!
Thanks for calling in!', array('voice' => 'man'));
}
echo $inbound_xml;
from flask import Flask, request
from telapi import inboundxml
@app.route('/call/intro')
def call_intro():
# A banned number assigned to a variable
banned_number = "+18005551234"
# Assign the number of the incoming call to a variable
call_from = request.args.get("From")
# Assign the name registered to the incoming caller to a variable
caller_name = request.args.get("CallerName")
# If the number comes from a banned number, then reject it
if call_from = banned_number:
return '%s' % inboundxml.Response(
inboundxml.Reject(reason = 'rejected')
)
else:
# If the number is not banned, answer the call and greet the caller
return '%s' % inboundxml.Response(
inboundxml.Say(
# Greet the incoming caller with their name
'Hello %s, Thanks for calling in!' % (caller_name)
))
def screening
# A banned number assigned to a variable
banned_number = "+18005551234"
# Assign the number of the incoming call to a variable
call_from = params["From"]
# Assign the name registered to the incoming caller to a variable
caller_name = params["CallerName"]
# If the number comes from a banned number, then reject it
if call_from == banned_number
xml = Telapi::InboundXml.new do
Reject(:reason => "rejected")
end
else
# If the number is not banned, answer and greet the caller
xml = Telapi::InboundXml.new do
Say("Hello #{caller_name}. Thanks for calling in.")
end
end
# Ruby used to generate an XML response
respond_to do |format|
format.xml { render :xml => xml.response }
end
end
Take away: These are just a few of many potential business uses that can be implemented with TelAPI using very little code. Ultimately, TelAPI enables developers of all skill levels to seamlessly build a wide-range of powerful telephony applications.
If you have any follow-up questions concerning the examples above, please reach out to Doug Crescenzi, TelAPI's developer evangelist.