Skip to content

Repository files navigation

whatsapp-api-client-cpp

Support links

SupportSupportSupport

Guides & News

GuidesNewsNews

whatsapp-api-client-cpp - library for integration with WhatsApp messenger using the API service green-api.com. You should get a registration token and an account ID in your personal cabinet. There is a free developer account tariff.

API

The documentation for the REST API can be found at the link. The library is a wrapper for the REST API, so the documentation at the link above also applies.

Authorization

To send a message or perform other GREEN API methods, the WhatsApp account in the phone app must be authorized. To authorize the account, go to your cabinet and scan the QR code using the WhatsApp app.

Installation

This library uses C++ 17, supports Linux (GCC) and Windows (Visual Studio 2019/2022) compilers.

Clone library repository

git clone https://github.com/green-api/whatsapp-api-client-cpp.git

Windows

Installing dependencies using vcpkg package manager:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
bootstrap-vcpkg.bat
vcpkg install curl
vcpkg install nlohmann-json
# vcpkg install websocketpp (optional)
vcpkg integrate install

Windows x64

Most Windows machines now have x64 version, but vcpkg uses x86 packages by default.

vcpkg install nlohmann-json:x64-windows
vcpkg install curl:x64-windows

To execute commands in PowerShell it is required to change commands to .\vcpkg

After building bootstrap-vcpkg.bat it is required to add vcpkg.exe into the PATH variables of your systemPATH\vcpkg\installed\x64-windows\bin.

Building the library

Windows

To build the library you need:

Building the library using CMake and Visual Studio compiler

Building is done by using the build.bat scenario or manually by using CMake (compiling Debug version of the library):

mkdir build
cd build
cmake ..
cd ../
cmake --build build

Most likely you will have to specify direct paths to the used libraries, uncomment the lines in CMakeList.txt

Importing the library

You can import the library in Visual Studio by adding the whatsapp-api-client-cpp.lib file in the project dependencies:

  1. Properties->Configuration properties->C/C++->General->Additional Include Directories (PATH/whatsapp-api-client-cpp/include)
  2. Properties->Configuration properties->Linker->General->Additional Library Directories (PATH/build/)
  3. Properties->Configuration properties->Linker->Inpit->Additional Dependencies (whatsapp-api-client-cpp.lib)

Carefully choose your configuration type and install required library version (debug and release)

Linux

Installing dependencies with vcpkg package manager:

  • Dependencies
    • curl
    • nlohmann-json
    • websocketpp (optional)

You can install dependencies with ready-made install.sh scenario (sudo sh install.sh) or manually:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg/vcpkg install curl
./vcpkg/vcpkg install nlohmann-json
# ./vcpkg install websocketpp (optional)
./vcpkg/vcpkg integrate install

You can build the library with ready-made build.sh scenario (sudo sh build.sh) or manually:

mkdir build
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=./vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build build/

You can change vcpkg catalogue by specifying relative or absolute path.

You might need to install additional packages to build the library:

sudo apt-get update
sudo apt-get install cmake
sudo apt-get install g++
sudo apt-get install pkg-config
sudo apt-get install libcurl4-openssl-dev
sudo apt install build-essential

Examples

How to initialize an object

To initialize an object it is required to use apiUrl and mediaUrl parameters specifically for your instance from your personal cabinet, that way you will get the most stable API work and minimal response time.

greenapi::GreenApi instance1101000001{
"https://api.green-api.com",
"https://media.green-api.com",
"1101123456",
"87be9e9532fc49748f2a44b9242e55f2e89f4bf97ed6498f80"
};

Sending a text message example

Link to the example: main.cpp.

nlohmann::json sendMessageJson{
{ "chatId","71234567890@c.us" },
{ "message","I use GREEN-API to send this message to you!" }
};
greenapi::Response sendMessage = instance1101000001.sending.sendMessage(sendMessageJson);
if (sendMessage.error) {
std::cout << "sendMessage error: {status code: " << sendMessage.status_code << ", request time: " << sendMessage.total_time << ", body: " << sendMessage.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tidMessage: " << sendMessage.bodyJson["idMessage"] << "\n" << std::endl;
}

Working with library examples

All examples are put in the examples folder, every file has examples of all the methods with all the supported parameters To run a particular example it is required to copy a code section into your main.cpp file and use your values in the parameters.

Examples list

DescriptionModule
Sending a text examplemain.cpp
Examples of account methodstest_accaunt.cpp
Examples of sending methodstest_sending.cpp
Examples of receiving methodstest_receiving.cpp
Examples of journal methodstest_journals.cpp
Examples of queue methodstest_queues.cpp
Examples of group methodstest_groups.cpp
Examples of status methodstest_statuses.cpp
Examples of read mark methodstest_readMark.cpp
Examples of service methodstest_serviceMethods.cpp

Full list of all methods

API MethodDescriptionDocumentation link
account.getSettingsMethod for getting current settings of the accountGetSettings
account.getWaSettingsMethod for getting information of the WhatsApp accountGetWaSettings
account.setSettingsMethod to set accountSetSettings
account.getStateInstanceMethod for getting the account stateGetStateInstance
account.getStatusInstanceMethod for getting socket connection state of the account with WhatsAppGetStatusInstance
account.rebootMethod for restarting the accountReboot
account.logoutMethod for logging outLogout
account.qrMethod for getting a QR codeQR
account.setProfilePictureMethod for setting a profile pictureSetProfilePicture
account.getAuthorizationCodeMethod for authorizing by phone numberGetAuthorizationCode
account.getStateInstanceHistoryMethod for getting the history of instance state changes in chronological orderGetStateInstanceHistory
device.getDeviceInfoMethod for getting information about the device (phone) on which the WhatsApp Business application is runningGetDeviceInfo
groups.createGroupMethod for creating a group chatCreateGroup
groups.updateGroupNameMethod for changing a group chat nameUpdateGroupName
groups.getGroupDataMethod for getting a group chat dataGetGroupData
groups.addGroupParticipantMethod for adding a participant into a group chatAddGroupParticipant
groups.removeGroupParticipantMethod for deleting a participant from a group chatRemoveGroupParticipant
groups.setGroupAdminMethod for setting a participant as a group administratorSetGroupAdmin
groups.removeAdminMethod for removing administrator rights from a participantRemoveAdmin
groups.setGroupPictureMethod for setting a group pictureSetGroupPicture
groups.leaveGroupMethod for leaving from a groupLeaveGroup
groups.updateGroupSettingsMethod for changing group chat settings (participant permissions). Beta versionUpdateGroupSettings
journals.getChatHistoryMethod for getting a chat historyGetChatHistory
journals.getMessageMethod for getting a messageGetMessage
journals.lastIncomingMessagesMethod for getting last incoming messagesLastIncomingMessages
journals.lastOutgoingMessagesMethod for getting last outgoing messagesLastOutgoingMessages
journals.lastIncomingCallsMethod for getting last incoming calls. Beta versionLastIncomingCalls
journals.lastOutgoingCallsMethod for getting last outgoing callsLastOutgoingCalls
queues.showMessagesQueueMethod for getting messages that are in the outgoing messages queueShowMessagesQueue
queues.clearMessagesQueueMethod for clearing messages from the outgoing message queueClearMessagesQueue
marking.readChatMethod for marking a chat as readReadChat
receiving.receiveNotificationMethod for receiving a notification from the incoming notification queueReceiveNotification
receiving.deleteNotificationMethod for deleting a notification from the incoming notification queueDeleteNotification
receiving.downloadFileMethod for downloading file from outgoing or incoming messageDownloadFile
sending.sendMessageMethod for sending a text message to a personal or group chatSendMessage
sending.sendButtonsMethod for sending buttons to a personal or group chatSendButtons
sending.sendTemplateButtonsMethod for sending a message with interactive buttons from the list of templates in a personal or group chatSendTemplateButtons
sending.sendListMessageMethod for sending a message with a selection button from a list of values to a personal or group chatSendListMessage
sending.sendFileByUploadMethod for sending a file through a form (form-data)SendFileByUpload
sending.sendFileByUrlMethod for sending a file by URLSendFileByUrl
sending.uploadFileMethod for uploading a file to the cloud storage, which can be sent using the sendFileByUrl methodUploadFile
sending.sendLocationMethod for sending a locationSendLocation
sending.sendContactMethod for sending a contactSendContact
sending.sendLinkMethod for sending a message with a link that will add an image preview, title and descriptionSendLink
sending.forwardMessagesMethod for forwarding messages to a personal or group chatForwardMessages
sending.sendPollMethod for sending messages with a poll to a private or group chatSendPoll
sending.sendInteractiveButtonsMethod for sending a message with interactive buttons (types: copy, call, url) to a personal chatSendInteractiveButtons
sending.sendInteractiveButtonsReplyMethod for sending a message with reply buttons to a personal chat. Beta versionSendInteractiveButtonsReply
serviceMethods.checkWhatsappMethod for checking if there is a WhatsApp account on the phone numberCheckWhatsapp
serviceMethods.getAvatarMethod for getting the avatar of the correspondent or group chatGetAvatar
serviceMethods.getContactsMethod for getting contacts of current accountGetContacts
serviceMethods.getContactInfoMethod for getting information of the contactGetContactInfo
serviceMethods.editMessageMethod for editing a message in the chatEditMessage
serviceMethods.deleteMessageMethod for deleting a message in the chatDeleteMessage
serviceMethods.archiveChatMethod for archiving a chatArchiveChat
serviceMethods.unarchiveChatMethod for unarchiving a chatUnarchiveChat
serviceMethods.setDisappearingChatMethod for changing the settings of disappearing messages in chatsSetDisappearingChat
serviceMethods.getChatsMethod for getting the list of chats of the current account in chronological orderGetChats
serviceMethods.sendTypingMethod for showing a "typing" or "recording audio" indicator in the specified chatSendTyping

Service methods documentation

https://green-api.com/docs/api/.

Library dependencies

Licence

Licences under Creative Commons Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0) . LICENSE.

Releases

Contributors

Languages