Sending message through WhatsApp

前端 未结 23 2508
萌比男神i
萌比男神i 2020-11-22 09:42

Since I found some older posts, that tell that whatsapp doesn\'t support this, I was wondering if something had changed and if there is a way to open a whatsapp \'chat\' wit

23条回答
  •  迷失自我
    2020-11-22 10:24

    The following API can be used in c++ as shown in my article.

    You need to define several constants:

    //
    #define    GroupAdmin                
    #define GroupName                
    #define CLIENT_ID                
    #define 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"
    
    //
    

    Then you connect to the API’s endpoint.

    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;
    }
    

    Then send both header and body and wait for the result that needs to be “OK”.

    Step 1 - open an HTTP request:

    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;
    }
    

    Step 2 - send the header:

    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);
    

    Step 3 - send the message:

    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());
    

    Now just check the result:

    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);
    

提交回复
热议问题