Android

How to Automatically Send Text Messages on Android Using Python

The process of sending text messages can be automated easily using the Python Programming Language by writing a few lines of code. Python provides modules like PYAIRMORE that can be used to send text messages from android device. This module has many functions but we will discuss about sending messages. Sending messages using PYAIRMORE is same as sending messages from your android. The only difference is we perform this task by writing a script and by means of scripting, you can automate a lot of stuff. To perform this task, you must have an android device with AirMore app installed on it.

Installing Pyairmore Module

To automate the sending of messages from your android device on another android device, first of all we have to install PYAIRMORE module. We can install this module by running the following command in the terminal.

ubuntu@ubuntu:~$ pip install pyairmore

The above command will install this module and now we can use this module to send text messages on Android.

What is AirMore?

AirMore is a very useful app to transfer data between mobile and PC. It supports both android and iOS. This app can connect a mobile with PC and we can control our mobile from PC. In this article, we will discuss only one function of this app, that is sending text messages on android device by writing a script.

Installing AirMore in Android Device

After installing PYAIRMORE, now we will connect our Android with PC so that we can send messages from android by writing a python script in PC. This can be done by installing AirMore app in Android. To install AirMore app in android, click on the link given below.

https://airmore.com/

After installing AirMore in android device, we will connect our device with PC. There are two ways we can connect our Android device with PC. You can follow any of them. Both methods have been described below.

Connection Using IP Address

After installing AirMore app in your android, now launch the AirMore app and turn on the hotspot as shown in the figure below. When you turn on the hotspot, it will show you Name, Password and IP address.

After this, connect your PC to the hotspot of your Android device and provide the password. Now open browser on PC, enter IP address, we got from AirMore app in the search box and press enter. A blue box will appear on the android screen to confirm connection. Confirm connection and now your PC is connected with Android.

NOTE: Note the IP Address and Port Number shown in the above figure, as they are required in the script we will write later.

Connect using QR Code or Radar Function

After installing AirMore app in your android, go to the following link and click on “Launch AirMore web to connect”.

https://airmore.com/

A window, as shown in the following figure, will be opened.

Open AirMore app in your android and tap “Scan to Connect” button. Now you can connect by scanning QR code on your PC with your android device or by clicking the android device icon in radar on your PC.

Checking Connection

After connecting android device with PC now we can check whether the connection has been established or not by writing a few lines of code as given below

\\ importing module for ip address
>>> from ipaddress import IPv4Address
\\ importing module to create a session between android and PC
>>> from pyairmore.request import AirmoreSession
\\ Creating an instance of IP address
>>> androidIP = IPv4Address("192.xx.xx.xx")
\\ creating a session between android and PC
>>> androidSession = AirmoreSession(androidIP)
\\ checking whether the connection is established or not
>>> print(androidSession.is_server_running)
True

If the android  is connected with PC then output of the code will be “True”, otherwise it will be “False”. After successful connection, now we write a python script to send messages.

Writing Python Script to Send Message

So far we have connected our android device with PC and authenticated the connection, now we move to the python script to send message from android to another android. Following is the complete python code to send message

\\ importing required modules
>>> from ipaddress import IPv4Address
>>> from pyairmore.request import AirmoreSession
>>> from pyairmore.services.messaging import MessagingService
\\ getting receiver mobile number
>>> mobileNumber = “receiver Mobile Number”
\\ getting text message
>>> textMessage = “Your Message Goes here”
\\ creating an instance of ip address
>>> androidIP = IPv4Address(192.xx.xx.xx)
\\ creating session
>>> androidSession = AirmoreSession(androidIP)
\\ creating an instance of messaging service of AirMore among all services available
>>> smsService = MessagingService(androidSession)
\\ sending text message using messaging service of AirMore
>>> smsService.send_message(“mobileNumber,textMessage”)

Adding Script to Crontab

So the above script will send a text message from android device via python script. But it will work when we run this script. To run this script automatically, we will add this to crontab. Run the following command in the terminal to edit crontab file.

ubuntu@ubuntu:~$ crontab -e

A file will be opened in your favourite editor. Go to the end of the file and add the following command to automate the task

0 8 * * * python /path/to/script

This will run the script at exact 08:00 AM every day. If you want to run this script at another time, it can also be done. In the above command, the first digit from left indicates the minutes, next indicates hours, then days of week, then number of month and last indicates the day of the month.

We can schedule this task at any time, any day and any month by using this command.

Conclusion

In this article, we discussed about sending text messages using your android cell number by writing a python script. First, we installed PYAIRMORE module then we discussed about AirMore app and how to install AirMore in android. There are some other modules in Python as well that can be used to send SMS messages via your SIM card. Similarly, if you want an SMS service for your business and you don’t want to use a SIM card, then you can buy a commercial API service such as Twilio and that can save you a lot of effort.

About the author

Usama Azad

Usama Azad

A security enthusiast who loves Terminal and Open Source. My area of expertise is Python, Linux (Debian), Bash, Penetration testing, and Firewalls. I’m born and raised in Wazirabad, Pakistan and currently doing Undergraduation from National University of Science and Technology (NUST). On Twitter i go by @UsamaAzad14