Greetings! My task was like that: 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 of that seemed to be needed just to send and receive messages. And so many pitfalls…

  1.Official API

There was no official API in the release. 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, 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 not so great.

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

3.Manual mode

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

Decision

Message sending

There is such a program for automating actions under 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 Automagic program. In it, events, actions and branches are drawn as a block diagram.

And in it I came up with a solution, we do not need root rights. It turned out by the method of scientific poking 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 its work:

1.Every 5 seconds, an event is generated by a periodic timer and transmitted to the next block.

2.The HTTP Request block asks the server if there is the next message in the queue that needs to be sent. The backend is written in PHP.

3.Parsing of JSON server response

4.If the label in the response came from the server that you need to send a message, go ahead.

5.Check if the device’s screen is turned on, if not, send a notification in order to wake up the phone. If the screen is on and another thread is not running that checks the status of whether the message has been delivered, then:

6.We generate a link and open it to initiate a dialogue with the phone number received from the server in WhatsApp

7.The most interesting. Control UI block. In it, in the form of a code, the logic of checking the success of opening a chat is implemented (checking registration in the messenger), inserting text into the input window and clicking the Send button.

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.

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 sec 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 it 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, without root rights.