This workshop has been retired and is no longer maintained or recommended.
In this workshop, you will learn how to use a modified version of the Twilio API to make phone calls and send text messages that allow you to make things like group texting apps.
Note: An API is an agreed upon way for one program to interact with another. APIs are cool because they let even our very simple programs to interact with very powerful programs with minimal code.
In this workshop, you probably won’t understand how it all works and that’s totally cool. Your objective is to learn how to make things happen.
Please use Google Chrome for this tutorial
twilio
in the projects
folderWhen you’re done with it, your folder structure should look like this:
Create an index.html
file inside of the twilio
folder:
Right click the twilio
folder you just created
Click “New File”
Then name the file index.html
Open the newly created index.html
and type the below HTML template into it:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
To add the capability to send text messages and make phone calls, we need to add the Twilio Basic JavaScript library.
script
tag inside the body
tag.<script src="//bit.ly/twilio-basic-v7"
sid="YOUR_TWILIO_ACCOUNT_SID_HERE"
token="YOUR_TWILIO_AUTH_TOKEN_HERE">
</script>
sid
and token
Change "YOUR_TWILIO_ACCOUNT_SID_HERE"
and "YOUR_TWILIO_AUTH_TOKEN_HERE"
to your actual sid
and token
. The facilitator should have given you an SID and a token. If not, you should ask them for one.
Note: If you are the facilitator (or if you’re just doing this workshop by yourself), follow the directions here to get your own Twilio SID and token.
If HTML is for the content, and CSS is for the look and feel of the website, JavaScript is used for interactivity, such as sending and receiving phone calls and text messages.
Just like we wrote HTML in .html
files and CSS in .css
files, we write JavaScript in .js
files.
Create an main.js
file inside of the twilio
folder:
twilio
folder you just createdmain.js
Although we’ve created our JavaScript file, the HTML will not run it until you explicitly include it with a script
tag, like so:
<script src="main.js"></script>
Add the above script tag underneath the current script
tag
Type the following into your main.js
file (don’t forget the parentheses commas, and quotation marks.):
Twilio.sendMessage("555-555-5555", "You just subscribed to Gossip Girl");
555-555-5555
and says the message You just subscribed to Gossip Girl.
We don’t want that.Change 555-555-5555
to your cell phone number
The code in our main.js
file will now run every time the index.html
page is open:
Open index.html
Click “Preview” → “Live Preview File”
You should now receive a text message!
Just to add some clarity to our currently blank webpage:
body
of the HTML:<p>Refresh the page to run the code again</p>
Let’s say we want to change the code.
index.html
openmain.js
and change Twilio.sendMessage
to Twilio.callAndSay
Save your main.js
file by clicking “File” → “Save” (or use the shortcut CTRL + S / Command + S)
Try calling a bunch of phones at once. Ask your neighbors for their phone number. Then you duplicate the code to call more people’s phones!
The live preview inside of Cloud9 is great for testing out HTML & CSS, but it is not powerful enough for us to test out our JavaScript. For that we will use Google Chrome’s developer tools. This will let us see console messages and JavaScript errors.
To open your website in Google Chrome
Now open the developer console.
Mac Directions
Command + option + j
PC Directions
CTRL + Shift + J
In the console here, you can see what’s happening underneath the hood:
Your Twilio phone number is +14848689421
This is just a taste of the console, we’ll be using it a lot more later.
The remainder of this workshop you will be focused on building your own project with Twilio. Below is a possible way you can pursue your project with some suggested time frames. If you get stuck on any of the below steps, ask your club leader for help!
When you open an example, before you do anything else, click “File” → Clone before you start modifying anything
Replace YOUR_TWILIO_ACCOUNT_SID_HERE
and YOUR_TWILIO_AUTH_TOKEN_HERE
with your own sid
and token
Group Messaging - Text a number to send a single message to multiple friends.
Colors - Text a number a color to change the background color of a website.
Group Chat (advanced) - Expanding on Group Messaging, a proper group chat application.
GIF Search (advanced) - Text a number to search the internet for GIFs.
GIF Background (advanced) - Set the background of a website to a GIF that matches what you text.
Send a text message to the given phone number.
Twilio.sendMessage("1-555-555-5555", "This is a text message");
Call the given phone number and say the given words.
Twilio.callAndSay("1-555-555-5555", "Words words words");
Call the given phone number and play the given music file (in the below example, an MP3).
Twilio.callAndPlay("1-555-555-5555", "http://mean2u.rfshq.com/downloads/music/giveyouup.mp3");
You can choose to play any of your favorite songs from YouTube by following these directions:
Twilio.callAndPlay
function to use this new URL.Listen for messages and run the given function whenever one is received.
// When a text message is received...
Twilio.listenForMessages(function (msg) {
// log the received message to the console
console.log(msg.body)
});
Get the most recently received text message.
// Get the most recent text message...
Twilio.getLatestMessage(function (msg) {
// And once it's retrieved, log it to the console
console.log(msg);
});
Retrieve all of the received text messages.
Twilio.getAllMessages(function (messageArray) {
// Print the number of received messages
console.log(messageArray);
});
(your feedback is anonymous + appreciated 💚)