Android - WhatsApp Group ID / Open group chat

13.7k views Asked by At

after long Googleing which didn't brought the result I hoped it would, I have two questions about accessing WhatsApp from another Android app.

First of all I want to explain my current development status:

Wrote an app with which you can share some text via WhatsApp. The app is exactly doing what it is supposed to do (as I am completely new to Android development). The first way I found was described in WhatsApp's "FAQ for Android developers". It creates a new intent, prefills the text which should be send and opens the contact picker:

int pos = 0; //0 is just an example value
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
PushAlert pa = pushAlerts.get(pos); //get my text object from ArrayList
sendIntent.setPackage("com.whatsapp"); //directly choose WhatsApp as sharing app
sendIntent.putExtra(Intent.EXTRA_TEXT, "*" + pa.getTitle() + " * \n +" + pa.getContent()); //filling 
sendIntent.setType("text/plain");
startActivity(sendIntent); //Open contact picker

Googled and googled so I found a way (code snippet) to open a specific personal chat and prefill it with the text I want to share:

private void openWhatsAppChat(){
  Intent sendIntent = new Intent("android.intent.action.SEND");
  sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.ContactPicker"));
  sendIntent.setType("text");
  sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("phone number")+"@s.whatsapp.net"); //number without '+' prefix and without '0' after country code
  sendIntent.putExtra(Intent.EXTRA_TEXT,"sample text you want to send along with the image");
  startActivity(sendIntent);
}

So my questions are:

  1. How can I get the WhatsApp ID of a WhatsApp group?
  2. Can I open the group chat and paste my text just with replacing the phone number in method 2 by the group ID? Or is there another way to open and prefill a group chat?
3

There are 3 answers

1
FacebookPeter7x On

i use Tasker to get the group's jid, especially the group that i'm NOT the admin of. you need to do it manually. so if you have 10 groups, you will need to do it 10 times. create a task in tasker, click app, click shortcut, click the magnifying glass, search for the group, click the group, the jid will be shown inside the long string. it should end with %40 so extract the jid manually

0
Hafiz On

To get information about the groups I belong to (or even left) I use an unofficial solution. I needed a set of features to automate groups in Whatsapp, so this solution suited me. Specifically to get the id of the groups I use the "Get groups" method This is roughly how it looks like.


    
    import requests
    
    url = "https://gate.whapi.cloud/groups?token=your_token"
    
    headers = {"accept": "application/json"}
    
    response = requests.get(url, headers=headers)
    
    print(response.text)
    

In response comes the group name, group id, creation date, last message, all members, icon and many other things. In fact, you can test it yourself here https://whapi.readme.io/reference/getgroups , but you will need a token in the service.

And then you can work with this information in any way you want. I have a counting of participants and distribution system + creation of new groups automatically. There are a lot of methods for automation, you can see here: https://whapi.cloud/docs#/Groups/getGroups

5
Ramiro On

You have to use group link.
When the user install your app, you should ask them to copy the group link from the whatsapp group info, then you store it to access that group directly from your app.
This link is only visible by groups admins, so if the user isn't admin, you should instruct them to ask the link from the admin.
Although this link was intended by whatsapp for invitation to groups, it makes the job of opening the desired group chat.

Intent intentWhatsapp = new Intent(Intent.ACTION_VIEW);
String url = "https://chat.whatsapp.com/<group_link>";
intentWhatsapp.setData(Uri.parse(url));
intentWhatsapp.setPackage("com.whatsapp");
startActivity(intentWhatsapp);