SQL Server - How to send messages to Whatsapp contacts, groups and mailing lists via API

Views: 2.497
Reading Time: 15 minutes

Hey guys!
In this article, I'll share with you a solution I've been looking for a long time ago, which is how to send messages to Whatsapp contacts, groups and broadcast lists using WebRequests. I had done something similar using other tools, but never Whatsapp, which does not have an official API and is completely closed on that.

Although there are even better communication tools than Whatsapp, like Telegram itself, 100 from 100 people I know have a Whatsapp account and most don't have a Telegram, so I think this article may end up being a public utility for who wants to create any automation that involves Whatsapp.

Check out my articles on other communication tools:

Whatsapp Communication API

The main character of this article, the NETiZap API is what makes easy integration between the SQL Server database and the Whatsapp communicator possible, since it does not have an official API and is completely closed about it. I have tried to do some direct integrations with Whatsapp and after some testing I was banned by 24h and notified that next time my number would be banned platform forever. After that, I left these tests aside .. lol

That's why using an API is so practical and safe: The number that triggers is the API number and not your personal / corporate number.

For testing this API, you can use the free demo, but for real routines deployment, you must purchase a paid plan:

PlanomodalityQuantity of ShipmentsValue
Free PlanFree for Demonstration, Testing or Periods.PersonalisedFree
Prepaid PlanMessage Pack + Support100$ 0,20 for message + $ 50,00 monthly
Post Paid PlanMonthly + SupportUnlimitedR$ 200,00
Postpaid Reseller PlanMonthly + SupportUnlimited$ 130,00 (5 line minimum)

Rules:

  • Use for spam purposes is prohibited
  • Rules supervised and enforced by whatsapp

Within the API package, you can use the ConsumersAPI.exe application, which allows you to test all API methods with default parameters:

You can also test using Postman:

Documentation for this API is available. this link here. To download the API developer package, click here on this link.

If you are interested in hiring this API for your company, please contact Thiago, developer of this API, at +55 27 99802-2075. The number used for sending the messages is not fixed and can be changed / chosen by the contracting company.

Sending Messages to Whatsapp with OLE Automation

Click here to view content
One way to do this communication from SQL Server to the API is by using the OLE Automation, which instantiates Windows objects into SQL Server. This method is already very old in SQL Server, known for some memory leaks and should be used ultimately, as SQLCLR is more secure and flexible than this method (and accentuation works), but a bit more complicated to implement. too.

Sending private messages through Whatsapp

To send a message to a specific Whatsapp contact, use the code below:

Result:

How it arrives on mobile:

How to retrieve the list of available groups

To list the available groups in your account, you can use the script below. To send a message to the group, simply use the same private message sending script and replace the recipient's phone number with the group id returned below.

JSON handling was done with the OPENJSON function, available from SQL Server 2016. If your version is earlier than this, you can replace it with a simple SELECT in the variable and manually identify the desired group id. If you would like to know more about JSON string handling, take a look at my article. SQL Server 2016 - Using Native JSON Support (JSON_VALUE, JSON_QUERY, OPENJSON, FOR JSON, ISJSON, JSON_MODIFY).

Result:

How to send messages to a Whatsapp group

Now that I've demonstrated how to send a private message and how to list the groups, let's join the two together and send messages to one group!

How it arrives on mobile:

How to retrieve the list of available broadcasts for your account

Almost identical to the account group listing, the script below lets you list the lists of broadcasts available for submission via the API:

Result:

How to create SQLCLR libraries (Only if using SQLCLR)

Click here to view content
In this article, I will demonstrate two techniques for performing HTTP requests through SQL Server: SQLCLR and OLE Automation. If you want to use SQLCLR, this topic is for installing the objects you need to be able to follow the examples in this article. If you choose to use OLE Automation, you can ignore this topic.

The simplest and most robust way to perform HTTP requests through SQL Server is undoubtedly using the SQLCLR (please click here if you don't even know what SQLCLR is).

How to create stpWs_Requirement in your environment (SQL Server 2012 to 2016):

If you are using SQL Server versions 2012, 2014, or 2016, you can use the script below to create the assembly (DLL) in the desired database and the stored procedure “stpWs_Requisicao”, which is used to perform the API HTTP request. .

How to create stpWs_Requirement in your environment (SQL Server 2017 +):

If you are using SQL Server version 2017 or greater, you can use the script below to create the assembly (DLL) in the desired database and the stored procedure “stpWs_Requisition”, which is used to perform the HTTP request from the API. . This script is different from the previous one, since as of the 2017 release, SQL Server now requires assembly signing even in Safe mode, due to security changes in the .NET Framework.

StpWs_Request C # source code:

If you don't want to create the assembly I made available above and you want to program your SQLCLR yourself in Visual Studio and then publish it to the database, I will make the C # source code available for this Stored Procedure.

Sending Messages to Whatsapp with SQLCLR

Click here to view content
Now that I have demonstrated how to connect to the API, making it possible to send messages to Whatsapp with any programming language, I will demonstrate how we can do this through SQL Server. For those who already follow my blog, know that there are at least two easy ways to do this: Using SQLCLR and OLE Automation.

Sending private messages through Whatsapp

The first test to perform is by sending private messages to a contact. Its use is very simple:

Result:

How the message arrives on mobile:

How to retrieve the list of available groups

To list the available groups in your account, you can use the script below. To send a message to the group, simply use the same private message sending script and replace the recipient's phone number with the group id returned below.

JSON handling was done with the OPENJSON function, available from SQL Server 2016. If your version is earlier than this, you can replace it with a simple SELECT in the variable and manually identify the desired group id. If you would like to know more about JSON string handling, take a look at my article. SQL Server 2016 - Using Native JSON Support (JSON_VALUE, JSON_QUERY, OPENJSON, FOR JSON, ISJSON, JSON_MODIFY).

Result:

How to send messages to a Whatsapp group

Now that I've demonstrated how to send a private message and how to list the groups, let's join the two together and send messages to one group!

Group result:

How to retrieve the list of available broadcasts for your account

Almost identical to the account group listing, the script below lets you list the lists of broadcasts available for submission via the API:

Result:

So, did you like this tip? You can now create alerts, monitoring, send attachments, group messages, broadcast lists and even bots using this API and fully integrated with your database.
Big hug and until next time!