WhatsApp Salesforce Integration - The Code

WhatsApp Salesforce Integration - The Code

In the previous post we saw the steps to register a phone number with WhatsApp using yowsup-cli and get the password. In this article I will quickly share the Salesforce code and Flask webapp that I have used for the demo video shared in the last post.

Visualforce Page

<apex:page standardController="Contact" extensions="WhatsAppController" tabStyle="Contact">

        position: absolute;
        top: 50%;
        padding: 20px;
        left: 50%;
        background: #51a284;
        border-radius: 5px;
        color: white;
  <apex:form >
      <apex:actionStatus id="wait" startText="Sending Message. Please Wait..." startStyleClass="startStyle">
      <apex:sectionHeader title="Send WhatsApp Message to {!Contact.Name}"/>
      <apex:pageBlock title="Write your message below" id="msgBlock">
          <apex:pageBlockSection columns="1">
              <apex:inputTextarea rows="10" cols="50" label="Message" value="{!message}"/>
              <apex:outputText rendered="{!showStatus}"><span style="color:#51a284">{!statusMessage}</span> </apex:outputText>
          <apex:pageBlockButtons >
              <apex:commandButton action="{!sendMessage}" value="Send" status="wait" rerender="msgBlock"/>

Apex Code

public class WhatsAppController{

    public String message{get;set;}
    public Contact thisContact{get;set;}
    public Boolean showStatus{get;set;}
    public String statusMessage{get;set;}

    public WhatsAppController(ApexPages.StandardController controller){
        controller.addFields(new List<String>{'Phone'});
        this.thisContact = (Contact)controller.getRecord();
        showStatus = false;
        message='Hello '+thisContact.Name +',\n';
    public void sendMessage(){
        if(message == null || message == ''){
            statusMessage ='Please enter some message';
            showStatus = true;
            Http http = new Http();
            HttpRequest req = new HttpRequest();
            HttpResponse res;
            res = http.send(req);
            message ='';
            statusMessage = 'Message Sent';
            showStatus = true;
            System.debug('\n\n---------- Response:' + res.getBody() + '\n\n');

Note: Configure the Openshift URL in the Remote Site Settings and create a custom button/link with the above page that can be used on Contact page layout

Flask Web App

Below is the Github repository where I have uploaded the Flask web app; that includes the latest WhatsApp SDK details and the corresponding MD5 classes key that will generate a valid token for registration. You can find those details under yowsup/env/env_android.py. You will encounter old_version or bad_token error if the configuration details in this file are incorrect or do not correspond to the correct SDK version.

Alternatively, open this link http://phpfiddle.org/ and run the below PHP code after replacing your phone number. The resulting string at the end is the token. Return this string directly in the getToken function in /yowsup/env/env_android.py and then try registering.

$waPrefix     = "Y29tLndoYXRzYXBw";
$signature    = "MIIDMjCCAvCgAwIBAgIETCU2pDALBgcqhkjOOAQDBQAwfDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFDASBgNVBAcTC1NhbnRhIENsYXJhMRYwFAYDVQQKEw1XaGF0c0FwcCBJbmMuMRQwEgYDVQQLEwtFbmdpbmVlcmluZzEUMBIGA1UEAxMLQnJpYW4gQWN0b24wHhcNMTAwNjI1MjMwNzE2WhcNNDQwMjE1MjMwNzE2WjB8MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEUMBIGA1UEBxMLU2FudGEgQ2xhcmExFjAUBgNVBAoTDVdoYXRzQXBwIEluYy4xFDASBgNVBAsTC0VuZ2luZWVyaW5nMRQwEgYDVQQDEwtCcmlhbiBBY3RvbjCCAbgwggEsBgcqhkjOOAQBMIIBHwKBgQD9f1OBHXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR+1k9jVj6v8X1ujD2y5tVbNeBO4AdNG/yZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb+DtX58aophUPBPuD9tPFHsMCNVQTWhaRMvZ1864rYdcq7/IiAxmd0UgBxwIVAJdgUI8VIwvMspK5gqLrhAvwWBz1AoGBAPfhoIXWmz3ey7yrXDa4V7l5lK+7+jrqgvlXTAs9B4JnUVlXjrrUWU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyNKOCjrh4rs6Z1kW6jfwv6ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtVJWQBTDv+z0kqA4GFAAKBgQDRGYtLgWh7zyRtQainJfCpiaUbzjJuhMgo4fVWZIvXHaSHBU1t5w//S0lDK2hiqkj8KpMWGywVov9eZxZy37V26dEqr/c2m5qZ0E+ynSu7sqUD7kGx/zeIcGT0H+KAVgkGNQCo5Uc0koLRWYHNtYoIvt5R3X6YZylbPftF/8ayWTALBgcqhkjOOAQDBQADLwAwLAIUAKYCp0d6z4QQdyN74JDfQ2WCyi8CFDUM4CaNB+ceVXdKtOrNTQcc0e+t";
$classesMd5   = "14w/wF67XBf2vTc+qALwKQ=="; // 2.16.148
$k            = "PkTwKSZqUfAUyR0rPQ8hYJ0wNsQQ3dW1+3SCnyTXIfEAxxS75FwkDf47wNv/c8pP3p0GXKR6OOQmhyERwx74fw1RYSU10I4r1gyBVDbRJ40pidjM41G1I1oN";
$KEY          = "The piano has been drinking";

$f =  'dsafsadfasdfasdfafasfds';//file_get_contents("magic.dat");
$count = 0;
for ($i=0; $i < strlen($f); $i++) {
	$f[$i] = $f[$i] ^ $KEY[$count++];
	if ($count == strlen($KEY) -1) {
		$count = 0;
$d = base64_decode($waPrefix) . $f;
$key2 = base64_decode('eQV5aq/Cg63Gsq1sshN9T3gh+UUp0wIw0xgHYT1bnCjEqOJQKCRrWxdAe2yvsDeCJL+Y4G3PRD2HUF7oUgiGo8vGlNJOaux26k+A2F3hj8A=');
$data = base64_decode($signature) . base64_decode($classesMd5) . '<PHONE_NUMBER_WITHOUT_COUNTRY_CODE>';
$opad = str_repeat(chr(0x5C), 64);
$ipad = str_repeat(chr(0x36), 64);
for ($i = 0; $i < 64; $i++) {
	$opad[$i] = $opad[$i] ^ $key2[$i];
	$ipad[$i] = $ipad[$i] ^ $key2[$i];
$output = hash("sha1", $opad . hash("sha1", $ipad . $data, true), true);

Github Repository: https://github.com/murali2805/inteygrate_flaskapp

Below is the summary of the steps that needs to be followed for the integration:

  1. Clone / Download the repository to your desktop.
  2. Follow the Whatsapp Salesforce Integration article for Registration using youwsup-cli and get the Password.
  3. Modify the flaskapp.py to replace the Expected token, Phone number and Password.
  4. Upload the repository to Openshift.
  5. Get the Openshift URL and use the same in your Salesforce Apex code.

Please let me know in the comments section if you face any issue with the repository code and I would be happy to help you.



At inteygrate.com, I am trying out ways to integrate Salesforce to various other technologies.

View Comments
Next Post

Salesforce Integrations using Mulesoft

Previous Post

WhatsApp Salesforce Integration