KEMBAR78
Twilio Lecture Notes | PDF | Interactive Voice Response | Computing
0% found this document useful (0 votes)
49 views10 pages

Twilio Lecture Notes

Uploaded by

etorikevin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views10 pages

Twilio Lecture Notes

Uploaded by

etorikevin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Twilio Lecture Notes

Twilio is a cloud communications platform that provides APIs (Application


Programming Interfaces) to developers for building a wide variety of
communication applications. With Twilio, developers can easily add messaging,
voice, video, and email capabilities to their web and mobile applications without
the need to manage complex infrastructure. Twilio abstracts away much of the
complexity of telecom networks, making it simpler for businesses to integrate
communication features like SMS, phone calls, and chat into their apps.

Key Features of Twilio:


1. SMS & MMS:

Send and receive text messages (SMS): Twilio allows you to send and
receive SMS messages globally, enabling businesses to communicate
with customers via mobile messaging.

MMS support: You can also send multimedia messages (MMS) that
include images, videos, and audio.

2. Voice:

Voice calling: Twilio allows you to make and receive phone calls over
the internet (Voice over IP or VoIP) using the Twilio API.

Programmable IVR: You can set up interactive voice response (IVR)


systems that interact with callers through automated menus.

Call recording: Twilio offers call recording and transcription features,


which can be used for customer support, sales, or compliance.

3. Video:

Video calls: Twilio enables real-time video calling capabilities, useful


for virtual meetings, telehealth applications, or online support systems.

Video chat rooms: You can create multi-party video chat rooms for
group calls, meetings, webinars, and more.

4. Email:

Twilio SendGrid: A cloud-based email API service for sending


transactional emails, such as account confirmations, marketing emails,

Twilio Lecture Notes 1


and notifications. SendGrid provides email delivery, analytics, and
tracking.

5. Authentication:

Two-Factor Authentication (2FA): Twilio offers tools for implementing


multi-factor authentication in your apps. This is crucial for securing user
accounts and adding an extra layer of security via SMS or voice calls.

6. Chat & Messaging Services:

Programmable chat: You can build in-app messaging features,


customer service chatbots, and live chat experiences.

WhatsApp messaging: Twilio also supports messaging on platforms


like WhatsApp, allowing businesses to engage with users via popular
messaging apps.

7. Phone Numbers:

Twilio provides virtual phone numbers in multiple countries and allows


you to rent these numbers for various use cases, such as sending SMS,
making calls, or receiving messages.

You can choose numbers with specific area codes or toll-free numbers.

8. Security Features:

Number masking: For privacy in transactions, Twilio can mask phone


numbers, such as in ride-sharing or e-commerce platforms, to keep
users' numbers private.

Fraud prevention: Twilio also provides tools to detect and prevent


fraudulent activity on voice and messaging services.

9. Global Reach:

Twilio supports phone numbers in over 100 countries, and it can send
messages globally, so it's ideal for companies with a worldwide
audience.

10. Programmable APIs:

Twilio provides several APIs that allow developers to programmatically


interact with voice, messaging, video, and more. This flexibility is what
makes Twilio appealing to developers building custom communication
workflows.

Twilio Lecture Notes 2


In this tutorial we are going to focus on one service provided by Twilio its SMS
feature. In order to send SMS with Twilio kindly follow these steps:

Step 1: Create a Twilio Account


1. Sign up for Twilio:

Visit the Twilio website: https://www.twilio.com.

Click on Sign Up in the upper-right corner and fill in the necessary


information (email, password, etc.).

Confirm your email address by clicking on the link sent to your inbox.

2. Verify your phone number:

After signing up, you'll be asked to verify your phone number. This is
necessary for sending messages through Twilio.

Twilio will send you a code to enter on the website for verification.

3. Access your Twilio Console:

Once logged in, you’ll land on your Twilio Console. This is your control
panel where you’ll manage your Twilio account, including your phone
numbers, API credentials, logs, and more.

Step 2: Get a Twilio Phone Number


1. Navigate to the Phone Numbers section:

In your Twilio Console, click on "Phone Numbers" in the left-hand


sidebar.

Then click on "Buy a Number" or "Get a Number".

2. Choose a number:

You can search for a phone number by country, capabilities (SMS,


Voice, etc.), and type (mobile, toll-free, etc.).

Select the phone number you want to use for sending SMS messages.
You’ll be able to configure this number in your API later.

Step 3: Get Your Twilio API Credentials


1. Navigate to the Console Dashboard:

Twilio Lecture Notes 3


On your Twilio Console dashboard, look for your Account SID and Auth
Token.

These are your API credentials that allow you to authenticate and
interact with Twilio’s services.

2. Store the credentials securely:

Make sure to save your Account SID and Auth Token securely, as you’ll
use them in your code to authenticate API requests.

Step 4: Install the Twilio SDK (for Code Integration)


Twilio provides official SDKs for multiple programming languages. You can
choose one depending on the language you're using (Python, Node.js, Java,
Ruby, PHP, etc.).

Here’s how to set up the SDK for Python and Node.js:

For Python:
1. Install the Twilio Python library:

pip install twilio

2. Set up a simple script to send an SMS.

For Node.js:
1. Install the Twilio Node.js SDK:

npm install twilio

2. Set up a simple script to send an SMS.

Step 5: Write Code to Send SMS

Python Example:

Twilio Lecture Notes 4


python
Copy code
from twilio.rest import Client

# Your Twilio Account SID and Auth Token from the Twilio Co
nsole
account_sid = 'your_account_sid'
auth_token = 'your_auth_token'

client = Client(account_sid, auth_token)

message = client.messages.create(
body="Hello, this is a test message from Twilio!", # Y
our message
from_='+12345678901', # Your Twilio phone number
to='+09876543210' # Recipient's phone number
)

print(f"Message SID: {message.sid}")

Node.js Example:

javascript
Copy code
const twilio = require('twilio');

// Your Twilio Account SID and Auth Token from the Twilio C
onsole
const accountSid = 'your_account_sid';
const authToken = 'your_auth_token';

const client = new twilio(accountSid, authToken);

client.messages.create({
body: 'Hello, this is a test message from Twilio!', //
Your message

Twilio Lecture Notes 5


from: '+12345678901', // Your Twilio phone number
to: '+09876543210' // Recipient's phone number
})
.then((message) => console.log("Message SID: " + message.si
d))
.catch((error) => console.log(error));

Step 6: Test Your SMS


1. Run your script:

Execute your Python script ( python send_sms.py ) or Node.js script ( node


send_sms.js ) from the terminal.

2. Check the recipient's phone:

If everything is set up correctly, you should receive the SMS on the


recipient's phone.

3. Check the Twilio Console:

In your Twilio Console, you can also check the Message Logs to see
the status of the sent message, any errors that might have occurred,
and other related data.

Step 7: (Optional) Handle Incoming SMS (If Needed)


If you want to handle incoming messages (e.g., responding to SMS), you will
need to set up a Webhook.

1. Set up a webhook URL:

In the Twilio Console, go to the "Phone Numbers" section and choose


the number you purchased.

Under the Messaging section, set up the Webhook URL for incoming
messages. This is the URL where Twilio will send HTTP requests when
an SMS is received.

2. Create a Webhook endpoint:

In your web server (e.g., Flask for Python, Express for Node.js), create
an endpoint to process incoming messages.

For example, using Flask in Python:

Twilio Lecture Notes 6


python
Copy code
from flask import Flask, request
from twilio.twiml.messaging_response import Messaging
Response

app = Flask(__name__)

@app.route("/sms", methods=['GET', 'POST'])


def sms_reply():
"""Respond to incoming messages with a simple tex
t message."""
resp = MessagingResponse()
resp.message("Thanks for your message!")
return str(resp)

if __name__ == "__main__":
app.run(debug=True)

Step 8: Monitor Your Usage & Billing


Twilio offers a free trial balance when you first sign up, but be mindful of
your usage to avoid unexpected charges.

You can view usage and billing details in the "Usage" section of your Twilio
Console.

If you run out of trial credit, you will need to upgrade your account by
adding a payment method.

When you sign up for a free Twilio account (also known as the Twilio trial
account), Twilio provides you with an initial balance of $15.00 USD in credits.
This is to allow you to test out the platform and explore its features before
committing to a paid plan.

Key Points About the Free Twilio Account:


1. $15.00 Trial Credit:

Twilio Lecture Notes 7


The free $15.00 credit can be used to send SMS messages, make voice
calls, purchase phone numbers, and use other Twilio services.

The trial credit is only available to new users when you sign up for a
Twilio account. Once it's used up, you'll need to add a payment method
to your account to continue using Twilio's services.

2. Limitations of the Free Account:

Trial number: With the free account, Twilio gives you a trial phone
number that you can use to send SMS and make calls. This number will
be a random number selected by Twilio.

Sender ID: When sending messages, the recipient will see that the
message is coming from your trial number, and you cannot send
messages to numbers that are not verified on your account (unless the
numbers are verified as part of the trial process).

Phone number verification: You are required to verify your personal


phone number in the Twilio console before you can send SMS to that
number. This step helps to ensure you're testing with your own number.

3. Message Costs:

Sending SMS messages and making calls through your free Twilio
account will be subject to the same pricing as a standard paid Twilio
account. However, the $15 trial credit will be deducted based on the
cost of these activities.

The cost per SMS message and call rates vary depending on the
destination (country, number type, etc.). For example, sending an SMS
within the U.S. typically costs around $0.0075 per message, but
international rates will be higher.

4. Expiration:

The $15 trial balance does not expire immediately after you sign up,
but if your account is inactive for a long period (typically 12 months),
Twilio may disable it. To keep your account active, you may need to
make a small payment or continue using the account.

If your trial balance is used up, you'll need to upgrade your account by
adding a credit card and switching to a paid plan to continue using
Twilio.

Twilio Lecture Notes 8


Summary of Steps:
1. Sign up for a Twilio account.

2. Buy a phone number via the Twilio Console.

3. Get your API credentials (Account SID and Auth Token).

4. Install the Twilio SDK for your preferred language (Python, Node.js, etc.).

5. Write the code to send an SMS using the Twilio API.

6. Test your message to ensure everything is working correctly.

7. Handle incoming SMS (optional, for two-way communication).

8. Monitor your account usage and billing in the Twilio Console.

Twilio makes it relatively easy to integrate messaging functionality into your


applications, and the steps above will guide you through the basics of sending
and receiving SMS messages using their platform.

Practical Exercise

Project: Appointment Reminder system


(50Mks)
Phase 1: Two-Factor Authentication (2FA) System (25Mks)
Objective: Implement a simple 2FA system using Twilio for the login of an
Appointment reminder application.

Features:

Users log in with their username and password.

After successful login, an SMS with a One-Time Password (OTP) is


sent to the user’s phone.

The user must input the OTP to complete the authentication process.

Skills: Twilio Messaging API, OTP generation, integrating SMS into a web
application, security best practices.

Twilio Lecture Notes 9


Extension Ideas: Allow users to choose between receiving an OTP via SMS
or via an authentication app (like Google Authenticator).

Phase 2 : Sending Reminders (25Mks)


Objective: Build an appointment reminder system where users can set
appointments, and the system will remind them via SMS.

Features:

Users can send an SMS to set an appointment (e.g., "Set appointment


with Dr. Ngong Felix at 3 PM tomorrow").

The system sends a reminder SMS 30minutes before the scheduled


time.

The system allows users to cancel or reschedule appointments via


SMS.

Skills: Twilio Messaging API, time and date manipulation, scheduling tasks,
backend management.

Extension Ideas: Integrate with a calendar API (Google Calendar, etc.) to


sync appointments and automatically send reminders.

The design and User experience of the application is left to the personal
creativity of the student but the key functionalities must be realized.

Feel free to add other Extension Ideas, Bonus points will be given for this

Twilio Lecture Notes 10

You might also like