Skip to main content

Automatically assign chats in Wati based on Lead Owner in Zoho CRM

Summary

Want to automatically assign incoming chats to the right sales owner in Wati? This guide shows you how to check if a user already exists in Zoho CRM and assign their chat to the correct Lead Owner. If the user is new, you can send them a welcome message instead. This setup helps ensure faster responses and better customer experience.

Note: This feature requires either the Pro or Business plan on Wati.

Instructions

Overview of the solution

You’ll set up a chatbot in Wati that does the following when a user starts a conversation:

  • Checks if the user exists in Zoho CRM using their phone number.

  • If the user exists:

    • Retrieves the Lead Owner from Zoho.

    • Assigns the chat to that Lead Owner in Wati.

  • If the user doesn’t exist:

    • Sends a welcome message.

  • If the Zoho API token has expired:

    • Refreshes the token and retries.

How to set up the Chatbot & assign conversations

1. Default Chatbot Setup

  • Configure the chatbot to automatically launch whenever a user starts a new conversation.

2. Check for Stored Access Token

  • The system checks if an access token (needed for API calls) is already saved in the contact’s Custom Attributes.

    • If the token exists → Use it in Step 3 (API Call).

    • If not → Proceed to get a new token (explained later).

3. Search for Lead in Zoho CRM

  • Make an API call to Zoho CRM’s "Search Records" endpoint.

  • Pass the user’s phone number to check if they are an existing lead.

4. Handle API Responses

Status Code

What It Means

Action to Take

200 (Success)

A lead exists, and the response includes the Lead Owner’s name.

Use "Set a Condition" + "Assign Agent" to route the chat to the Lead Owner.

204 (No Content)

No lead exists for this phone number (new user).

Treat them as a new lead and proceed with your default chatbot flow.

401 (Unauthorized)

The access token has expired.

Steps to set this up Chatbot

1. Download the Chatbot Template

Download the ready-to-use chatbot template: Check Zoho Lead Owner and Assign Chat.json

This template contains the required flows to connect Wati with Zoho CRM.

2. Import the Template into Wati

  • Go to Automation > Chatbots in your Wati account.

  • Click Import JSON.

  • Upload the downloaded chatbot template.

3. Update Your Zoho API Details

In the first webhook node of your chatbot flow:

Update your Zoho API Endpoint.

Add your Access Token.

4. Generate a Zoho CRM Access Token (If Needed)

If you already have an access token for Zoho CRM, skip to Step 5.

To generate a new Access Token:

  • Go to the Zoho API Console: api-console.zoho.com

  • Create a Self Client to get your:

    • client_id

    • client_secret

    • code

  • Use the following command to generate your access token:

curl --location --request POST 'https://accounts.zoho.com/oauth/v2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'client_id=<Enter Client ID>' \
--data-urlencode 'client_secret=<Enter Client Secret>' \
--data-urlencode 'code=<Enter Code>'

Your token will look like this:Zoho-oauthtoken 1000.xxxxxxxxxxx.xxxxxxxxxxxx

5. Add the Access Token to the Second Webhook Node

In the second webhook node:

  • Go to Customize Headers.

  • Add your Zoho CRM Access Token in the Authorization field.

6. Understand How the Chatbot Logic Works

The chatbot uses Zoho’s API to search for a user based on their phone number.

Case 1: User Exists in Zoho (Status Code: 200)

  • The API will return the Lead Owner.

  • Use the Set a Condition node to check if the Lead Owner matches an agent name in Wati.

  • If it matches, use the Assign Agent node to assign the chat to that agent.

Case 2: User Does Not Exist in Zoho (Status Code: 204)

  • Treat this user as new.

  • Send them a welcome message.

Case 3: Access Token Expired (Status Code: 401)

  • Use another webhook node to refresh the token.

  • Make an API call with your client_id, client_secret, and refresh_token:

curl --location --request POST 'https://accounts.zoho.com/oauth/v2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=refresh_token' \
--data-urlencode 'client_id=<Enter Client ID>' \
--data-urlencode 'client_secret=<Enter Client Secret>' \
--data-urlencode 'refresh_token=<Enter Refresh Token>'
  • Save the new token in a dummy contact (for example, 22222222) in Wati.

  • The chatbot will use this token for future runs.

Conclusion

This setup helps you:

  • Automatically assign chats to the right sales owner.

  • Provide a better customer experience with faster responses.

  • Handle new users smoothly with a welcome message.

  • Keep your Zoho token management automated.

Frequently Asked Questions (FAQs)

General

1. What does this chatbot setup do?

The chatbot checks whether a user exists in Zoho CRM using the user's phone number. If the user exists, it retrieves the Lead Owner and assigns the chat to the corresponding agent in Wati. If the user does not exist, it sends a welcome message. If the Zoho CRM access token has expired, it refreshes the token and retries the API call.

2. Which Wati plans support this feature?

This feature requires either the Pro or Business plan on Wati.

Chatbot and Zoho CRM Integration

3. How does the chatbot check whether a user exists in Zoho CRM?

The chatbot makes an API call to Zoho CRM's Search Records endpoint and uses the user's phone number to search for an existing lead.

4. What happens when the Zoho CRM API returns status code 200?

A status code 200 means that a lead exists and the response includes the Lead Owner's name. The chatbot uses Set a Condition to check whether the Lead Owner matches an agent name in Wati. If it matches, the Assign Agent node assigns the chat to that agent.

5. What happens when the Zoho CRM API returns status code 204?

A status code 204 means that no lead exists for the user's phone number. The chatbot treats the user as a new lead and proceeds with the default chatbot flow, including sending a welcome message.

6. What happens when the Zoho CRM API returns status code 401?

A status code 401 means that the Zoho CRM access token has expired. The chatbot uses the Refresh Access Token Endpoint to obtain a new token, updates the token in Custom Attributes, and retries the Zoho CRM API call with the fresh token.

Access Token Configuration

7. How do I generate a Zoho CRM access token for the chatbot?

Go to the Zoho API Console at api-console.zoho.com and create a Self Client to obtain the client_id, client_secret, and code. Then use Zoho's OAuth token endpoint with grant_type=authorization_code, the client ID, client secret, and code to generate an access token.

8. Where do I add the Zoho CRM access token in the chatbot?

In the first webhook node, update the Zoho API endpoint and add the access token. In the second webhook node, go to Customize Headers and add the Zoho CRM access token in the Authorization field.

9. How does the chatbot refresh and store an expired Zoho CRM access token?

When the Zoho CRM API returns status code 401, the chatbot uses another webhook node to refresh the token using the client_id, client_secret, and refresh_token. The new token is saved in a dummy contact in Wati, such as 22222222, and is used for future chatbot runs.

Did this answer your question?