Wednesday, May 20, 2015

How to integrate WhatsApp with an Android app or website — prefill messages using whatsapp://

We can integrate WhatsApp with an Android app in two ways:

Methods

  1. Using the Android Intent system (covered in my previous post: http://karjol-gt.blogspot.in/2015/05/how-to-share-media-or-message-text-with.html).
  2. Using the custom URL scheme provided by WhatsApp.

We will discuss the second option in this post.

WhatsApp custom URL scheme (website or app)

WhatsApp provides a custom URL scheme to interact with the app. If you have a website and want to open a WhatsApp chat with a pre-defined message, you can use WhatsApp’s custom URL scheme.

Opening whatsapp://send?text= followed by the text to send will open WhatsApp, allow the user to choose a contact, and prefill the input field with the specified text.

Example (on a website)

Use a normal anchor tag. The message must be URL-encoded (spaces become %20, etc.). Example:

<a href="whatsapp://send?text=Hello%20World!">Hello, world!</a>

When the user clicks this link, WhatsApp (if installed) opens, the user selects a contact, and the predefined text "Hello World!" appears in the chat input.

Notes and compatibility

  • URL-encode the message text. Unencoded spaces and special characters may break the URL.
  • This method relies on the whatsapp:// custom scheme and therefore requires the WhatsApp app to be installed on the device. Behavior can vary by platform, browser, and WhatsApp version. Test on the target devices and browsers.
  • Consider adding a fallback for environments where the whatsapp:// scheme is unsupported (for example, conditionally offering the WhatsApp FAQ or guiding users to install WhatsApp).

Summary

That’s it. Happy Coding…

Reference

No comments: