Sending WhatsApp messages automatically

My site organizes weekly soccer matches, recurring poker nights or other events. People can give feedback, see other member’s feedback etc.

I wanted to implement a feature that feedback of a user of the site (e.g. “John will join next week’s training”) is pushed via WhatsApp to other members of the group (which opted-in for such messages). I already implemented that with Telegram, which is easy, because Telegram offers an API.

WhatsApp doesn’t. It is programmatically impossible to create automatic messages in a clean way, unless you registered for a business account (which requires a certain reach of your site and also a monetary invest).

Here is the solution: Use an Android phone with WhatsApp and remote-control it via an automation tool (here: Tasker and some plugins).

Note: This is neither usable nor intended to be used for Spam messages! WhatsApp will block your phone number immediately if you send Spam via this (or any) mechanism!

Rough overview

The solution works as follows:

  • You have a dedicated (old) phone with a dedicated WhatsApp account and a dedicated Gmail account. The phone is always running.
  • You send an email to this Gmail account with a specific subject line that contains the target phone number, message text and a password.
  • An automation App on the phone will intercept the notification for this email, extract the needed data and simulate a user’s manual input to send a WhatsApp message.

What do you need?

  • A mobile phone (Smartphone) which runs a rather recent Android. This phone will be fully blocked and cannot be used for anything else. Basically it will send out the WhatsApp messages automatically. The phone doesn’t have to have much processing power or memory, so it can be an outdated model, e.g. some phone you retired already, or a phone shot for small money on Ebay. I run this service on a Sony Xperia Z3 (which is oversized) with Android 5.0.2.
  • A SIM card (phone number) which is needed to get a WhatsApp account. Obviously this SIM card should be sitting inside the phone. You’ll only need it to register WhatsApp on the phone, and you can remove it afterwards, if the phone is connected to the Internet otherwise (i.e. via WiFi). However, the WhatsApp messages will have this phone number as sender number.
  • A free Google Mail account (to be used for this phone).
  • The Android “Tasker” App. This is not free, but probably the most powerful App available on Android. It allows to completely automate your phone. Not easy to understand, but worth its money.
  • Two extensions to Tasker, namely AutoInput and AutoNotification. Both Apps are controlled via the AutoApps master App, and both are not free, but offer a 7 day trial mode to play around with them.

Prepare the phone

  • Setup the phone’s software from scratch: Create a Google account, install GMail with this account on the phone and install WhatsApp. Install Tasker, AutoInput and AutoNotification.
  • Set up Tasker:
    • Prepare the “Tasker_Gmail_to_Whatsapp_Profile.prf.xml” file (attached to this post; unzip it first) with a simple text editor: Change the stated phone number +49177999999 (in line 750) to the mobile phone number of the phone. Change the SECRETPASSWORD in line 743 to a password of your choice. This password has to be contained in the emails which trigger a WhatsApp message, see below.
    • Import the Profile (long-click on PROFILE tab) to Tasker from the file “Tasker_Gmail_to_Whatsapp_Profile.prf.xml”
    • After this import, you will see a new profile “Gmail To WhatsApp”, and also a new task “Whatsapp Message”. Click on the Profile; if there is a warning icon, you need to have AutoNotification installed before.
  • Set up AutoNotification: These two Apps (AutoNotification and AutoInput) are used from within Tasker. However, they need to be set up correctly: Start the AutoNotification App and start Trial. Go to Tasker, Profiles, click on “AutoNotification” (next to yellow icon). Click on “Configuration” (edit icon). Set:  Action Type = Created, Persistency Type = Both,  Apps:  Choose Gmail. Click OK checkmark (at top right)
  • Set up AutoInput : Start the AutoInput App and start Trial. Enable the AutoInput Accessability service.
  • For both extensions, don’t forget to buy the full version after the 7 day trial version ended, otherwise your setup will silently stop working.
  • Your phone may ask for other settings regarding battery optimization, foreground App execution etc. In all cases, the phone and the Apps may not go to sleep, so any battery optimization features should be switched off.
  • Setup your phone: Enable the Developer options (click seven times on the Build number in the ‘About phone’ screen in your phone settings). In the Developer options, there is an item to let the phone stay active when being charged: Switch it on (the phone may not go to sleep, ever). Then attach a charging device to your phone and leave it charging.
  • Setup Gmail: Each incoming email should display a notification in the notification bar – this should be the default behaviour already. Also, if that is not already default behaviour: Set up your Gmail account such that emails are pushed to the phone immediately.

Try it out

You are all set now. Now send an email to the Gmail account which you just created (and linked to the phone). It needs to have the following subject line:

Target number (in international format)|text to send|secret password|

Example: +4917712312312|Hello, this is an automated message|SECRETPASSWORD|

The target number is the WhatsApp phone number of the recipient, in international format (e.g. “+4917799999999”). No spaces are allowed. The recipient doesn’t have to know your WhatsApp number; if you never contacted him before from your number, WhatsApp will offer him to block you. So please inform him beforehand; e.g. with an Opt-In mechanism.

The text to send is exactly that: Whatever you want to send. It shouldn’t be too long.

The secret password is the password you defined in the Tasker profile (default: SECRETPASSWORD). This only protects your script so that no random email to your account will trigger the script.

The three items are separated by a vertical hyphen (pipe) “|”, and there is also a pipe at the end.

The email body can be empty.

The email sender is irrelevant.

How does it work?

Once your phone (i.e. the Gmail App) receives the email, it will show a notification in the Android system notification bar. AutoNotification will intercept this notification and extract the email subject. It will hand over this subject to the Tasker script.

Tasker separates the three items from the subject, compares the password and – if it is correct – triggers the WhatsApp App. AutoInput fills in the message in WhatsApp and sends it. Then it goes back to the screen it was before.

You can actually see that happen on screen, as it’s basically only an automated control of the phone.

Now you can put your phone somewhere (leave it on charger!), decrease the screen brightness to lowest setting (to save energy) and just send emails to your phone.

Every once in a while (every few days) you should check your phone if everything is still okay. There may be many notifications in the notification area; you may want to remove them every once in a while.

How much does it cost?

You need the phone, which you may have already sitting around somewhere. Otherwise a cheap China phone for 30 EUR should be sufficient, or a second-hand phone from Ebay.

You need the SIM card. There are free SIM cards available, e.g. in Germany at Netzclub which even have some data included, but any prepaid card should suffice if you connect your phone to WiFi afterwards.

You need the Tasker App and the extensions, altogether for around 8 EUR.

You need the energy. Assume around 4 kWh per year for the phone, which results in one or two EUR per year. However, leaving the charger connected all the time is expensive: This will costs another 10 to 15 EUR per year. This heavily depends on the used charger; good chargers need less power when not charging.

By the way, my script is free, but I’d be grateful for any donation. See the Donation section at my Unspontan-site.

This article was written by Frank

See for more info about me...

Leave a Reply

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