How to make an API on any Android application. For example, WhatsApp

Greetings! My task was drawn: it is necessary to integrate messengers into the CRM system, otherwise it is difficult to analyze the work of managers when they communicate with clients using one common “working” phone. And the first thing to do was connect WhatsApp, as one of the most popular instant messengers for our target audience.

Analysis of the situation...


All that seemed to be needed was to send and receive messages. And so many pitfalls ...

1. Official API


There was no official API in the release then. But Facebook managed to send a link to the form to connect the official API, which is being tested.

2. Third-party service. Connection via WhatsApp WEB


I considered the option of a "gray" method, the use of an unofficial service that sends messages via the WEB interface, is connected by scanning a QR code.

But this is a dependency on someone else’s API, at any moment everything can fall off, and I didn’t want to transfer client numbers to someone’s service. And to write under another author’s API, and even to rewrite it when you move to another service, is also such a pleasure.

Well, you also need to keep the phone on (although some services for an additional fee solved this problem :)

3. Manual mode


Plant a person, let copy-paste messages to customers. Joking as a joke, but from this idea a solution was born!

Decision


Message sending


imageThere is such a program for automating actions for Android, called Tasker , one of the first such programs. But I didn’t figure it out the first time ... I looked for analogues, I came across the Automagic program . In it, events, actions and branches are drawn as a block diagram.

Offtop

And in it I came up with a solution, we do not need root rights. It turned out using a scientific poke method to draw a message sending script, then I will call the stream, so each individual drawn algorithm is called in this program. And now we will analyze his work:

WhatsApp message flow screenshot
image

  1. Every 5 seconds, an event is generated by a periodic timer and transmitted to the next block.
  2. HTTP Request , , ? PHP.
  3. JSON.
  4. , , .
  5. , , , , . , , , , :
  6. , WhatsApp'
  7. . Control UI. ( ), .

    , ,
    sleep(2000);  // 2    
     
    sle = 200;
    ch_c = 0;
    ch = false;
    is_sended = false;
     
    sleep(sle);
    ch = existsElementById("com.whatsapp:id/entry"); // ,      
    sleep(sle);
    if (ch == false) {
      ch = existsElementById("com.whatsapp:id/entry"); //  
      ch_c = ch_c + 1;
      sleep(1500);
    }
    
    if (ch == false) {
      ch = existsElementById("com.whatsapp:id/entry"); 
      ch_c = ch_c + 1;
      sleep(5500);
    }
    
    ch = existsElementById("com.whatsapp:id/entry");
    sleep(sle);
    setText2ById("com.whatsapp:id/entry",text); //  
    sleep(sle);
    
    clickById("com.whatsapp:id/send");  //   
    sleep(sle);
    back();
    sleep(sle);
    back();
    status = 2;
    
    if(ch == true) {  //   , ,    
      is_sended = true;
      status = 1;
    }
    

  8. We send the results of sending a message to the server.
  9. We return to the home screen.

Receive messages


Receiving messages is much easier. We put the event on a notification from the messenger. As soon as a notification arrives, mark it read, and send it to the server as is. That already parses the addressee and the text.

Screenshot of message receiving algorithm


Pros, cons, refinement and plans


The algorithm performs the main function of sending a message perfectly. Since the WEB interface is not used, grabbing a bath from the messenger is less risky, as it seems to me. I rented a weak virtual server, installed Android there and transferred everything.

It takes ~ 5 seconds to send one message to a new contact. To check the delivery and read the message, I developed a separate stream, but it is in draft form and sometimes does not work. Therefore, about him later.

At the moment, the function of receiving media messages (photos, audio, attachments) is not implemented, but this can be downloaded from WhatsApp using the share message button in the future.

A little later I want to write a SIP trunk on top of the virtual machine where the messenger is running, through which it will be possible to call through WhatsApp as through SIP ^. ^ Which can save communication costs.

I also want to take screenshots of avatars and send them to the server to attach a thumbnail to a contact in the CRM system.

Well, in general, the most interesting: This method of wrapping in the API is suitable for almost any application for Android :)

Everything was originally developed and launched on Android 9, xiaomi mi 9 se, root is not right.

Source: https://habr.com/ru/post/undefined/


All Articles