desktop python script retrieve sms messages sent to android device

Viewed 1720

I have a web scraper written in python/selenium that runs daily on a home server. One of the sites I'm scraping have recently introduced 2FA which overall I'm in favour of however the downside is that it has broken my automation as now a one-time-passcode is sent via SMS to my mobile number - which needs to be typed in before the script can continue. To get my script fully automated again I need a means for my desktop python script to poll my android device for its SMS messages.

I looked at Twilio however the docs suggest it doesn't fit my use case as;

Hosted SMS provides a method for customers to use Twilio ... to send and receive messages on voice-enabled numbers they already own ... Hosted SMS supports USA & Canada in Beta. Mobile numbers are not supported.

Google have the SMS Retriever API which also doesn't fit my purpose as it requires a custom hash code in the message body and the SMS User Consent API requires the user to tap the screen to approve each request (which of course isn't fully automated).

The quickest haphazard method I can think of to get these OTPs to my python script would be to write a simple discrete mobile app with permission to access SMS and create the following workflow:

  • have the desktop python script pause when it reaches a 2FA screen and listen on a specified UDP/TCP port
  • have the mobile app simply looking for messages from a target number (or with a target phrase)
  • when such a message arrives the app would encrypt the message text, and
  • send it via UDP/TCP to the aforementioned port on my home server
  • let the script decrypt and parse for the OTP (or timeout after x minutes)

Ideally the only thing that would hinder the workflow is if my phone happened to be off at the time that the script runs.

... rather than cludging this together I am wondering if anyone can recommend a better means (or API) of achieving this?

1 Answers
Related