Overview

Zoho Cliq can be extended with bots, message actions, webhooks and connections, all scripted in Deluge from the console (Profile > Bots & Tools). This page is the short path to a working integration: what to use when, the variables each handler gets, and snippets to paste and adapt.

Host depends on your data centre (cliq.zoho.com, .in, .eu, .com.au). Snippets use cliq.zoho.in.

Setup

1. Bots & Tools > Bots > Create     unique name, letters/digits/underscore (no hyphens), e.g. mybuddy
2. Add the bot to the channels it must work in
3. Bots & Tools > Connections       create a Zoho Cliq connection (scopes below), authorise it
4. Bots & Tools > Webhook Tokens    generate a token (max 5 per user), keep it out of git
5. Bot > Edit Handlers              paste the handler code
6. Test in a sandbox channel first

Choosing a Piece

NeedUse
Run something on one specific message with a clickMessage action
Commands by typing (@mybuddy status)Mention handler
A script, CI job or service posts into CliqIncoming webhook handler + webhook token
Greet usersWelcome handler
Read or write threads and chats from DelugeConnection to Cliq
Create a ticket or issue in another serviceMessage action or mention + connection to it
Plain alert, no logiczoho.cliq.postToChannelAsBot(...)
Bot             identity + handlers + incoming URL. Not for "click on one message".
Message action  menu entry on a message. Can start a thread. Cannot post as the bot by itself.
Connection      stored credential Deluge uses for you. Skip it for public APIs or a plain secret header.

Bot Handlers

Console: Bots & Tools > Bots > your bot > Edit Handlers. Every handler returns a Map; an empty Map says nothing. Read the comment at the top of the editor for the exact variable names.

HandlerFires whenVariables
WelcomeUser subscribes or returnsnewuser, user
MentionBot is mentionedmessage, mentions, user, chat
ParticipationA person writes in a chat the bot is inoperation, data, chat
Incoming WebhookHTTP POST to the bot’s incoming URLhttp_method, params, headers, body
Message actionClick on a messagemessage, messages, attachments, user, mentions, chat, location

Mention Command Router

response = Map();
raw = message.toString().trim();       // mention already removed
cmd = raw;
rest = "";
if(raw.contains(" "))
{
	cmd = raw.getPrefix(" ");
	rest = raw.getSuffix(" ");
}
cmd = cmd.toLowerCase();
if(cmd == "ping")
{
	text = "pong";
}
else if(cmd == "status")
{
	text = "All good. Args: " + rest;
}
else
{
	text = "Commands: *ping*, *status <text>*";
}
response.put("text", text);
return response;

Echo Handler (see what you really receive)

response = Map();
out = "message: " + ifnull(message, "-").toString() + "\n" + "user: " + ifnull(user, "-").toString() + "\n" + "chat: " + ifnull(chat, "-").toString();
info out;                                // shows in the execution log
if(out.length() > 3500)
{
	out = out.subString(0, 3500);
}
response.put("text", out);
return response;

Handy Fields

chat.get("chat_type")                    // "channel", "thread", ...
chat.get("channel_unique_name")          // in a channel
chat.get("parent_resource").toString().toMap().get("channel_unique_name")   // in a thread
user.get("id").toString()
user.get("first_name")

Posting From Code

// into a channel as the bot
zoho.cliq.postToChannelAsBot("channel_unique_name", "mybuddy", "text");

// debugging: write to the execution log
info "value: " + someVar;

Webhooks & Tokens

A webhook token (Bots & Tools > Webhook Tokens) authenticates inbound calls as the user who generated it. Treat it like a password: store it outside git (chmod 600 file), rotate by generating a new one.

# Post to the bot's incoming webhook (handler receives the JSON as `body`)
curl -s -X POST "https://cliq.zoho.in/api/v2/bots/mybuddy/incoming?zapikey=$CLIQ_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"title":"Build finished","detail":"main passed in 4m12s"}'
# Same from a script, building safe JSON with jq
jq -n --arg t "$1" --arg d "${2:-}" '{title:$t, detail:$d}' |
curl -s -o /dev/null -w 'HTTP %{http_code}\n' -X POST \
  "https://cliq.zoho.in/api/v2/bots/mybuddy/incoming?zapikey=$CLIQ_KEY" \
  -H 'Content-Type: application/json' --data @-

HTTP 200 means the webhook accepted the call, not that the post worked: check the handler log.

Incoming Webhook Handler (router)

response = Map();
raw = body.toString();
if(!raw.trim().startsWith("{"))
{
	// another service's event: echo it while you learn its shape
	if(raw.length() > 3000)
	{
		raw = raw.subString(0, 3000);
	}
	zoho.cliq.postToChannelAsBot("sandbox_channel", "mybuddy", "Event:\n" + raw);
	return response;
}
payload = body.toMap();
if(payload.get("chat_id") != null)
{
	// post text into a chat or thread as the bot, through the connection
	msg = Map();
	msg.put("text", payload.get("text").toString());
	hdr = Map();
	hdr.put("Content-Type", "application/json");
	r = invokeurl
	[
		url: "https://cliq.zoho.in/api/v2/chats/" + payload.get("chat_id").toString() + "/message?bot_unique_name=mybuddy"
		type: POST
		parameters: msg.toString()
		headers: hdr
		connection: "myconnection"
	];
	if(r.toString().toLowerCase().contains("error"))
	{
		response.put("text", "post failed: " + r.toString());
	}
	return response;
}
if(payload.get("title") != null)
{
	zoho.cliq.postToChannelAsBot("sandbox_channel", "mybuddy", "*" + payload.get("title") + "*\n" + payload.get("detail"));
}
return response;

Calling Your Own Service From Cliq

mh = Map();
mh.put("X-My-Secret", "PASTE_SHARED_SECRET");     // keep real secrets out of git
mh.put("Content-Type", "application/json");
res = invokeurl
[
	url: "https://example.com/hook"
	type: POST
	parameters: payload.toString()
	headers: mh
];
info res;

Connections

A connection is a named credential: put connection: "name" in invokeurl and Zoho adds and refreshes the token. The name is the link name shown in the console.

Kinds         Zoho services (Cliq, Desk, CRM, Creator, Catalyst), prebuilt third-party (GitHub, Google, Slack),
              custom (OAuth 2 client id and secret, or an API key)
Create        Bots & Tools > Connections > create > pick service > name > scopes > authorise
Identity      calls run as the authorising user; use a dedicated account for production
Login user    option "use credentials of login user": each user authorises their own account
Scopes        ZohoCliq.Messages.READ (read threads), ZohoCliq.Webhooks.CREATE (post, start threads)
New scope     reauthorise the connection
// GET through a connection
res = invokeurl
[
	url: "https://cliq.zoho.in/api/v2/chats/" + chatId + "/messages?limit=100"
	type: GET
	connection: "myconnection"
];
info res;
// create a GitHub issue from a message through another connection
issue = Map();
issue.put("title", "From Cliq");
issue.put("body", message.get("text").toString());
hdr = Map();
hdr.put("Content-Type", "application/json");
r = invokeurl
[
	url: "https://api.github.com/repos/OWNER/REPO/issues"
	type: POST
	parameters: issue.toString()
	headers: hdr
	connection: "githubcon"
];
info r;                                  // a 200 with an error body is common: always log

Cliq REST Through a Connection

JobCall
Recent messages of a chat or threadGET /api/v2/chats/{chat_id}/messages?limit=100
One messageGET /api/v2/chats/{chat_id}/messages/{message_id}
The message a thread started fromGET /api/v3/threads/{thread_chat_id}/messages/main
Post to a chat or thread (shows the connection owner)POST /api/v3/chats/{chat_id}/messages {"text":"..."}
Post as the botPOST /api/v2/chats/{chat_id}/message?bot_unique_name={bot} {"text":"..."}
Start a thread under a channel messagePOST /api/v3/channelsbyname/{channel}/messages {"text":"...","thread_message_id":"<id>"}

Useful message keys: content.text, sender.id (bots start with b-), type, thread_information.chat_id, time, id.

Message Actions

Console: Bots & Tools > Message Actions > create > set access > edit code. A generic name (for example “Buddy”) lets you add more actions later. The returned text is shown only to the person who clicked.

response = Map();
if(chat.get("chat_type") != "channel")
{
	response.put("text", "Use this on a channel message.");
	return response;
}
channelName = ifnull(chat.get("channel_unique_name"), "").toString();
chatId = chat.get("id").toString();
msgId = message.get("id").toString();
hdr = Map();
hdr.put("Content-Type", "application/json");

// 1. use the message's thread, or start one under it
threadChat = "";
ti = message.get("thread_information");
if(ti != null)
{
	threadChat = ti.toString().toMap().get("chat_id").toString();
}
else
{
	body = Map();
	body.put("text", "#Noted");
	body.put("thread_message_id", msgId);
	invokeurl
	[
		url: "https://cliq.zoho.in/api/v3/channelsbyname/" + channelName + "/messages"
		type: POST
		parameters: body.toString()
		headers: hdr
		connection: "myconnection"
	];
	// 2. re-read the message to learn the new thread's chat id
	r2 = invokeurl
	[
		url: "https://cliq.zoho.in/api/v2/chats/" + chatId + "/messages/" + msgId
		type: GET
		connection: "myconnection"
	];
	ti2 = r2.toString().toMap().get("thread_information");
	if(ti2 != null)
	{
		threadChat = ti2.toString().toMap().get("chat_id").toString();
	}
}
if(threadChat == "")
{
	response.put("text", "Could not find the thread id.");
	return response;
}
response.put("text", "Done.");
return response;

To post as the bot from a message action, call the bot’s own incoming webhook (the connection call posts as its owner and ignores the bot name):

hook = Map();
hook.put("chat_id", threadChat);
hook.put("text", "#Checking");
hook.put("bot_unique_name", "mybuddy");
hdr = Map();
hdr.put("Content-Type", "application/json");
invokeurl
[
	url: "https://cliq.zoho.in/api/v2/bots/mybuddy/incoming?zapikey=PASTE_WEBHOOK_TOKEN"
	type: POST
	parameters: hook.toString()
	headers: hdr
];

Why a Message Action Instead of a Mention

A mention as the first message of a new thread is never delivered to the bot.
The action works on the message itself, can create the thread, and needs no typing.

Limits

Messages posted by other bots or integrations never reach a handler you can write.
  Use a message action (click) or a scheduled job that reads the channel through a connection.
A bot cannot start a thread: the first message of a thread shows the connection owner.
A message posted as a bot never returns its id: post by chat id through a connection.
A connection call ignores the bot name; the v2 chat endpoint with ?bot_unique_name= honours it.
A channel can hold at most 10 bots.
Access is by channel, not by user: keep an allowed-channels list and check it in the handler.

Troubleshooting

SymptomCauseFix
Variable 'x' is not defined on saveGuessed variable nameUse the names in the editor’s top comment
Webhook returns 200, nothing in Cliq200 only means acceptedCheck the handler log; bot needs channel membership
First mention in a new thread does nothingNot delivered to botsUse a message action
Invalid or expired tokenAuthoriser revoked or changed passwordReauthorise the connection
Permission error on a callScope missing when authorisedAdd the scope, reauthorise
Connection not foundWrong link nameCopy the name from the console
Post shows your name, not the bot’sConnection posts show the owner?bot_unique_name= on the v2 chat endpoint, or postToChannelAsBot
Bot’s own status messages read back as repliesThey look human when posted through a connectionStart them with a marker such as *# and skip those
Ids contain %20Cliq encodes spaces in some idsNormalise ids before storing
Secret leaked into a chatFilled handler pasted as textRotate it, and share filled handlers only by clipboard

Quick Reference

ItemValue
Bot incoming URLhttps://cliq.zoho.in/api/v2/bots/{bot}/incoming?zapikey={token}
Webhook token limit5 per user
Post as bot (Deluge)zoho.cliq.postToChannelAsBot(channel, bot, text)
Use a connectioninvokeurl [ url: ... type: GET connection: "name" ]
Debuginfo value; then read the execution log
Replyresponse = Map(); response.put("text", "..."); return response;
Bot unique nameletters, digits, underscore; no hyphens

See Also