Wiring SMS Into Your Own Project Without a Physical SIM

If you’re building a web service, writing your own autotests or setting up automation scripts on Linux at some point you’re going to run into the classic application problem of how do you get an SMS in?

The traditional way is to connect a USB modem with a physical SIM card to a server and then use Linux tools such as gammu or minicom. For a cloud server, a container, or in a remote location, however, this is all completely unnecessary. You’d end up with the cost of the hardware, the requirement for physical access, the need for a good network signal, and the whole of the manual maintenance.

It is far more convenient to eliminate physical hardware from the equation entirely and work with messages over the network. Using virtual numbers for SMS from GetTempNumber allows you to decouple a phone number from a plastic card and receive incoming data directly into scripts.

Wiring SMS Into Your Own Project Without a Physical SIM

Let’s see how the software approach works, how it differs from the standard SIM card approach, and how you can very quickly add SMS reception to your Linux project.

Physical SIM vs. Virtual Number: What Is the Difference?

Imagine you want to get physical mail, rather than setting up a mailbox or a mail client of some sort. You just want the messages to appear in your project/script as soon as they arrive.

Handling SMS works on the exact same principle.

How it looks in practice:

  • The legacy (hardware) approach: You buy a physical SIM card, stick it in a special modem, connect the modem to your computer and then you have special software to read it. If the modem hangs up, the network goes down, or the operator blocks the card, then you’re stuck and have to go and fix the hardware.
  • The software approach: The number is in the cloud with the provider, and when an SMS comes in, it simply sends the message text to your project via a standard HTTP request. You don’t have to worry about a SIM card, a modem or signal strength at all.

This approach gets rid of all the hardware headaches completely. The number is always available, and messages go straight to where they need to go in plain text.

Two Simple Ways to Receive SMS in Your Code

Once your number and project are set up, the main issue is how to get the message text from the cloud. In practice, there are two very clear and reliable ways of doing that.

1. API Polling

This is the same as checking your email inbox every so often to see if anything new has arrived.

Your script/program makes a request to the service at set intervals, asking, “Are there any new messages for my number?” If the service replies that there is a new SMS then your code will pick it up and do what you need.

Best for simple local scripts, scheduled tasks (e.g. cron), or where your project is running on your own PC and doesn’t have a public IP address that it can be connected to.

2. Instant Notifications (Webhooks)

This is the quicker and simpler option. The cloud service (not your script) is what kicks the process off.

When an SMS arrives at the number, the service will immediately fire off a standard internet request (typically an HTTP POST) to your server address. Your program then receives that request, reads the message text straight away, and processes it.

Best for full websites, bots and web services where low latency is important and you want to be able to react to messages immediately.

In Practice: Connecting Your Scripts to a Virtual Number

In practice, setting up the incoming messages is far easier than setting up the port settings and drivers for the old hardware. All you’re really doing is sending a normal network request from your server to the cloud service using your virtual number.

You get an access key and a target endpoint URL. From the Linux command line or your preferred scripting language, you then make a standard request to the remote number to get the data. The data comes back as a clean text block with sender, timestamp, and message body all separated. To extract the verification code, you just need some basic system filtering tools. Once you’ve parsed the data you can use it immediately for your automated tests, system logs etc. It’s very easy to add a virtual number into your existing processes.

Conclusion

Now that pretty much all the infrastructure has moved to the cloud and containers, we’re well past the point where a plastic SIM card and a rather fiddly USB modem is going to be a viable option for our automation scripts. SMS is just another form of text data that we can deal with in software via standard text and network requests.

This approach saves you a massive amount of time and hassle. You don’t have to be worrying about lost connections in some remote server room or how you’re going to restart a gateway that’s effectively frozen on the other side of the world. Once you’ve got a virtual number in your code, you can deal with the incoming messages and get on with your actual project.

Popular on OTW Right Now!

Add a Comment

Your email address will not be published. Required fields are marked *