Hackernoon logoI Automated My Whatsapp Chats on Android and Here's How You Can Too by@aditiBhatnagar

I Automated My Whatsapp Chats on Android and Here's How You Can Too

Aditi Hacker Noon profile picture


www.aditi.fyi | Security Enthusiast, Engineer @ Microsoft, Founder @digitised.in

Android Accessibility Service to Alter the Way You Use Your Mobile

I am a big fan of automation. Honestly, in this era of the attention economy, I try creative ways to save my time wherever I can.

So one fine day, when a friend of mine said, "Hey, can you please remind me to write that essay tomorrow", the inherent spirit kicked in and I ended up coding an Android app that can automatically send messages to her.

And think about that! How interesting the concept is! There is a difference between setting an alarm for yourself and getting reminded of stuff and someone texting you to remind you to do something. When a message is sent by a person, it has that special effect! So, I thought, why not use technology to intervene in human lives in a psychologically positive way?

And There it Comes: Automate Whatsapp Chats

Yeah, you can use it in A LOT of ways, in fact, once you have learned the technique, you can pretty much automate anything on Android - from automating sending emails, switching songs, clear data, and what not!

This is what the app looks like:

I have used Accessibility Service for it.

What is Accessibility Service, you may ask?

Accessibility services are a feature of the Android framework designed to provide alternative navigation feedback to the user on behalf of applications installed on Android devices. An accessibility service can communicate to the user on the application's behalf, for example by converting text to speech or providing haptic feedback when a user is hovering on an important area of the screen

Source: https://codelabs.developers.google.com/codelabs/developing-android-a11y-service/#0

I have used the Accessibility Service to create this automation for myself. The service enables us to not just view text but also, perform actions on the user's behalf. So, how can you create similar automation for yourself?

Follow these steps:

1. Define the service and XML config:


<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

        <activity android:name=".MainActivity"
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            android:label="Automate Chats WhatsApp"
                <action android:name="android.accessibilityservice.AccessibilityService" />

                android:resource="@xml/accessibility_service_config" />

2. Define the Accessibility Service, extends AccessibilityService:

Reference: https://developer.android.com/reference/android/accessibilityservice/AccessibilityService

3. Find out the IDs of UI elements that hold the:

  • Name of the person on WhatsApp
  • Edit Text (where you type the chat)
  • ID of the send button (note that the send button appears only once the user has typed the text)

You can use tools like UI Automator to find these IDs. And there you go!

Once you have the IDs, you can then define the implementation of the method,

public void onAccessibilityEvent (AccessibilityEvent event) { ... }

Get the root from the event nodes, traverse through the tree and find out the nodes having the above IDs acquired in step 3.

Once you have the IDs, you can then use the method performAction to simulate sending the chats:

 AccessibilityNodeInfo textBox = getNode(rootNode, chatBoxRefId);
                Bundle arguments = new Bundle();
                if (convIndex == convs.length -1) {
                    arguments.putString(AccessibilityNodeInfoCompat.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "<3");

                } else {
                    arguments.putString(AccessibilityNodeInfoCompat.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, convs[convIndex % (convs.length - 1)]);
                    convIndex ++;
                textBox.performAction(AccessibilityNodeInfoCompat.ACTION_SET_TEXT, arguments);
                AccessibilityNodeInfo sendButton = getNode(rootNode, sendButtonRefId);

So, that is all folks! Easy-peasy! Try it out and have fun! Stay curious and creative, I'll see you in the next post soon.

If you want an already coded app, checkout: https://www.aditi.fyi/whatsapp-hack

Previously published at: https://www.aditi.fyi/post/i-automated-my-whatsapp-chats


Join Hacker Noon

Create your free account to unlock your custom reading experience.