Assign chats in WATI based on Lead owner in Zoho CRM

Jahnavi Prasad Updated by Jahnavi Prasad

Use Case: When a user initiates a conversation, check if they already exist in Zoho. If they are an existing Lead/Contact, find their Lead Owner and assign the chat to them in Wati. If they are a new user, send a welcome message.

This use case requires either the Pro or Business Plan on Wati

Solution:

  1. Set up a chatbot that is launched by default for all user-initiated conversations.
  2. For the contact, if the access token is already stored in the Custom Attributes, use it in Step 3
  3. Make an API call to the Zoho CRM Search Records endpoint. You will pass the user's phone number and the API returns their Lead Owner.
  4. If the API returns the name of the Lead Owner associated with the phone number (Status Code: 200)
    1. Use a combination of 'Set a Condition' and 'Assign Agent' to assign chats to the Lead Owner
  5. If the API returns Status Code:204 it means a lead doesn't exist for this contact
    1. Consider this contact as a new user and continue with your use case
  6. If the API returns Status Code: 401, the access token has expired
    1. Refresh and get the new token using the Refresh Access Token Endpoint
    2. Add this new token to the Custom attributes and go to Step 3

Implementation:

  1. Download this Chatbot Template: Check Zoho Lead Owner and Assign Chat.json
  2. Import it into your WATI Account using the Import JSON button at Automation -> Chatbots
  3. In the First Webhook Node, update your API Endpoint and Access token
  4. Now we generate a Zoho CRM API Token to be used in the Second Webhook. If you already have a token ready, skip to Step 5
    1. If you don't have an Access token to trigger Zoho APIs,
      1. Go to Zoho API console: api-console.zoho.com
      2. Create a Self Client with client_id, client_secret, and code
      3. Substitute these values in the following snippet and make the API call to get an Access Token
        (Postman, Terminal, and Command Prompt can all be used to make the API Call)
        curl --location --request POST 'https://accounts.zoho.com/oauth/v2/token?grant_type=authorization_code&client_id=<Enter Client ID>&client_secret=<Enter CLient Secret>&code=1000.xxxx.xxxx'
      4. The Access Token will be in the format of Zoho-oauthtoken 1000.xxxxxxxxxxx.xxxxxxxxxxxx
  5. In the second webhook Node, add your Zoho CRM Access Token in Customize Headers -> Authorization Field
  6. For Case 1, Status Code: 200, it means the phone number exists in Zoho and we have found the Lead Owner
    1. We can use the 'Set a condition' nodes to check if the LeadOwner value is equal to an Agent Name in WATI, if yes assign the chat to the agent using the 'Assign Agent' Node, else check if the LeadOwner value is equal to a different Agent Name in WATI.
  7. For Case 2, Status Code: 204, the phone number does not exist in Zoho and is a new user. You can send a welcome message here.
  8. For Case 3, Status Code: 401, it means the Access Token has expired. We use another Webhook node here to refresh the access token. We must pass the client_id, client_secret, and refresh_token generated in step 4 to create a new token.
  9. We should also save this new token in a dummy contact '22222222' so it can be retrieved for the next chatbot run

How did we do?

Create a contact in WATI when a new lead comes into ZOHO CRM

Convert first time visitors to Zoho Lead/Contact using Zoho Flows

Contact