8

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?
HSV_DO
  • 99
  • 1
  • 1
  • 9

2 Answers2

6

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);
Ramiro
  • 948
  • 2
  • 9
  • 22
  • It's correct this code opens the specified group chat. But I am not able to prefill text into the chat. Is there a way to fill the text too? Tried the same as in the post (`intent.putExtra(Intent.EXTRA_TEXT, "myText")`) but this does not work. – HSV_DO Feb 05 '18 at 19:09
  • I'm afraid that can not be done with the current whatsapp "api". This is the closest you will get to automating a group chat. I understand the people at Facebook trying to prevent whatsapp spam and all, but they should understand that we programmers need a more open api. – Ramiro Feb 05 '18 at 19:51
  • But thanks anyway! If you find a way to do this in the future, it would be great if you share it here. – HSV_DO Feb 05 '18 at 21:29
  • any updates? I am too trying to achieve the same but it only opens up the group and do not send any message. – suv Jul 31 '18 at 12:29
  • In code, how can I find the available groups and who's in each of them? – android developer Jun 29 '22 at 08:01
0

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