Building a fully featured burner phone with Kotlin
Tags: kotlin, twilio, springboot, burnerphone
Published: 2017-07-28 12:37:09

---
title: Building a fully featured burner phone with Kotlin
published: true
description: In this article, we will look at how to build a fully featured "burner phone" using Kotlin and Twilio that we will then deploy to Heroku.
cover_image: https://www.twilio.com/blog/wp-content/uploads/2017/07/jYjvOBIKVWapsjbGMKuRHIqddPnzLAwytYMbrrkySu6wr-zX4Ym_wCq6M7fSMhui3HHR-5FTsUAV35LUaIDqLbXV6l_KXq_Ts2nSM2Ks-P6Emq5jM057wTeXuQiheuJYDgFWCx8v.png
tags: [kotlin, Twilio, springboot, burner phone]
canonical_url: https://www.twilio.com/blog/2017/07/building-a-fully-featured-burner-phone-with-kotlin.html
---

You walk into your favourite coffee shop and today they ask you if you’d like to register for a loyalty card. All they need is your name and phone number.

On the way back you stop for groceries and the cashier asks if you’d like to enter a prize draw. All they need is your name and phone number.

It’s finally time to put that lawn mower you’ve not used in years on Craigslist but at the end of the ad, it asks you to enter your phone number so buyers can contact you directly.

![spam messages coming in](https://www.twilio.com/blog/wp-content/uploads/2017/07/tAczNU_xh6czYFKDxwC2smcRLRUQwjL6IehMlfIZw1wSgDz0Zw-UU-gZ83CtTFQJNVBePv0zHyG5ykkYZMleGb6zQ0597kI1AsJurCTwHsiQtFyoYiWH1yzIq8WJVvbI9rRLauFt.png)

## You need a burner phone!
Let’s look at how we can use Kotlin and Twilio to build and deploy a burner phone application so that we can use multiple phone numbers for these situations. If you just want to download the code you can have a look at [this repo](https://github.com/mplacona/BurnerPhones) or deploy it to Heroku using the button below.

[![Deploy to Heroku][2]][1]

## Our tools
 - I will be using [IntelliJ IDEA](https://www.jetbrains.com/idea/) for the code but feel free to use your preferred IDE as long as it works well with Gradle.
 - One or multiple Twilio Phone numbers for creating various burner phones. You can [get them here](https://www.twilio.com/try-twilio).

## The project
Let’s start by creating a new Gradle project in IntelliJ and adding Kotlin (Java) as the library.

I have described the entire process in the first part of [Send and Receive SMS messages with Kotlin](https://www.twilio.com/blog/2017/05/send-and-receive-sms-messages-with-kotlin.html). You can follow it [here](https://www.twilio.com/blog/2017/05/send-and-receive-sms-messages-with-kotlin.html#kotlin-get-started). Alternatively, you can just clone the repository as follows.

```
git clone git@github.com:mplacona/BurnerPhones.git
cd BurnerPhones
git checkout starter-application
```
With that project open on IntelliJ, start by right clicking on `App.kt` and choosing “Run”.

![Run Kotlin application](https://www.twilio.com/blog/wp-content/uploads/2017/07/s1zKnoqIiSRnbhRZeD8XMv0draOzy3uZ4H5xyIplW7ycTzzHodnSB86ODBWbPDk36u8ZwKTLPMdWJb_bew_ZPPgOWMHlrXK3oNvmlEGFTskKSaxfYAAJAD_34IugyViAF8RRZzPS.png)

In your browser go to [http://localhost:8080/sms/](http://localhost:8080/sms/) and you will see the default controller displayed.
Let’s change one configuration in `App.kt` so the application knows where to forward the SMS messages and calls to. Inside the `main` function add the following:

```
fun main(args: Array<String>) {
    System.setProperty("MY_NUMBER", System.getenv("MY_NUMBER"))
    SpringApplication.run(App::class.java, *args)
}
```
I have my telephone number set as an [environment variable](https://en.wikipedia.org/wiki/Environment_variable) on my computer. My colleague Dominik wrote a nice article explaining [how to set environment variables](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html) on Mac, Windows and Linux.

Once you have the “MY_NUMBER variable set correctly for your environment with your own telephone number we’re ready to start building the application.

## Forwarding SMS messages
We still want to get every SMS message sent to our burner phones, but we want them redirected to our own number. The beauty of doing this is that if we ever get tired of being spammed from a certain number, we can just discard that number on the Twilio console and be done with it.

Open `SMSController.kt` and create a new mapping called `forwardSMS` just below the one that is already there.

```
import com.twilio.twiml.Body
import com.twilio.twiml.Message
import com.twilio.twiml.MessagingResponse
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
 
@RequestMapping("/sms")
@RestController class SMSController {
    @RequestMapping(value = "/")
    fun helloSpringBoot() = "A nice looking sms controller"
 
    @RequestMapping(value = "/forwardSMS", produces = arrayOf("text/xml"))
    fun forwardSMS(@RequestParam(value = "From") from: String, @RequestParam(value = "Body") body: String): String{
        val message = Message.Builder()
                .to(System.getProperty("MY_NUMBER"))
                .body(Body("Message from: $from \n $body")).build()
        return MessagingResponse.Builder().message(message).build().toXml()
    }
}
```
This new mapping will tell Twilio to forward any inbound SMS to our own number. It will forward the message so we still know whose number originally sent it.

It is useful to know who sent the message so you can respond if you need or just as easily add a condition to the top of the mapping to block any messages from that specific sender.

Restart the application and test it out by sending a `POST` request to it with [cURL](https://curl.haxx.se/) for example:

```
curl -X POST 
  http://localhost:8080/sms/forwardSMS 
  -F From=+1234567890 
  -F 'Body=Checking my TwiML works'
```
And the result should be the [TwiML](https://www.twilio.com/docs/api/twiml) that will tell Twilio to forward the message.

Before we configure our Twilio numbers let’s add call forwarding to our application.

## Forwarding Calls
To forward calls we will use the same principle as above but will have it in a different controller to keep things neat. If you cloned the starter application you should already have that controller.

Create a new Kotlin controller called `CallController.kt` alongside `SMSController.kt` if you don’t already have one.

In that controller add the highlighted code or just copy everything if you still have an empty controller.

```
import com.twilio.twiml.*
import com.twilio.twiml.Number
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
 
@RequestMapping("/call")
@RestController class CallController {
    @RequestMapping(value = "/")
    fun helloSpringBoot() = "A nice looking call controller"
 
    @RequestMapping(value = "/forwardCall", produces = arrayOf("text/xml"))
    fun forwardCall(@RequestParam(value = "From") from: String): String{
        val call = Dial.Builder().number(Number.Builder(System.getProperty("MY_NUMBER")).build()).build()
        return VoiceResponse.Builder()
                .dial(call).say(Say.Builder("You have a call from $from").voice(Say.Voice.ALICE).build())
                .build().toXml()
    }
}
```
Just like before, we are adding information about the caller for when the call is forwarded. So when you get a call to this Twilio number you hear a message that tells you the number that is calling.

You can also test that this is working correctly by making a POST request to it and checking that the [TwiML](https://www.twilio.com/docs/api/twiml) returned looks right.

![curl request to check for TwiML](https://www.twilio.com/blog/wp-content/uploads/2017/07/9mv1RiAvhqGQ18XbZ9mTqU9EXnkx46MGg3kxZ9G8rnHblvAW7KKS9rhu_b7jyHzF4BLaow63BXeV9GZoJSOrgieiwnCynkQYjbyk0QrtVlkR62rNwNzeeQETNJZIb-bjKe5gZnds.png)

Before we configure our Twilio numbers to use the application we need to deploy it somewhere. Let’s look at how to do this with [Heroku](https://www.heroku.com/).

## Deploying your Kotlin application to Heroku
If you haven’t yet create an account on Heroku. Once you have an account and installed the [heroku toolbelt](https://devcenter.heroku.com/articles/heroku-cli), go to your terminal and log in from there.

```
heroku login
Enter your Heroku credentials.
Email: me@example.com
Password:
```
Still in terminal create a new Heroku application.

```
heroku create
```
If you haven’t created this application from scratch but cloned the `starter-application` repository, you will now need to merge it into master as follows:

```
git commit -am "complete tutorial"
git checkout master
git merge starter-application
```

You can skip the step above if you created the project from scratch, just make sure you in master and we’re ready to push this to Heroku.

```
git push heroku master
```
This will take a few seconds and when it’s complete you should see a message that looks like this on your terminal.

![creating a heroku application in Kotlin](https://www.twilio.com/blog/wp-content/uploads/2017/07/NPbry1_yi4TU-__rUGzkTEWJj-tNodFdlopvlrnzKUfbll3fhvGJiG-l2966nyDb1-5ZTX9skrjQ4QFMUYzR_-kDjhdT-EnI9oD-73BbNC48S5S9yIEMYkmsTHbNFjrtJpkIt7uS.png)

Copy this URL as we will use it later on to configure our Twilio numbers.
Just like in our local environment we need to set a phone number as an environment variable for this deployment. You can do this by running the following on your terminal:

```
heroku config:set MY_NUMBER=+441234567890
```

Make sure you use your own telephone number as the value for `MY_NUMBER`. Now we’re ready to configure our burner numbers.

## Creating burner numbers
The beauty of this application is that because we’re not hard-coding anything in the application itself, it can be used for as many Twilio numbers as we want. In our case, we could have one number for loyalty cards, one for prize draws and one for selling things on the internet.

Head to the [Twilio console](https://www.twilio.com/console/phone-numbers/incoming) and get as many numbers as you like, then configure and save them all as follows:

![Twilio console](https://www.twilio.com/blog/wp-content/uploads/2017/07/JiXCHuCdXGU4ZtcjAa3e1UuAT2f2AQB9ptBC8rCSpBTAPVqeZG2hNS51566XXhEerg7DffyKtw6MIlJlHwlTTiH7spCohyYRX000RbbM3WXuSYBAqIgkhX5HzwiNadr4vEPVlq5j.png)

Make sure you change the application URL to match what you copied earlier when you deployed your application to Heroku.
Give those numbers away instead of your own number now and just wait for all the messages and calls to start coming in.

![incoming messages to burner phone](https://www.twilio.com/blog/wp-content/uploads/2017/07/E54orG-nolK-BzTAftsdIFK9ESQh38608oRoZ-HOgNME99AH8sJddfljQMoOro6BNkUVGUuwalXGli0ub2zQCgXHK9GgvUzNNaJddpT-u-RGdRJehvBa62lljM65iEfw99OBn8C.png)

## Burn it!
![gif of burning superhero](https://www.twilio.com/blog/wp-content/uploads/2017/07/yIc_arxv5GJCxXWGqLO_1ruyfD48xh8Yo00m0RDw_0NQYEOoahehBDEzf1B76dH2hMwppC_zmww86Mkcocz_0S4WuqD5Rfase6d1yhb68fuYVl9hUCuY8oYnrT_RBjbRnYZZ70MJ.png)

Having burner phones that can just be created and disposed of when they’re no longer needed has never been so easy.
I personally have each one of my burner numbers added to my contacts so they display nicely when I get a call or text message and I know roughly what to expect.

A nice addition to this application would be to add a way of blocking senders before messages or calls are forwarded to us, or adding a voicemail system for certain numbers to make sure we never miss any calls.

I would love to see what new features you add to it. Hit me up on twitter [@marcos_placona](https://twitter.com/marcos_placona?lang=en) or drop me a note on the comments below to tell me more about it.

------------
<i>[Building a fully featured burner phone with Kotlin](https://www.twilio.com/blog/2017/07/building-a-fully-featured-burner-phone-with-kotlin.html) was originally published on the [Twilio blog](https://www.twilio.com/blog) on July 12, 2017.</i>

  [1]: https://heroku.com/deploy?template=https://github.com/mplacona/BurnerPhones/tree/master
  [2]: https://www.twilio.com/blog/wp-content/uploads/2017/07/rRESopN9OU-mG_8U4-m4Ltz-GneWE1HkYs0WEOVpZLb7XN-5h6sU0gUy7tzQc501sGJ7BivD0JfViasCuYVwB6uyKTL6wCGsARp0GT9ZkoHO6mRlq-Df80H8POJo7x6UOnAH_qnv.png