Making your own SMS API! Is it a viable alternative to payed service?

One day I was thinking that it would be nice to at least be able to simulate a 2 factor scenario where a registered user would get an authorization code sent to them using SMS upon login.

How can I experiment with this without having to invest money upfront?

Idea

If I can interact with a used phone that I have using command line somehow, then what I can do is to wrap that in a REST API so that I can interact with it in a convenient way programmatically.

What you need

  • An reasonably modern Android smartphone (version 9 +)
  • Some knowledge about Python and the Flask framework.
  • A webbserver where you can host your API.

Steps

These are roughly the steps I took to get this working.

steps

Android Debug Bridge (adb)

Download and install adb from here you only need to "standalone" version of the tool for your platform.

Using this tool we can send command to our phone using command line, and if we can do it from command line... it means we can write code around it.

Developer mode / USB Debugging.

You need to enable developer mode on your phone, when done you need to enable usb debugging. Once you connect the phone to your computer, be sure to allow the connection to be used for debugging.

Device Security

To be able to interact with the phone via python script for example you will have to disable lock screen on the device, if this is not disabled then you will have to unlock the phone each time you want to send SMS.

ADB Commands

First step after having prepared the above was to just see if I could send SMS from command line.

It was certainly not obvious how to use this for sending SMS, and if you go to google you will find years of history of people attempting this on many generations of android.

Not sure if this would have been super trivial for an actual android developer, but even when looking at the developer documentation for android I did not fully get it.

Have a look in the for yourself in the android documentation (link below), maybe you learn something that I missed or did not understand and can share it with me.. thanks!

https://developer.android.com/reference/android/content/Intent#AC TION_SENDTO

Anyway, I save you the trouble of searching.. these are the commands (for Linux):

** 1. List Devices**

This should give you a list of connected devices. Note that is you are running this on WSL on Windows then USB devices will not passed to WSL.

adb devices

** 2. Initiate SMS message**

This command will start the messaging app and a new message to the listed number. Note that you will have to escape spaces in the message text for all text to be included.

adb shell am start -a android.intent.action.SENDTO -d sms:"+{phonenumber}" --es 
sms_body "{message}" --ez exit_on_sent true'

** 3. UI Navigation**

Since this interaction with the phone all happens in the android user interface, it also means we have to navigate it to actually send the SMS.

The commands listed below corresponds to pressing: TAB, TAB, ENTER

adb shell input keyevent 22  
adb shell input keyevent 22  
adb shell input keyevent 66  

Conclusion

This was a fun experiement and I certainly learned new things while doing it, however it is by no means a viable solution for anything other then an experiment.

If its not obvious why this is not viable, let me tell you.

  • Since you basically interact with the Android UI using command line to do the steps needed to send the sms, it also means that the process is pretty slow.

  • As you saw in my introduction I was already aware that this is not viable for the usecase I has in mind.. which is to work as 2 factor authentication.

  • For a "production" solution you would need to be able queue requests for processing, as well as having some means of sending more then one SMS at a time. (im sure there are plenty more good reasons).