Photo by James Harrison on Unsplash

How I Create a Whatsapp REST API and CLI to Automate My Messages With Golang (Part 1)

José Daniel
Geek Culture
Published in
3 min readMar 25, 2021

--

In the last weekends, I searching for some coding challenge, and in my searches, I discover a golang library that allows me to connect, send and receive messages when scanning the generated string QRCode by the library, after this the idea comes to my mind! Why not create a REST API to get the QR Code and create endpoints to send messages to any number using my WhatsApp session?

Trying to connect session inside the request

The first way that I thought of was to create the endpoints and runs the concurrency WhatsApp connection inside the request execution, like:

It wasn’t going to work, and I discover it after because the response was being written after the Whatsapp Connection closes, but the QRCode was generating on execution time, so I thought a more daring way to achieve the response and not stop the Whatsapp connection, on the development process I was creating my own CLI to automate the creation of boilerplate projects, so it was very fast if I wanted to create the CLI program for the Whatsapp connections, I thought that in this way, I could use the program inside the Whatsapp REST API, and in the same instance the CLI would keep running, assuming that the REST API was running on a docker container.

So, I present you the CLI for Whatsapp Messaging:

https://github.com/jsdaniell/whats-cli

You can install also it as an npm package, or directly use it npx if you have the NodeJS installed on your machine, I use gorelease to generate the binaries that I would use in the future on the REST API, you can easily try a test with npx whats-cli connect-qr . You can see other commands on the repository.

Using the CLI inside REST API

Now the primary change on my REST API code was to add the CLI binary download to use the commands inside the requests controllers, so before runs the API my code was like this:

Getting the Whatsapp CLI, now was only necessary to run the commands inside the requests, and getting the output on execution time writing for the response server, so the CLI program would continue the process on Docker Machine where the REST API was executing, my connect function now is:

Until now, this only supports one connection per instance, but it only adds a parameter to create a different file session.gob on the docker image directory where the API is running.

Testing the API locally

To test the API locally, clone the git repository, download modules dependencies, and runs the main file.

go mod download

go run main.go

The API’s endpoints until now are:

GET: https://localhost:9000/getQrCode >
returns the QRCode PNG on the response.

GET: https://localhost:9000/disconnect>
disconnect the actually logged session on the server.

POST: https://localhost:9000/send>
send a message to some number.

{
"number": "558599999999",
"message": "Message"
}

I like to write about my repositories and this is also one more weekend of so much learning, about automation, this is in development, but all the tools are ready to use, and now it's time to code the automation tool and write the PART 2 of this article.

--

--

José Daniel
Geek Culture

Always learning! When not learning, consider yourself challenged!