Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / Win32

Sending WhatsApp Messages from a Win32 C++ Program - Part 1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
3 Nov 2018CPOL2 min read 61.7K   3K   29   13
A simple way for sending WhatsApp messages to an individual or to a group in C++

Introduction

I was looking for a simple API for sending WhatsApp messages to a group of programmers at Secured Globe, Inc. There are several service providers and we have chosen one of them (WhatsAppMate) and started a free trial. However, their code samples for using their services are in almost any programming language except for C++. So we wrote our own C++ class for that purpose. Please read Part 2 as well. 

Background

WhatsApp is a multi-platform free service for chatting via video or voice and for sending messages to individuals or groups, including files, media, etc. WhatsApp is better than the old SMS because it is free and has more features. During our day to day work, we need to set up all sort of alerts to be sent to a group who share a job or work on a feature and when that's done automatically, it makes life easier to be notified.

Using the Code

For the GitHub repo, see this one (the class) and this one (the MFC based tool).

There are several types of messages that can be sent, including individual or group messages and with or without a photo or PDF file attached to them. You can read about it in this page.

Here are the building blocks you need to have. Alternatively, I will later attach a compiled (.exe) test application.

Image 1Image 2

C++
//
#define    GroupAdmin                <YOUR GROUP ADMIN MOBILE PHONE>
#define GroupName                <YOUR GROUP NAME>
#define CLIENT_ID                <YOUR CLIENT ID>
#define CLIENT_SECRET            <YOUR CLIENT SECRET>

#define GROUP_API_SERVER        L"api.whatsmate.net"
#define GROUP_API_PATH          L"/v3/whatsapp/group/text/message/12"
#define IMAGE_SINGLE_API_URL    L"http://api.whatsmate.net/v3/whatsapp/group/image/message/12"

//

Connecting to the Internet

First, we open an Internet connection and connect to the API service:

C++
hOpenHandle = InternetOpen(_T("HTTP"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hOpenHandle == NULL)
{
    return false;
}

hConnectHandle = InternetConnect(hOpenHandle,
    GROUP_API_SERVER,
    INTERNET_DEFAULT_HTTP_PORT,
    NULL, NULL, INTERNET_SERVICE_HTTP,
    0, 1);

if (hConnectHandle == NULL)
{
    InternetCloseHandle(hOpenHandle);
    return false;
}

Opening a Request

C++
const wchar_t *AcceptTypes[] = { _T("application/json"),NULL };
HINTERNET hRequest = HttpOpenRequest(hConnectHandle, _T("POST"),
                     GROUP_API_PATH, NULL, NULL, AcceptTypes, 0, 0);

if (hRequest == NULL)
{
    InternetCloseHandle(hConnectHandle);
    InternetCloseHandle(hOpenHandle);
    return false;
}

The Header

Next, we post the Header which is composed using the following code:

C++
std::wstring HeaderData;

HeaderData += _T("X-WM-CLIENT-ID: ");
HeaderData += _T(CLIENT_ID);
HeaderData += _T("\r\nX-WM-CLIENT-SECRET: ");
HeaderData += _T(CLIENT_SECRET);
HeaderData += _T("\r\n");
HttpAddRequestHeaders(hRequest, HeaderData.c_str(), HeaderData.size(), NULL);

Shooting the Message

Now we are ready to shoot our message to the group:

C++
std::wstring WJsonData;
WJsonData += _T("{");
WJsonData += _T("\"group_admin\":\"");
WJsonData += groupAdmin;
WJsonData += _T("\",");
WJsonData += _T("\"group_name\":\"");
WJsonData += groupName;
WJsonData += _T("\",");
WJsonData += _T("\"message\":\"");
WJsonData += message;
WJsonData += _T("\"");
WJsonData += _T("}");

const std::string JsonData(WJsonData.begin(), WJsonData.end());

bResults = HttpSendRequest(hRequest, NULL, 0, (LPVOID)(JsonData.c_str()), JsonData.size());

Checking the Result

The recipient will see a WhatsApp message like this:

Image 3

But to be sure, at this point we need to know if everything went well. That is done by querying the result of our Http Request.

The StatusText we expect if it worked is "OK".

C++
TCHAR StatusText[BUFFER_LENGTH] = { 0 };
DWORD StatusTextLen = BUFFER_LENGTH;
HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_TEXT, &StatusText, &StatusTextLen, NULL);
bResults = (StatusTextLen && wcscmp(StatusText, L"OK")==FALSE);

Using the SGWhatsApp Class

Now we can see how we use our class.

C++
#include "SGWhatsApp.h"

int _tmain(int argc, _TCHAR* argv[])
{    
    SGWhatsApp sender;
    sender.SendGroupMessage(GroupAdmin, GroupName, _T("hi"));
    return 0;
}

History

  • 3rd November, 2018: Initial version

Thank you

Thanks for Ivan Voloschuk for his help. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Secured Globe, Inc.
United States United States
Michael Haephrati is a music composer, an inventor and an expert specializes in software development and information security, who has built a unique perspective which combines technology and the end user experience. He is the author of a the book Learning C++ , which teaches C++ 20, and was published in August 2022.

He is the CEO of Secured Globe, Inc., and also active at Stack Overflow.

Read our Corporate blog or read my Personal blog.





Comments and Discussions

 
Questionclient id and client secret Pin
Хамза Фахір10-Jun-22 10:15
Хамза Фахір10-Jun-22 10:15 
AnswerRe: client id and client secret Pin
Michael Haephrati12-Jun-22 6:01
professionalMichael Haephrati12-Jun-22 6:01 
Generalsetup code Pin
Хамза Фахір16-Jun-22 5:55
Хамза Фахір16-Jun-22 5:55 
AnswerRe: setup code Pin
Michael Haephrati17-Jun-22 4:11
professionalMichael Haephrati17-Jun-22 4:11 
QuestionRequest Pin
brighton katsuwa16-Jul-21 20:56
brighton katsuwa16-Jul-21 20:56 
Goodmorning sir I would like to ask if your project WhatsMate above apply to new coding project for instance I'm writing my own chat messenger in C++
AnswerRe: Request Pin
Michael Haephrati18-Jul-21 3:06
professionalMichael Haephrati18-Jul-21 3:06 
QuestionWhich client ID and Client Secret? Pin
Member 1408879513-Dec-18 11:44
Member 1408879513-Dec-18 11:44 
AnswerRe: Which client ID and Client Secret? Pin
Member 1418962820-Mar-19 5:04
Member 1418962820-Mar-19 5:04 
QuestionHTTP is not secure Pin
Mohammad Nasim7-Nov-18 18:37
Mohammad Nasim7-Nov-18 18:37 
AnswerRe: HTTP is not secure Pin
rxantos2-Dec-18 17:41
rxantos2-Dec-18 17:41 
GeneralRe: HTTP is not secure Pin
Mohammad Nasim4-Dec-18 11:37
Mohammad Nasim4-Dec-18 11:37 
PraiseVery useful Pin
Member 140427203-Nov-18 8:11
Member 140427203-Nov-18 8:11 
AnswerRe: Very useful Pin
Michael Haephrati4-Nov-18 3:50
professionalMichael Haephrati4-Nov-18 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.