Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send text to specific contact programmatically (whatsapp)

I wanted to know how I can send text to a specific whatsapp contact. I found some code to view a specific contact, but not to send data.

Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,     new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",     new String[] { id }, null); c.moveToFirst(); Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));  startActivity(i); c.close(); 

This works fine for viewing a whatsapp-contact, but how can I add some text now? Or didn't the Whatsapp-developer implement such kind of an api?

like image 376
Manuel Allenspach Avatar asked Sep 29 '13 18:09

Manuel Allenspach


People also ask

Can we send WhatsApp message programmatically?

It open particular contact number with particular chat text but not send it, user have to press send button for send message. +1 Up-vote for open particular activity.

How can I send message to WhatsApp API?

Install the WhatsApp Business API Client — Install your API client. Once your client is working, you can update your application settings. Start using the client — Register your phone number with an API call to /account and send a test message with a call to /messages .

How can I send auto message in WhatsApp without adding contact?

With that said, here's how you can send WhatsApp messages to unsaved numbers without adding contact. Open your phone's browser. Now you can copy and paste this link http://wa.me/xxxxxxxxxx, or this link — http://api.whatsapp.com/send?phone=xxxxxxxxxx in the address bar.


2 Answers

I've found the right way to do this:

Source code: phone and message are both String.

    PackageManager packageManager = context.getPackageManager();     Intent i = new Intent(Intent.ACTION_VIEW);      try {         String url = "https://api.whatsapp.com/send?phone="+ phone +"&text=" + URLEncoder.encode(message, "UTF-8");         i.setPackage("com.whatsapp");         i.setData(Uri.parse(url));         if (i.resolveActivity(packageManager) != null) {             context.startActivity(i);         }     } catch (Exception e){         e.printStackTrace();     } 

Enjoy yourself!

like image 57
Crono Avatar answered Sep 23 '22 15:09

Crono


I think the answer is a mix of your question and this answer here: https://stackoverflow.com/a/15931345/734687 So I would try the following code:

  1. change ACTION_VIEW to ACTION_SENDTO
  2. set the Uri as you did
  3. set the package to whatsapp
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("content://com.android.contacts/data/" + c.getString(0))); i.setType("text/plain"); i.setPackage("com.whatsapp");           // so that only Whatsapp reacts and not the chooser i.putExtra(Intent.EXTRA_SUBJECT, "Subject"); i.putExtra(Intent.EXTRA_TEXT, "I'm the body."); startActivity(i); 

I looked into the Whatsapp manifest and saw that ACTION_SEND is registered to the activity ContactPicker, so that will not help you. However ACTION_SENDTO is registered to the activity com.whatsapp.Conversation which sounds more adequate for your problem.

Whatsapp can work as a replacement for sending SMS, so it should work like SMS. When you do not specify the desired application (via setPackage) Android displays the application picker. Thererfor you should just look at the code for sending SMS via intent and then provide the additional package information.

Uri uri = Uri.parse("smsto:" + smsNumber); Intent i = new Intent(Intent.ACTION_SENDTO, uri); i.putExtra("sms_body", smsText);   i.setPackage("com.whatsapp");   startActivity(i); 

First try just to replace the intent ACTION_SEND to ACTION_SENDTO . If this does not work than provide the additional extra sms_body. If this does not work than try to change the uri.

Update I tried to solve this myself and was not able to find a solution. Whatsapp is opening the chat history, but doesn't take the text and send it. It seems that this functionality is just not implemented.

like image 24
ChrLipp Avatar answered Sep 23 '22 15:09

ChrLipp