One useful and common way to use Twilio is to create a phone number which forwards to your real phone. You can hand out the Twilio number to colleagues or customers and take business calls on your personal phone without worrying about handing out your real phone number. This also gives you a lot of options for how to handle these calls - you have the full power of Twilio at your disposal.
In this post I'll show you how to create a voicemail system that will jump in when your cell number is busy, or when you don't answer the call. It will play a message to your caller, then record their message and text you a link to the recording when it's done.
Primer: How Twilio Handles Calls
When someone dials your Twilio number, an HTTP request is sent to a URL that you provide. The web server …
You can do all kinds of weird and wonderful things with Twilio's APIs, and if you could call those APIs from your browser you'd be able to build some awesome apps that you could use from a browser anywhere. This isn't directly possible, because the Twilio API doesn't send any CORS headers (for good reason).
A better way to use Twilio API from the browser is to implement a custom web app which accesses Twilio API. In this post I'll show how you can build such a custom web app on the Twilio Runtime platform, using Functions and Assets.
If you follow along with the code, you'll build a single-page app with a backend that is hosted on Twilio. This will let you call the Lookup API from anywhere in the world. I've chosen the Lookup API because it's relatively simple, but using the same principles you …
Java 8 was released in 2014, and introduced a raft of new language features such as Lambdas and the Streams API. A lot has happened since 2014 - Java is now at version 15, but industry surveys consistently report 8 as the most widely-used version, with very few developers using 7 or lower.
In October this year, the Twilio Java Helper Library was updated to use Java 8 features in release 8.0.0. This new major version reflects the fact that the library no longer supports Java 7.
One Java 8 API which sometimes gets overlooked is the CompletionStage API, usually accessed through the CompletableFuture class. The CompletionStage API lets programmers define pipelines of asynchronous operations for data, and handles the asynchronous behaviour for you. You define what you want to happen, and Java takes care of when it can happen.
In this post I'll show how you …
[Header image adapted from this image, CC BY 2.0 from Microsoft NYC]
When building a Java application that uses Twilio's API most developers will have code that runs at startup, initializing the Twilio helper library like this:
Twilio.init( TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
This sets the account credentials as static fields in the
Twilio class. Effectively they are now global settings for the application, and when your code calls the API these will be used automatically, and the helper library creates an HTTP client for you. This means that you don't have to repeat your credentials every time you call a method that uses the Twilio API:
Message.creator(TO_NUMBER, FROM_NUMBER, "Hello 👋").create();
This is convenient, however there are cases where you want more control over the HTTP client. This might be to configure an HTTP proxy, or to provide custom credentials if you are using multiple Twilio accounts or Projects in the same …
[header image credit: NASA Astronomy Picture Of the Day, Sep 15 2020]
Happy Java 15 release day! This release marks three years since the start of the six-monthly release schedule and I am still appreciating the speed of new features and the insight it's giving into what's coming up in Java's future.
In Java 15 there are improvements to tooling, security, cross-platform consistency, efficiency and performance. You can read the full release notes and the list of major features, which are designated a Java Enhancement Proposal (JEP) number. In this post I've selected a few features which I think are interesting for developers.
The features I've selected are a mix of fully-released and "preview" features (more on these later).
How to use these features
Using webhooks is a common and powerful way to configure your Twilio phone numbers to respond to incoming phone calls or SMS (or faxes). You provide a URL and Twilio makes an HTTP request to that URL when a call or message comes in to find out how to respond.
One implication of this is that the URL you provide has to be accessible from the internet, so when you're building your web app to handle the webhooks, how can you know for sure that the requests are really coming from Twilio and not malicious or opportunistic internet scoundrels?
The answer is that all valid webhook requests from Twilio are signed in a header called
X-Twilio-Signature. The algorithm used to create the signature is described in detail in our docs. Briefly, the ingredients of the signature are:
- The details of the request
- The webhook URL as configured …
Traditionally Java applications have been used for long-running processes - web application servers can run for days or weeks at a time. The JVM handles this well: Garbage Collection is efficient over huge amounts of memory, and Profile-Guided Optimization can make your code faster the longer it runs.
However, it’s perfectly possible to write short-lived apps too, and in this post I’ll show how to build a CLI app whose total runtime is just a couple of seconds. You can build sophisticated CLI tools in Java for data processing, connection to databases, fetching data from the web, or taking advantage of any of the Java libraries that you're used to.
I’ll use jbang for packaging and running the app, and picocli to handle argument parsing and output. The app will send an SMS using Twilio’s Messaging API, in a single Java source file less than 100 lines long. …
[header image credit: Iron in the Butterfly Nebula, NASA Astronomy Picture of the Day July 21 2020 (modified)]
Making HTTP requests is a core feature of modern programming, and is often one of the first things you want to do when learning a new programming language. For Java programmers there are many ways to do it - core libraries in the JDK and third-party libraries. This post will introduce you to the Java HTTP clients that I reach for. If you use other ones, that’s great! Let me know about it. In this post I’ll cover:
Core Java APIs for making Java http requests
Since Java 1.1 there has …
We understand how phone numbers work, don’t we? Someone calls your number and your phone rings. But wouldn't it be nice to have more control - to screen incoming calls and treat them differently depending on who is calling?
In this post I’ll show you how to set up a Twilio phone number to handle incoming calls using a Spring Boot app. Your app will look up the caller in a Google Sheet to decide what to do. There are options for blocking calls or forwarding them to other numbers. Any changes made to the Google Sheet will take effect immediately. The sheet looks like this:
Once you’ve set this up you can hand out your Twilio phone number freely, knowing that you can easily block unwanted callers, or redirect people to any other number depending on who they are.
To set this up you will need:
- A Google account …
If you’re working in a statically-typed language like Java then dealing with JSON can be tricky. JSON doesn’t have type definitions and is lacking some features which we would like - there’s only strings, numbers, booleans and
null, so to store other types (like dates or times) we’re forced to use a string-based convention. Despite its shortcomings, JSON is the most common format for APIs on the web so we need a way to work with it in Java.
Jackson is one of the most popular Java JSON libraries, and is the one I use most frequently. In this post I’ll pick a fairly complex JSON document and three queries which I want to make using Jackson. I’ll compare three different approaches:
- Tree model
- Data binding
- Path queries
All the code used in this post is in this repository. It’ll work with Java 8 onwards.