Automate WhatsApp messages with a function in a Workflow Rule

Automate WhatsApp messages with a function in a Workflow Rule

In Zoho CRM we can generate functions to execute an API. In addition, these functions can be used together with Workflow Rules to automate actions. In this article, we are going to see how to use Zoho CRM functions and WOZTELL API to send WhatsApp messages. To do this we:
  1. Generate an Access Token
  2. Build the function

Generate an Access Token

The WOZTELL API needs an Access Token to verify the user's identity. Therefore, before starting to use our API we must generate a token. To do this we can use the platform documentation to generate a token.

Remember that the API for sending messages uses the Scopes: bot:sendResponses, bot:admin

Click HERE to go directly to the article to generate the tokens.

Build the function

In Zoho CRM we can create functions for our Workflow Rules by following these steps:

1. Log in to your Zoho CRM account

2. Go to Settings


3. In the section AUTOMATION, click on WORKFLOW RULES



4. Create a new Workflow Rule by clicking on the + Create Rule button:


5. Choose against which module will the rule be linked, type the name of the rule and a description:


6. Once you've defined the conditions of the rule, you will be able to choose which action do you want to be triggered:


Once you create the function, you will be able to add the following code:
// Standard parameters
token = "your-WOZTELL-token";
phone = "your-destination-phone";
channelId = "your-WOZTELL-channel-id";
templateName = "your-whatsapp-template-name";
templateLanguage = "your-whatsapp-language-code";

if(phone.startsWith("00")){
      phone = phone.removeFirstOccurence("00");
}else if(phone.startsWith("+")){
      phone = phone.removeFirstOccurence("+");
}

// Function tag is used in the Zoho CRM logs and Zoho Cliq integration
// It is saved in the Woztell Logs Module and Zoho Cliq message
function_tag = "your-function-tag";

// You can decide if you want to post the message in Zoho Cliq or not.
// Set true to publish or false to not publish it
// post_in_zoho_cliq = true;
// or
// post_in_zoho_cliq = false;
post_in_zoho_cliq = false;

// Body template parameters
// Don't remove bodyParameterList = List();
bodyParameterList = List();
// Comment this code if the template doesn't have parameters
bodyParameter1 = Map();
bodyParameter1.put("type","text");
bodyParameter1.put("text","parameter-1-value");
bodyParameterList.add(bodyParameter1);

// DON'T MODIFY THE FOLLOWING CODE !!
postMessage = Map();
postMessage.put("channelId",channelId);
postMessage.put("recipientId",phone);
responseMap = Map();
responseMap.put("type","TEMPLATE");
responseMap.put("elementName",templateName);
responseMap.put("languageCode",templateLanguage);
componentsList = list();

if(!bodyParameterList.isEmpty())
{
      bodyComponent = Map();
      bodyComponent.put("type","body");
      bodyComponent.put("parameters",bodyParameterList);
      componentsList.add(bodyComponent);
}

responseMap.put("components",componentsList);
responseList = List();
responseList.add(responseMap);
postMessage.put("response",responseList);
message_meta = Map();
message_meta_zoho = Map();
message_meta_zoho.put("user",function_tag);
message_meta_zoho.put("cliq",post_in_zoho_cliq);
// module: Leads or Contacts
message_meta_zoho.put("record",{"id":"XXXXX".toString(),"name": "XXXX","module":"Leads"});
message_meta.put("zoho",message_meta_zoho);
postMessage.put("meta",message_meta);

response = invokeurl
[
      url :PostURL
      type :POST
      parameters:postMessage.toString()
];

info response;

This code allows you to modify it so you can automate the sending. We are explaining now the parts that can be modified:

  1. token = "your-WOZTELL-token";
      As explained at the top of this article, you should generate the token from the Woztell platform.

  1. For the phone variable, you can delete it and add an argument as it is shown here:


To add the argument so it can have a dynamic value depending on each client, we can use # and select the module we want (for example, #Leads or #Contacts). Once the related module has been chosen, we can select which field will we relate to. In this case, we are using the Phone field from the Leads module.



  1. For the channelId variable, we can get it by following the steps in this article. Note that each WhatsApp phone number that is activated on a channel on the Woztell platform will have a different channelId

  1. The variables templateName and templateLanguage are referred to the name of the template we want to send and the language of the same, as each template can be created in different languages. This information can be get from the Woztell platform, on the Channels section by clicking on the Edit button and moving to Platform.
 



  1. The variable function_tag is used so you can identify who has sent the message. For example, you can type the name of the rule so you know which rule is triggering the message. This will provide us with information we will be able to check from Zoho Cliq and also from the "Woztell logs" module on Zoho CRM.

  1. The variable post_in_zoho_cliq is used to decide whether you want the message to be posted on Zoho Cliq or not. If you want that the message gets posted in Zoho Cliq, you can establish it as true. Otherwise, establish it as false.
Bear in mind that if the template DOES NOT contain parameters, you must comment the following lines by adding //
bodyParameter1 = Map();
bodyParameter1.put("type","text");
bodyParameter1.put("text","parameter-1-value");
bodyParameterList.add(bodyParameter1);

On the other hand, if the template contains more than 1 parameter, you must duplicate as many times as parameters you have that section. 
bodyParameter1 = Map();
bodyParameter1.put("type","text");
bodyParameter1.put("text","parameter-1-value");
bodyParameterList.add(bodyParameter1);

bodyParameter2 = Map();
bodyParameter2.put("type","text");
bodyParameter2.put("text","parameter-2-value");
bodyParameterList.add(bodyParameter2);
...

Once all the variables have been configured, you shouldn't modify the rest of the code.

We are presenting an example of the code using a WhatsApp template with a document in the HEADER and a parameter in the BODY:
// Standard parameters
token = "your-WOZTELL-token";
phone = "your-destination-phone";
channelId = "your-WOZTELL-channel-id";
templateName = "template-name";
templateLanguage = "template-language";

if(phone.startsWith("00")){
      phone = phone.removeFirstOccurence("00");
}else if(phone.startsWith("+")){
      phone = phone.removeFirstOccurence("+");
}

// Function tag is used in the Zoho CRM logs and Zoho Cliq integration
// It is saved in the Woztell Logs Module and Zoho Cliq message
function_tag = "automated message sample";

// You can decide if you want to post the message in Zoho Cliq or not.
// Put true to publish or false to not publish
// post_in_zoho_cliq = true;
// or
// post_in_zoho_cliq = false;
post_in_zoho_cliq = true;
//
// Header file
// Don't remove headerParameterList = List();
headerParameterList = List();
// If the template has a file (document, image, video) in the header
// Comment this code if the template doesn't have header or header is text
headerParameter1 = Map();
headerParameter1.put("type","document");
headerParameter1.put("document", {"link":"url of the file"});
headerParameterList.add(headerParameter1);
//
// Body template parameters
// Don't remove bodyParameterList = List();
bodyParameterList = List();
// Comment this code if the template doesn't have parameters
bodyParameter1 = Map();
bodyParameter1.put("type","text");
bodyParameter1.put("text","test Woztell");
bodyParameterList.add(bodyParameter1);

//
//
//
// DON'T MODIFY THE FOLLOWING CODE !!
postMessage = Map();
postMessage.put("channelId",channelId);
postMessage.put("recipientId",phone);
responseMap = Map();
responseMap.put("type","TEMPLATE");
responseMap.put("elementName",templateName);
responseMap.put("languageCode",templateLanguage);
componentsList = list();

if(!headerParameterList.isEmpty())
{
headerComponent = Map();
headerComponent.put("type","header");
headerComponent.put("parameters",headerParameterList);
componentsList.add(headerComponent);
}

if(!bodyParameterList.isEmpty())
{
bodyComponent = Map();
bodyComponent.put("type","body");
bodyComponent.put("parameters",bodyParameterList);
componentsList.add(bodyComponent);
}

responseMap.put("components",componentsList);
responseList = List();
responseList.add(responseMap);
postMessage.put("response",responseList);
message_meta = Map();
message_meta_zoho = Map();
message_meta_zoho.put("user",function_tag);
message_meta_zoho.put("cliq",post_in_zoho_cliq);
// module: Leads or Contacts
message_meta_zoho.put("record",{"id":"XXXXX".toString(),"name": "XXX","module":"Leads"});
message_meta.put("zoho",message_meta_zoho);
postMessage.put("meta",message_meta);

response = invokeurl
[
url :PostURL
type :POST
parameters:postMessage.toString()
];

info response;