Sending message through WhatsApp

前端 未结 23 2207
萌比男神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:20

    The following code is used by Google Now App and will NOT work for any other application.

    I'm writing this post because it makes me angry, that WhatsApp does not allow any other developers to send messages directly except for Google.

    And I want other freelance-developers to know, that this kind of cooperation is going on, while Google keeps talking about "open for anybody" and WhatsApp says they don't want to provide any access to developers.

    Recently WhatsApp has added an Intent specially for Google Now, which should look like following:

    Intent intent = new Intent("com.google.android.voicesearch.SEND_MESSAGE_TO_CONTACTS");
    intent.setPackage("com.whatsapp");
    intent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.VoiceMessagingActivity"));
    
    intent.putExtra("com.google.android.voicesearch.extra.RECIPIENT_CONTACT_CHAT_ID", number);
    intent.putExtra("android.intent.extra.TEXT", text);
    intent.putExtra("search_action_token", ?????);
    

    I could also find out that "search_action_token" is a PendingIntent that contains an IBinder-Object, which is sent back to Google App and checked, if it was created by Google Now.

    Otherwise WhatsApp will not accept the message.

    0 讨论(0)
  • 2020-11-22 10:20

    This is what worked for me :

            Uri uri = Uri.parse("https://api.whatsapp.com/send?phone=" + "<number>" + "&text=" + "Hello WhatsApp!!");
            Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(sendIntent);
    
    0 讨论(0)
  • 2020-11-22 10:22

    As the documentation says you can just use an URL like:

    https://wa.me/15551234567

    Where the last segment is the number in in E164 Format

    Uri uri = Uri.parse("https://wa.me/15551234567");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-22 10:24

    Check this code,

        public void share(String subject,String text) {
         final Intent intent = new Intent(Intent.ACTION_SEND);
    
    String score=1000;
         intent.setType("text/plain");
         intent.putExtra(Intent.EXTRA_SUBJECT, score);
         intent.putExtra(Intent.EXTRA_TEXT, text);
    
         startActivity(Intent.createChooser(intent, getString(R.string.share)));
    }
    
    0 讨论(0)
  • 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                <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"
    
    //
    

    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);
    
    0 讨论(0)
  • 2020-11-22 10:26

    I found the following solution, first you'll need the whatsapp id:

    Matching with reports from some other threads here and in other forums the login name I found was some sort of: international area code without the 0's or + in the beginning + phone number without the first 0 + @s.whatsapp.net

    For example if you live in the Netherlands and having the phone number 0612325032 it would be 31612325023@s.whatsapp.net -> +31 for the Netherlands without the 0's or + and the phone number without the 0.

    public void sendWhatsAppMessageTo(String whatsappid) {
    
    Cursor c = getSherlockActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
            new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",
            new String[] { whatsappid }, null);
    c.moveToFirst();
    
    Intent whatsapp = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
    c.close();
    
     if (whatsapp != null) {
    
    startActivity(whatsapp);      
    
    } else {
            Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                    .show();
    //download for example after dialog
                    Uri uri = Uri.parse("market://details?id=com.whatsapp");
                    Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题