在Zoho CRM中使用函数自动化WhatsApp消息

在工作流规则中使用函数自动化WhatsApp消息

Warning
在开始编写代码之前,我们建议您使用以下方法:  https://support.woztell.com/portal/en/kb/articles/whatsapp-workflows-inside-zoho-crm


在Zoho CRM中,我们可以生成函数来执行API。此外,这些函数可以与工作流规则一起使用,以实现自动化。在本文中,我们将展示如何使用Zoho CRM函数和 WOZTELL API 来发送WhatsApp消息。为此,我们需要:
  1. 生成访问令牌
  2. 构建函数

生成访问令牌

WOZTELL API需要一个访问令牌来验证用户的身份。因此,在开始使用我们的API之前,我们必须生成一个令牌。为此,我们可以使用平台文档来生成令牌。

警告
请记住, 发送消息的API 使用的范围是:bot:sendResponses, bot:admin

点击 这里 直接进入生成令牌的文章。

构建函数

在Zoho CRM中,我们可以创建函数来执行工作流规则。以下是创建这些函数的步骤:

1. 登录您的Zoho CRM帐户

2. 转到设置


3. 在“自动化”部分,点击“工作流规则”(Workflow Rules)




4. 创建一个新的工作流规则,通过点击 + 创建规则 按钮:


5. 选择与规则关联的模块,输入规则名称及其描述:


6. 一旦你定义了规则的条件,就可以选择要触发的操作:


创建函数后,您将能够添加以下代码:
备注
// 标准参数
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("+");
}

// 功能标签用于Zoho CRM日志和Zoho Cliq集成
// 它保存在Woztell日志模块和Zoho Cliq消息中
function_tag = "your-function-tag";

// 您可以决定是否希望在Zoho Cliq中发布消息。
// 设置true发布或false不发布
// post_in_zoho_cliq = true;
// 或者
// post_in_zoho_cliq = false;
post_in_zoho_cliq = false;

// 请求体模板参数
// 不要删除 bodyParameterList = List();
bodyParameterList = List();
// 如果模板没有参数,请注释此代码
bodyParameter1 = Map();
bodyParameter1.put("type","text");
bodyParameter1.put("text","parameter-1-value");
bodyParameterList.add(bodyParameter1);

// 不要修改以下代码 !!
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);
// 模块:Leads 或 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;

此代码允许您进行修改,以便自动化发送。现在我们将解释可以修改的部分:

  1. token = "your-WOZTELL-token";
      正如本文顶部所述,您应该从Woztell平台生成令牌。

  1. 对于 phone 变量,您可以删除它并添加一个参数,如此处所示:


为了添加参数,使其根据每个客户的不同而具有动态值,我们可以使用#并选择我们想要的模块(例如,#Leads或#Contacts)。一旦选择了相关的模块,我们可以选择要关联的字段。在这种情况下,我们使用来自Leads模块的 Phone 字段。



  1. 对于 channelId 变量,我们可以通过遵循这文章来找到它。请注意,每个在Woztell平台上的WhatsApp通道激活的电话号码都有一个不同的 channelId

  1. 变量 templateName 和 templateLanguage 是指我们要发送的模板名称及其语言,因为每个模板可以用不同的语言创建。此信息可以从Woztell平台获取,通过点击频道部分的Edit按钮并转到Platform
 



  1. 变量 function_tag 用于让您识别是谁发送了消息。例如,您可以输入规则名称,以便知道是哪条规则触发了消息。这将为我们提供可以在Zoho Cliq和Zoho CRM的“Woztell日志”模块中检查的信息。

  1. 变量 post_in_zoho_cliq 用于决定是否希望在Zoho Cliq中发布消息。如果希望将消息发布到Zoho Cliq,您可以将其设置为 true。否则,设置为 false
请记住,如果模板不包含参数,您需要通过添加//来注释掉以下行:
备注
bodyParameter1 = Map();
bodyParameter1.put("type","text");
bodyParameter1.put("text","parameter-1-value");
bodyParameterList.add(bodyParameter1);

另一方面,如果模板包含多个参数,则必须根据参数的数量重复该部分。 
备注
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);
...

一旦您配置了所有变量,您就不应修改其余代码。

以下是使用WhatsApp模板的代码示例,其中头部包含文件,主体包含参数:
信息
// 标准参数
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("+");
}

// 功能标签用于Zoho CRM日志和Zoho Cliq集成
// 它保存在Woztell日志模块和Zoho Cliq消息中
function_tag = "automated message sample";

// 您可以决定是否希望在Zoho Cliq中发布消息。
// 设置true发布或false不发布
// post_in_zoho_cliq = true;
// 或者
// post_in_zoho_cliq = false;
post_in_zoho_cliq = true;
//
// 标头文件
// 不要删除 headerParameterList = List();
headerParameterList = List();
// 如果模板包含文件(文档、图片、视频)作为标头
// 如果模板没有标头或标头是文本,请注释此代码
headerParameter1 = Map();
headerParameter1.put("type","document");
headerParameter1.put("document", {"link":"url of the file"});
headerParameterList.add(headerParameter1);
//
// 请求体模板参数
// 不要删除 bodyParameterList = List();
bodyParameterList = List();
// 如果模板没有参数,请注释此代码
bodyParameter1 = Map();
bodyParameter1.put("type","text");
bodyParameter1.put("text","test Woztell");
bodyParameterList.add(bodyParameter1);

//
//
//
// 不要修改以下代码 !!
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);
// 模块:Leads 或 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;