<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Bot · QuickRef</title><link>https://qref.eknath.dev/tags/bot/</link><description>Bot updates from QuickRef</description><language>en-us</language><atom:link href="https://qref.eknath.dev/tags/bot/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Sat, 05 Dec 2026 00:00:00 +0000</lastBuildDate><item><title>Zoho Cliq Bots</title><link>https://qref.eknath.dev/tools/zoho-cliq-bots/</link><guid isPermaLink="true">https://qref.eknath.dev/tools/zoho-cliq-bots/</guid><pubDate>Sat, 05 Dec 2026 00:00:00 +0000</pubDate><category>zoho</category><category>cliq</category><category>bot</category><category>deluge</category><category>webhook</category><category>integration</category><description>Zoho Cliq bot handlers, incoming webhooks and tokens, message actions and Deluge connections, with copy-paste snippets for quick integrations.</description><content:encoded><![CDATA[<h2 id="overview">Overview</h2>
<p>Zoho Cliq can be extended with bots, message actions, webhooks and connections, all scripted in Deluge from the console (Profile &gt; Bots &amp; 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.</p>
<p>Host depends on your data centre (<code>cliq.zoho.com</code>, <code>.in</code>, <code>.eu</code>, <code>.com.au</code>). Snippets use <code>cliq.zoho.in</code>.</p>
<h2 id="setup">Setup</h2>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>1. Bots &amp; Tools &gt; Bots &gt; Create     unique name, letters/digits/underscore (no hyphens), e.g. mybuddy
</span></span><span style="display:flex;"><span>2. Add the bot to the channels it must work in
</span></span><span style="display:flex;"><span>3. Bots &amp; Tools &gt; Connections       create a Zoho Cliq connection (scopes below), authorise it
</span></span><span style="display:flex;"><span>4. Bots &amp; Tools &gt; Webhook Tokens    generate a token (max 5 per user), keep it out of git
</span></span><span style="display:flex;"><span>5. Bot &gt; Edit Handlers              paste the handler code
</span></span><span style="display:flex;"><span>6. Test in a sandbox channel first
</span></span></code></pre></div><h2 id="choosing-a-piece">Choosing a Piece</h2>
<table>
  <thead>
      <tr>
          <th>Need</th>
          <th>Use</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Run something on one specific message with a click</td>
          <td>Message action</td>
      </tr>
      <tr>
          <td>Commands by typing (<code>@mybuddy status</code>)</td>
          <td>Mention handler</td>
      </tr>
      <tr>
          <td>A script, CI job or service posts into Cliq</td>
          <td>Incoming webhook handler + webhook token</td>
      </tr>
      <tr>
          <td>Greet users</td>
          <td>Welcome handler</td>
      </tr>
      <tr>
          <td>Read or write threads and chats from Deluge</td>
          <td>Connection to Cliq</td>
      </tr>
      <tr>
          <td>Create a ticket or issue in another service</td>
          <td>Message action or mention + connection to it</td>
      </tr>
      <tr>
          <td>Plain alert, no logic</td>
          <td><code>zoho.cliq.postToChannelAsBot(...)</code></td>
      </tr>
  </tbody>
</table>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>Bot             identity + handlers + incoming URL. Not for &#34;click on one message&#34;.
</span></span><span style="display:flex;"><span>Message action  menu entry on a message. Can start a thread. Cannot post as the bot by itself.
</span></span><span style="display:flex;"><span>Connection      stored credential Deluge uses for you. Skip it for public APIs or a plain secret header.
</span></span></code></pre></div><h2 id="bot-handlers">Bot Handlers</h2>
<p>Console: Bots &amp; Tools &gt; Bots &gt; your bot &gt; 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.</p>
<table>
  <thead>
      <tr>
          <th>Handler</th>
          <th>Fires when</th>
          <th>Variables</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Welcome</td>
          <td>User subscribes or returns</td>
          <td><code>newuser</code>, <code>user</code></td>
      </tr>
      <tr>
          <td>Mention</td>
          <td>Bot is mentioned</td>
          <td><code>message</code>, <code>mentions</code>, <code>user</code>, <code>chat</code></td>
      </tr>
      <tr>
          <td>Participation</td>
          <td>A person writes in a chat the bot is in</td>
          <td><code>operation</code>, <code>data</code>, <code>chat</code></td>
      </tr>
      <tr>
          <td>Incoming Webhook</td>
          <td>HTTP POST to the bot&rsquo;s incoming URL</td>
          <td><code>http_method</code>, <code>params</code>, <code>headers</code>, <code>body</code></td>
      </tr>
      <tr>
          <td>Message action</td>
          <td>Click on a message</td>
          <td><code>message</code>, <code>messages</code>, <code>attachments</code>, <code>user</code>, <code>mentions</code>, <code>chat</code>, <code>location</code></td>
      </tr>
  </tbody>
</table>
<h3 id="mention-command-router">Mention Command Router</h3>
<pre tabindex="0"><code class="language-deluge" data-lang="deluge">response = Map();
raw = message.toString().trim();       // mention already removed
cmd = raw;
rest = &#34;&#34;;
if(raw.contains(&#34; &#34;))
{
	cmd = raw.getPrefix(&#34; &#34;);
	rest = raw.getSuffix(&#34; &#34;);
}
cmd = cmd.toLowerCase();
if(cmd == &#34;ping&#34;)
{
	text = &#34;pong&#34;;
}
else if(cmd == &#34;status&#34;)
{
	text = &#34;All good. Args: &#34; + rest;
}
else
{
	text = &#34;Commands: *ping*, *status &lt;text&gt;*&#34;;
}
response.put(&#34;text&#34;, text);
return response;
</code></pre><h3 id="echo-handler-see-what-you-really-receive">Echo Handler (see what you really receive)</h3>
<pre tabindex="0"><code class="language-deluge" data-lang="deluge">response = Map();
out = &#34;message: &#34; + ifnull(message, &#34;-&#34;).toString() + &#34;\n&#34; + &#34;user: &#34; + ifnull(user, &#34;-&#34;).toString() + &#34;\n&#34; + &#34;chat: &#34; + ifnull(chat, &#34;-&#34;).toString();
info out;                                // shows in the execution log
if(out.length() &gt; 3500)
{
	out = out.subString(0, 3500);
}
response.put(&#34;text&#34;, out);
return response;
</code></pre><h3 id="handy-fields">Handy Fields</h3>
<pre tabindex="0"><code class="language-deluge" data-lang="deluge">chat.get(&#34;chat_type&#34;)                    // &#34;channel&#34;, &#34;thread&#34;, ...
chat.get(&#34;channel_unique_name&#34;)          // in a channel
chat.get(&#34;parent_resource&#34;).toString().toMap().get(&#34;channel_unique_name&#34;)   // in a thread
user.get(&#34;id&#34;).toString()
user.get(&#34;first_name&#34;)
</code></pre><h3 id="posting-from-code">Posting From Code</h3>
<pre tabindex="0"><code class="language-deluge" data-lang="deluge">// into a channel as the bot
zoho.cliq.postToChannelAsBot(&#34;channel_unique_name&#34;, &#34;mybuddy&#34;, &#34;text&#34;);

// debugging: write to the execution log
info &#34;value: &#34; + someVar;
</code></pre><h2 id="webhooks--tokens">Webhooks &amp; Tokens</h2>
<p>A webhook token (Bots &amp; Tools &gt; Webhook Tokens) authenticates inbound calls as the user who generated it. Treat it like a password: store it outside git (<code>chmod 600</code> file), rotate by generating a new one.</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Post to the bot&#39;s incoming webhook (handler receives the JSON as `body`)</span>
</span></span><span style="display:flex;"><span>curl -s -X POST <span style="color:#a5d6ff">&#34;https://cliq.zoho.in/api/v2/bots/mybuddy/incoming?zapikey=</span><span style="color:#79c0ff">$CLIQ_KEY</span><span style="color:#a5d6ff">&#34;</span> <span style="color:#79c0ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#79c0ff"></span>  -H <span style="color:#a5d6ff">&#39;Content-Type: application/json&#39;</span> <span style="color:#79c0ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#79c0ff"></span>  -d <span style="color:#a5d6ff">&#39;{&#34;title&#34;:&#34;Build finished&#34;,&#34;detail&#34;:&#34;main passed in 4m12s&#34;}&#39;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Same from a script, building safe JSON with jq</span>
</span></span><span style="display:flex;"><span>jq -n --arg t <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$1</span><span style="color:#a5d6ff">&#34;</span> --arg d <span style="color:#a5d6ff">&#34;</span><span style="color:#a5d6ff">${</span><span style="color:#79c0ff">2</span><span style="color:#ff7b72">:-</span><span style="color:#a5d6ff">}</span><span style="color:#a5d6ff">&#34;</span> <span style="color:#a5d6ff">&#39;{title:$t, detail:$d}&#39;</span> |
</span></span><span style="display:flex;"><span>curl -s -o /dev/null -w <span style="color:#a5d6ff">&#39;HTTP %{http_code}\n&#39;</span> -X POST <span style="color:#79c0ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#79c0ff"></span>  <span style="color:#a5d6ff">&#34;https://cliq.zoho.in/api/v2/bots/mybuddy/incoming?zapikey=</span><span style="color:#79c0ff">$CLIQ_KEY</span><span style="color:#a5d6ff">&#34;</span> <span style="color:#79c0ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#79c0ff"></span>  -H <span style="color:#a5d6ff">&#39;Content-Type: application/json&#39;</span> --data @-
</span></span></code></pre></div><p>HTTP 200 means the webhook accepted the call, not that the post worked: check the handler log.</p>
<h3 id="incoming-webhook-handler-router">Incoming Webhook Handler (router)</h3>
<pre tabindex="0"><code class="language-deluge" data-lang="deluge">response = Map();
raw = body.toString();
if(!raw.trim().startsWith(&#34;{&#34;))
{
	// another service&#39;s event: echo it while you learn its shape
	if(raw.length() &gt; 3000)
	{
		raw = raw.subString(0, 3000);
	}
	zoho.cliq.postToChannelAsBot(&#34;sandbox_channel&#34;, &#34;mybuddy&#34;, &#34;Event:\n&#34; + raw);
	return response;
}
payload = body.toMap();
if(payload.get(&#34;chat_id&#34;) != null)
{
	// post text into a chat or thread as the bot, through the connection
	msg = Map();
	msg.put(&#34;text&#34;, payload.get(&#34;text&#34;).toString());
	hdr = Map();
	hdr.put(&#34;Content-Type&#34;, &#34;application/json&#34;);
	r = invokeurl
	[
		url: &#34;https://cliq.zoho.in/api/v2/chats/&#34; + payload.get(&#34;chat_id&#34;).toString() + &#34;/message?bot_unique_name=mybuddy&#34;
		type: POST
		parameters: msg.toString()
		headers: hdr
		connection: &#34;myconnection&#34;
	];
	if(r.toString().toLowerCase().contains(&#34;error&#34;))
	{
		response.put(&#34;text&#34;, &#34;post failed: &#34; + r.toString());
	}
	return response;
}
if(payload.get(&#34;title&#34;) != null)
{
	zoho.cliq.postToChannelAsBot(&#34;sandbox_channel&#34;, &#34;mybuddy&#34;, &#34;*&#34; + payload.get(&#34;title&#34;) + &#34;*\n&#34; + payload.get(&#34;detail&#34;));
}
return response;
</code></pre><h3 id="calling-your-own-service-from-cliq">Calling Your Own Service From Cliq</h3>
<pre tabindex="0"><code class="language-deluge" data-lang="deluge">mh = Map();
mh.put(&#34;X-My-Secret&#34;, &#34;PASTE_SHARED_SECRET&#34;);     // keep real secrets out of git
mh.put(&#34;Content-Type&#34;, &#34;application/json&#34;);
res = invokeurl
[
	url: &#34;https://example.com/hook&#34;
	type: POST
	parameters: payload.toString()
	headers: mh
];
info res;
</code></pre><h2 id="connections">Connections</h2>
<p>A connection is a named credential: put <code>connection: &quot;name&quot;</code> in <code>invokeurl</code> and Zoho adds and refreshes the token. The name is the link name shown in the console.</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>Kinds         Zoho services (Cliq, Desk, CRM, Creator, Catalyst), prebuilt third-party (GitHub, Google, Slack),
</span></span><span style="display:flex;"><span>              custom (OAuth 2 client id and secret, or an API key)
</span></span><span style="display:flex;"><span>Create        Bots &amp; Tools &gt; Connections &gt; create &gt; pick service &gt; name &gt; scopes &gt; authorise
</span></span><span style="display:flex;"><span>Identity      calls run as the authorising user; use a dedicated account for production
</span></span><span style="display:flex;"><span>Login user    option &#34;use credentials of login user&#34;: each user authorises their own account
</span></span><span style="display:flex;"><span>Scopes        ZohoCliq.Messages.READ (read threads), ZohoCliq.Webhooks.CREATE (post, start threads)
</span></span><span style="display:flex;"><span>New scope     reauthorise the connection
</span></span></code></pre></div><pre tabindex="0"><code class="language-deluge" data-lang="deluge">// GET through a connection
res = invokeurl
[
	url: &#34;https://cliq.zoho.in/api/v2/chats/&#34; + chatId + &#34;/messages?limit=100&#34;
	type: GET
	connection: &#34;myconnection&#34;
];
info res;
</code></pre><pre tabindex="0"><code class="language-deluge" data-lang="deluge">// create a GitHub issue from a message through another connection
issue = Map();
issue.put(&#34;title&#34;, &#34;From Cliq&#34;);
issue.put(&#34;body&#34;, message.get(&#34;text&#34;).toString());
hdr = Map();
hdr.put(&#34;Content-Type&#34;, &#34;application/json&#34;);
r = invokeurl
[
	url: &#34;https://api.github.com/repos/OWNER/REPO/issues&#34;
	type: POST
	parameters: issue.toString()
	headers: hdr
	connection: &#34;githubcon&#34;
];
info r;                                  // a 200 with an error body is common: always log
</code></pre><h3 id="cliq-rest-through-a-connection">Cliq REST Through a Connection</h3>
<table>
  <thead>
      <tr>
          <th>Job</th>
          <th>Call</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Recent messages of a chat or thread</td>
          <td><code>GET /api/v2/chats/{chat_id}/messages?limit=100</code></td>
      </tr>
      <tr>
          <td>One message</td>
          <td><code>GET /api/v2/chats/{chat_id}/messages/{message_id}</code></td>
      </tr>
      <tr>
          <td>The message a thread started from</td>
          <td><code>GET /api/v3/threads/{thread_chat_id}/messages/main</code></td>
      </tr>
      <tr>
          <td>Post to a chat or thread (shows the connection owner)</td>
          <td><code>POST /api/v3/chats/{chat_id}/messages</code> <code>{&quot;text&quot;:&quot;...&quot;}</code></td>
      </tr>
      <tr>
          <td>Post as the bot</td>
          <td><code>POST /api/v2/chats/{chat_id}/message?bot_unique_name={bot}</code> <code>{&quot;text&quot;:&quot;...&quot;}</code></td>
      </tr>
      <tr>
          <td>Start a thread under a channel message</td>
          <td><code>POST /api/v3/channelsbyname/{channel}/messages</code> <code>{&quot;text&quot;:&quot;...&quot;,&quot;thread_message_id&quot;:&quot;&lt;id&gt;&quot;}</code></td>
      </tr>
  </tbody>
</table>
<p>Useful message keys: <code>content.text</code>, <code>sender.id</code> (bots start with <code>b-</code>), <code>type</code>, <code>thread_information.chat_id</code>, <code>time</code>, <code>id</code>.</p>
<h2 id="message-actions">Message Actions</h2>
<p>Console: Bots &amp; Tools &gt; Message Actions &gt; create &gt; set access &gt; edit code. A generic name (for example &ldquo;Buddy&rdquo;) lets you add more actions later. The returned <code>text</code> is shown only to the person who clicked.</p>
<pre tabindex="0"><code class="language-deluge" data-lang="deluge">response = Map();
if(chat.get(&#34;chat_type&#34;) != &#34;channel&#34;)
{
	response.put(&#34;text&#34;, &#34;Use this on a channel message.&#34;);
	return response;
}
channelName = ifnull(chat.get(&#34;channel_unique_name&#34;), &#34;&#34;).toString();
chatId = chat.get(&#34;id&#34;).toString();
msgId = message.get(&#34;id&#34;).toString();
hdr = Map();
hdr.put(&#34;Content-Type&#34;, &#34;application/json&#34;);

// 1. use the message&#39;s thread, or start one under it
threadChat = &#34;&#34;;
ti = message.get(&#34;thread_information&#34;);
if(ti != null)
{
	threadChat = ti.toString().toMap().get(&#34;chat_id&#34;).toString();
}
else
{
	body = Map();
	body.put(&#34;text&#34;, &#34;#Noted&#34;);
	body.put(&#34;thread_message_id&#34;, msgId);
	invokeurl
	[
		url: &#34;https://cliq.zoho.in/api/v3/channelsbyname/&#34; + channelName + &#34;/messages&#34;
		type: POST
		parameters: body.toString()
		headers: hdr
		connection: &#34;myconnection&#34;
	];
	// 2. re-read the message to learn the new thread&#39;s chat id
	r2 = invokeurl
	[
		url: &#34;https://cliq.zoho.in/api/v2/chats/&#34; + chatId + &#34;/messages/&#34; + msgId
		type: GET
		connection: &#34;myconnection&#34;
	];
	ti2 = r2.toString().toMap().get(&#34;thread_information&#34;);
	if(ti2 != null)
	{
		threadChat = ti2.toString().toMap().get(&#34;chat_id&#34;).toString();
	}
}
if(threadChat == &#34;&#34;)
{
	response.put(&#34;text&#34;, &#34;Could not find the thread id.&#34;);
	return response;
}
response.put(&#34;text&#34;, &#34;Done.&#34;);
return response;
</code></pre><p>To post as the bot from a message action, call the bot&rsquo;s own incoming webhook (the connection call posts as its owner and ignores the bot name):</p>
<pre tabindex="0"><code class="language-deluge" data-lang="deluge">hook = Map();
hook.put(&#34;chat_id&#34;, threadChat);
hook.put(&#34;text&#34;, &#34;#Checking&#34;);
hook.put(&#34;bot_unique_name&#34;, &#34;mybuddy&#34;);
hdr = Map();
hdr.put(&#34;Content-Type&#34;, &#34;application/json&#34;);
invokeurl
[
	url: &#34;https://cliq.zoho.in/api/v2/bots/mybuddy/incoming?zapikey=PASTE_WEBHOOK_TOKEN&#34;
	type: POST
	parameters: hook.toString()
	headers: hdr
];
</code></pre><h3 id="why-a-message-action-instead-of-a-mention">Why a Message Action Instead of a Mention</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>A mention as the first message of a new thread is never delivered to the bot.
</span></span><span style="display:flex;"><span>The action works on the message itself, can create the thread, and needs no typing.
</span></span></code></pre></div><h2 id="limits">Limits</h2>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>Messages posted by other bots or integrations never reach a handler you can write.
</span></span><span style="display:flex;"><span>  Use a message action (click) or a scheduled job that reads the channel through a connection.
</span></span><span style="display:flex;"><span>A bot cannot start a thread: the first message of a thread shows the connection owner.
</span></span><span style="display:flex;"><span>A message posted as a bot never returns its id: post by chat id through a connection.
</span></span><span style="display:flex;"><span>A connection call ignores the bot name; the v2 chat endpoint with ?bot_unique_name= honours it.
</span></span><span style="display:flex;"><span>A channel can hold at most 10 bots.
</span></span><span style="display:flex;"><span>Access is by channel, not by user: keep an allowed-channels list and check it in the handler.
</span></span></code></pre></div><h2 id="troubleshooting">Troubleshooting</h2>
<table>
  <thead>
      <tr>
          <th>Symptom</th>
          <th>Cause</th>
          <th>Fix</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>Variable 'x' is not defined</code> on save</td>
          <td>Guessed variable name</td>
          <td>Use the names in the editor&rsquo;s top comment</td>
      </tr>
      <tr>
          <td>Webhook returns 200, nothing in Cliq</td>
          <td>200 only means accepted</td>
          <td>Check the handler log; bot needs channel membership</td>
      </tr>
      <tr>
          <td>First mention in a new thread does nothing</td>
          <td>Not delivered to bots</td>
          <td>Use a message action</td>
      </tr>
      <tr>
          <td>Invalid or expired token</td>
          <td>Authoriser revoked or changed password</td>
          <td>Reauthorise the connection</td>
      </tr>
      <tr>
          <td>Permission error on a call</td>
          <td>Scope missing when authorised</td>
          <td>Add the scope, reauthorise</td>
      </tr>
      <tr>
          <td>Connection not found</td>
          <td>Wrong link name</td>
          <td>Copy the name from the console</td>
      </tr>
      <tr>
          <td>Post shows your name, not the bot&rsquo;s</td>
          <td>Connection posts show the owner</td>
          <td><code>?bot_unique_name=</code> on the v2 chat endpoint, or <code>postToChannelAsBot</code></td>
      </tr>
      <tr>
          <td>Bot&rsquo;s own status messages read back as replies</td>
          <td>They look human when posted through a connection</td>
          <td>Start them with a marker such as <code>*#</code> and skip those</td>
      </tr>
      <tr>
          <td>Ids contain <code>%20</code></td>
          <td>Cliq encodes spaces in some ids</td>
          <td>Normalise ids before storing</td>
      </tr>
      <tr>
          <td>Secret leaked into a chat</td>
          <td>Filled handler pasted as text</td>
          <td>Rotate it, and share filled handlers only by clipboard</td>
      </tr>
  </tbody>
</table>
<h2 id="quick-reference">Quick Reference</h2>
<table>
  <thead>
      <tr>
          <th>Item</th>
          <th>Value</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Bot incoming URL</td>
          <td><code>https://cliq.zoho.in/api/v2/bots/{bot}/incoming?zapikey={token}</code></td>
      </tr>
      <tr>
          <td>Webhook token limit</td>
          <td>5 per user</td>
      </tr>
      <tr>
          <td>Post as bot (Deluge)</td>
          <td><code>zoho.cliq.postToChannelAsBot(channel, bot, text)</code></td>
      </tr>
      <tr>
          <td>Use a connection</td>
          <td><code>invokeurl [ url: ... type: GET connection: &quot;name&quot; ]</code></td>
      </tr>
      <tr>
          <td>Debug</td>
          <td><code>info value;</code> then read the execution log</td>
      </tr>
      <tr>
          <td>Reply</td>
          <td><code>response = Map(); response.put(&quot;text&quot;, &quot;...&quot;); return response;</code></td>
      </tr>
      <tr>
          <td>Bot unique name</td>
          <td>letters, digits, underscore; no hyphens</td>
      </tr>
  </tbody>
</table>
<h2 id="see-also">See Also</h2>
<ul>
<li><a href="https://www.zoho.com/cliq/help/platform/bots.html">Bots</a> and <a href="https://www.zoho.com/cliq/help/platform/bothandlers.html">Bot handlers</a></li>
<li><a href="https://www.zoho.com/cliq/help/platform/bot-mentionshandler.html">Mentions handler</a>, <a href="https://www.zoho.com/cliq/help/platform/bot-participation-handler.html">Participation handler</a>, <a href="https://www.zoho.com/cliq/help/platform/bot-incomingwebhookhandler.html">Incoming webhook handler</a></li>
<li><a href="https://www.zoho.com/cliq/help/platform/webhook-tokens.html">Webhook tokens</a></li>
<li><a href="https://www.zoho.com/cliq/help/platform/messageactions.html">Message actions</a> and <a href="https://www.zoho.com/cliq/help/platform/messageaction-handlers.html">handler</a></li>
<li><a href="https://www.zoho.com/cliq/help/platform/connections.html">Connections in Cliq</a>, <a href="https://www.zoho.com/deluge/help/connections.html">Deluge connections</a></li>
<li><a href="https://www.zoho.com/cliq/help/platform/invoke-url.html">Invoke URL in Cliq</a>, <a href="https://www.zoho.com/deluge/help/webhook/invokeurl-api-task.html">Deluge invokeURL</a></li>
<li><a href="https://www.zoho.com/cliq/help/platform/cliq-responses.html">Cliq responses</a> (cards, buttons, forms, banners)</li>
<li><a href="https://www.zoho.com/cliq/help/restapi/v2/">REST API v2</a>, <a href="https://www.zoho.com/cliq/help/restapi/v3/messages/">Messages v3</a></li>
<li><a href="https://qref.eknath.dev/tools/curl/">curl</a> and <a href="https://qref.eknath.dev/tools/jq/">jq</a> for testing webhooks</li>
</ul>
]]></content:encoded></item><item><title>Telegram Bots</title><link>https://qref.eknath.dev/tools/telegram-bots/</link><guid isPermaLink="true">https://qref.eknath.dev/tools/telegram-bots/</guid><pubDate>Sat, 05 Dec 2026 00:00:00 +0000</pubDate><category>telegram</category><category>bot</category><category>api</category><category>webhook</category><category>automation</category><category>cli</category><description>Telegram Bot API quick reference: BotFather, polling vs webhooks, sending messages and keyboards, commands, groups and limits, with copy-paste snippets.</description><content:encoded><![CDATA[<h2 id="overview">Overview</h2>
<p>A Telegram bot is an account controlled over a plain HTTPS API: you call <code>https://api.telegram.org/bot&lt;TOKEN&gt;/METHOD</code> and receive updates either by polling or by a webhook. No SDK is required; every example here works with <code>curl</code>.</p>
<h2 id="setup">Setup</h2>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>1. In Telegram, open @BotFather and send /newbot      pick a display name, then a username ending in &#34;bot&#34;
</span></span><span style="display:flex;"><span>2. BotFather replies with the token                   123456:ABC-DEF... (a password: never commit it)
</span></span><span style="display:flex;"><span>3. Open your bot and press Start (a bot cannot message you first)
</span></span><span style="display:flex;"><span>4. Optional: /setcommands, /setprivacy, /setjoingroups, /revoke (new token), /mybots
</span></span></code></pre></div><p><strong>Warning:</strong> the token is a password. Never commit it, paste it into a chat or leave it in shell history; keep it in an environment variable or a <code>chmod 600</code> file outside the repo. The snippets below assume <code>TG_TOKEN</code>, <code>TG_CHAT</code> and <code>API=&quot;https://api.telegram.org/bot$TG_TOKEN&quot;</code> are set.</p>
<h2 id="quick-start">Quick Start</h2>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Check the token</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/getMe&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Press Start in the bot, then find your chat id</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/getUpdates&#34;</span> | jq <span style="color:#a5d6ff">&#39;.result[].message.chat | {id, type, username}&#39;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Send a message</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/sendMessage&#34;</span> -d <span style="color:#79c0ff">chat_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$TG_CHAT</span><span style="color:#a5d6ff">&#34;</span> -d <span style="color:#79c0ff">text</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;Build finished&#34;</span>
</span></span></code></pre></div><p>Every response is JSON: <code>{&quot;ok&quot;: true, &quot;result&quot;: ...}</code> or <code>{&quot;ok&quot;: false, &quot;error_code&quot;: 400, &quot;description&quot;: &quot;...&quot;}</code>. Always check <code>ok</code>.</p>
<h2 id="receiving-updates">Receiving Updates</h2>
<p>Two mutually exclusive ways. Unreceived updates are kept for at most 24 hours.</p>
<table>
  <thead>
      <tr>
          <th></th>
          <th>Long polling (<code>getUpdates</code>)</th>
          <th>Webhook (<code>setWebhook</code>)</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Needs a public URL</td>
          <td>No</td>
          <td>Yes, HTTPS, ports 443, 80, 88 or 8443</td>
      </tr>
      <tr>
          <td>Good for</td>
          <td>Local dev, scripts, laptops</td>
          <td>Servers, serverless, production</td>
      </tr>
      <tr>
          <td>Gotcha</td>
          <td>Only one poller per bot</td>
          <td>Polling stops working while a webhook is set</td>
      </tr>
  </tbody>
</table>
<h3 id="long-polling">Long Polling</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#ff7b72">import</span> <span style="color:#ff7b72">os</span><span style="color:#ff7b72;font-weight:bold">,</span> <span style="color:#ff7b72">time</span><span style="color:#ff7b72;font-weight:bold">,</span> <span style="color:#ff7b72">requests</span>
</span></span><span style="display:flex;"><span>API <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#79c0ff">f</span><span style="color:#a5d6ff">&#34;https://api.telegram.org/bot</span><span style="color:#a5d6ff">{</span>os<span style="color:#ff7b72;font-weight:bold">.</span>environ[<span style="color:#a5d6ff">&#39;TG_TOKEN&#39;</span>]<span style="color:#a5d6ff">}</span><span style="color:#a5d6ff">&#34;</span>
</span></span><span style="display:flex;"><span>offset <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#79c0ff">None</span>
</span></span><span style="display:flex;"><span><span style="color:#ff7b72">while</span> <span style="color:#79c0ff">True</span>:
</span></span><span style="display:flex;"><span>    r <span style="color:#ff7b72;font-weight:bold">=</span> requests<span style="color:#ff7b72;font-weight:bold">.</span>get(<span style="color:#79c0ff">f</span><span style="color:#a5d6ff">&#34;</span><span style="color:#a5d6ff">{</span>API<span style="color:#a5d6ff">}</span><span style="color:#a5d6ff">/getUpdates&#34;</span>, params<span style="color:#ff7b72;font-weight:bold">=</span>{<span style="color:#a5d6ff">&#34;timeout&#34;</span>: <span style="color:#a5d6ff">50</span>, <span style="color:#a5d6ff">&#34;offset&#34;</span>: offset}, timeout<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">60</span>)<span style="color:#ff7b72;font-weight:bold">.</span>json()
</span></span><span style="display:flex;"><span>    <span style="color:#ff7b72">for</span> u <span style="color:#ff7b72;font-weight:bold">in</span> r<span style="color:#ff7b72;font-weight:bold">.</span>get(<span style="color:#a5d6ff">&#34;result&#34;</span>, []):
</span></span><span style="display:flex;"><span>        offset <span style="color:#ff7b72;font-weight:bold">=</span> u[<span style="color:#a5d6ff">&#34;update_id&#34;</span>] <span style="color:#ff7b72;font-weight:bold">+</span> <span style="color:#a5d6ff">1</span>               <span style="color:#8b949e;font-style:italic"># acknowledge by moving the offset</span>
</span></span><span style="display:flex;"><span>        msg <span style="color:#ff7b72;font-weight:bold">=</span> u<span style="color:#ff7b72;font-weight:bold">.</span>get(<span style="color:#a5d6ff">&#34;message&#34;</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#ff7b72">if</span> msg <span style="color:#ff7b72;font-weight:bold">and</span> <span style="color:#a5d6ff">&#34;text&#34;</span> <span style="color:#ff7b72;font-weight:bold">in</span> msg:
</span></span><span style="display:flex;"><span>            requests<span style="color:#ff7b72;font-weight:bold">.</span>post(<span style="color:#79c0ff">f</span><span style="color:#a5d6ff">&#34;</span><span style="color:#a5d6ff">{</span>API<span style="color:#a5d6ff">}</span><span style="color:#a5d6ff">/sendMessage&#34;</span>, json<span style="color:#ff7b72;font-weight:bold">=</span>{<span style="color:#a5d6ff">&#34;chat_id&#34;</span>: msg[<span style="color:#a5d6ff">&#34;chat&#34;</span>][<span style="color:#a5d6ff">&#34;id&#34;</span>], <span style="color:#a5d6ff">&#34;text&#34;</span>: <span style="color:#a5d6ff">&#34;You said: &#34;</span> <span style="color:#ff7b72;font-weight:bold">+</span> msg[<span style="color:#a5d6ff">&#34;text&#34;</span>]})
</span></span><span style="display:flex;"><span>    time<span style="color:#ff7b72;font-weight:bold">.</span>sleep(<span style="color:#a5d6ff">0.2</span>)
</span></span></code></pre></div><h3 id="webhook">Webhook</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Register (secret_token: 1-256 chars of A-Z a-z 0-9 _ -)</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/setWebhook&#34;</span> <span style="color:#79c0ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#79c0ff"></span>  -d <span style="color:#79c0ff">url</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;https://example.com/tg/hook&#34;</span> <span style="color:#79c0ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#79c0ff"></span>  -d <span style="color:#79c0ff">secret_token</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;PASTE_LONG_RANDOM_SECRET&#34;</span> <span style="color:#79c0ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#79c0ff"></span>  -d <span style="color:#79c0ff">allowed_updates</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#39;[&#34;message&#34;,&#34;callback_query&#34;]&#39;</span> <span style="color:#79c0ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#79c0ff"></span>  -d <span style="color:#79c0ff">drop_pending_updates</span><span style="color:#ff7b72;font-weight:bold">=</span>true
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/getWebhookInfo&#34;</span>      <span style="color:#8b949e;font-style:italic"># pending_update_count, last_error_message, last_error_date</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/deleteWebhook&#34;</span>       <span style="color:#8b949e;font-style:italic"># go back to polling</span>
</span></span></code></pre></div><p>Telegram sends an HTTPS POST with a JSON <code>Update</code> and the header <code>X-Telegram-Bot-Api-Secret-Token</code>. Answer with a 2xx; otherwise it retries and eventually gives up.</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Minimal receiver (FastAPI): check the secret header, then handle the update</span>
</span></span><span style="display:flex;"><span><span style="color:#ff7b72">import</span> <span style="color:#ff7b72">os</span><span style="color:#ff7b72;font-weight:bold">,</span> <span style="color:#ff7b72">requests</span>
</span></span><span style="display:flex;"><span><span style="color:#ff7b72">from</span> <span style="color:#ff7b72">fastapi</span> <span style="color:#ff7b72">import</span> FastAPI, Header, HTTPException, Request
</span></span><span style="display:flex;"><span>app <span style="color:#ff7b72;font-weight:bold">=</span> FastAPI()
</span></span><span style="display:flex;"><span>API <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#79c0ff">f</span><span style="color:#a5d6ff">&#34;https://api.telegram.org/bot</span><span style="color:#a5d6ff">{</span>os<span style="color:#ff7b72;font-weight:bold">.</span>environ[<span style="color:#a5d6ff">&#39;TG_TOKEN&#39;</span>]<span style="color:#a5d6ff">}</span><span style="color:#a5d6ff">&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#d2a8ff;font-weight:bold">@app.post</span>(<span style="color:#a5d6ff">&#34;/tg/hook&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#ff7b72">async</span> <span style="color:#ff7b72">def</span> <span style="color:#d2a8ff;font-weight:bold">hook</span>(req: Request, x_telegram_bot_api_secret_token: str <span style="color:#ff7b72;font-weight:bold">=</span> Header(<span style="color:#79c0ff">None</span>)):
</span></span><span style="display:flex;"><span>    <span style="color:#ff7b72">if</span> x_telegram_bot_api_secret_token <span style="color:#ff7b72;font-weight:bold">!=</span> os<span style="color:#ff7b72;font-weight:bold">.</span>environ[<span style="color:#a5d6ff">&#34;TG_SECRET&#34;</span>]:
</span></span><span style="display:flex;"><span>        <span style="color:#ff7b72">raise</span> HTTPException(status_code<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">403</span>)
</span></span><span style="display:flex;"><span>    update <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#ff7b72">await</span> req<span style="color:#ff7b72;font-weight:bold">.</span>json()
</span></span><span style="display:flex;"><span>    msg <span style="color:#ff7b72;font-weight:bold">=</span> update<span style="color:#ff7b72;font-weight:bold">.</span>get(<span style="color:#a5d6ff">&#34;message&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#ff7b72">if</span> msg <span style="color:#ff7b72;font-weight:bold">and</span> msg<span style="color:#ff7b72;font-weight:bold">.</span>get(<span style="color:#a5d6ff">&#34;text&#34;</span>) <span style="color:#ff7b72;font-weight:bold">==</span> <span style="color:#a5d6ff">&#34;/start&#34;</span>:
</span></span><span style="display:flex;"><span>        requests<span style="color:#ff7b72;font-weight:bold">.</span>post(<span style="color:#79c0ff">f</span><span style="color:#a5d6ff">&#34;</span><span style="color:#a5d6ff">{</span>API<span style="color:#a5d6ff">}</span><span style="color:#a5d6ff">/sendMessage&#34;</span>, json<span style="color:#ff7b72;font-weight:bold">=</span>{<span style="color:#a5d6ff">&#34;chat_id&#34;</span>: msg[<span style="color:#a5d6ff">&#34;chat&#34;</span>][<span style="color:#a5d6ff">&#34;id&#34;</span>], <span style="color:#a5d6ff">&#34;text&#34;</span>: <span style="color:#a5d6ff">&#34;Hello!&#34;</span>})
</span></span><span style="display:flex;"><span>    <span style="color:#ff7b72">return</span> {<span style="color:#a5d6ff">&#34;ok&#34;</span>: <span style="color:#79c0ff">True</span>}
</span></span></code></pre></div><p>You can also reply inside the webhook response by returning JSON with a <code>method</code> field:</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-json" data-lang="json"><span style="display:flex;"><span>{<span style="color:#7ee787">&#34;method&#34;</span>: <span style="color:#a5d6ff">&#34;sendMessage&#34;</span>, <span style="color:#7ee787">&#34;chat_id&#34;</span>: <span style="color:#a5d6ff">123456789</span>, <span style="color:#7ee787">&#34;text&#34;</span>: <span style="color:#a5d6ff">&#34;Replied in the response&#34;</span>}
</span></span></code></pre></div><h3 id="local-webhook-testing">Local Webhook Testing</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Expose localhost over HTTPS (any tunnel works), then register the https URL it prints</span>
</span></span><span style="display:flex;"><span>cloudflared tunnel --url http://localhost:8000
</span></span><span style="display:flex;"><span>ngrok http <span style="color:#a5d6ff">8000</span>
</span></span></code></pre></div><h2 id="sending-messages">Sending Messages</h2>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Formatting: parse_mode=HTML is the least painful</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/sendMessage&#34;</span> -d <span style="color:#79c0ff">chat_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$TG_CHAT</span><span style="color:#a5d6ff">&#34;</span> -d <span style="color:#79c0ff">parse_mode</span><span style="color:#ff7b72;font-weight:bold">=</span>HTML <span style="color:#79c0ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#79c0ff"></span>  --data-urlencode <span style="color:#79c0ff">text</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#39;&lt;b&gt;Deploy&lt;/b&gt; done: &lt;a href=&#34;https://example.com/build/42&#34;&gt;build 42&lt;/a&gt; &lt;code&gt;main&lt;/code&gt;&#39;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Reply to a message / send into a forum topic</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/sendMessage&#34;</span> -d <span style="color:#79c0ff">chat_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$TG_CHAT</span><span style="color:#a5d6ff">&#34;</span> -d <span style="color:#79c0ff">text</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;Re:&#34;</span> -d <span style="color:#79c0ff">reply_to_message_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">123</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/sendMessage&#34;</span> -d <span style="color:#79c0ff">chat_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$TG_CHAT</span><span style="color:#a5d6ff">&#34;</span> -d <span style="color:#79c0ff">text</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;In topic&#34;</span> -d <span style="color:#79c0ff">message_thread_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">45</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Silent, no link preview</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/sendMessage&#34;</span> -d <span style="color:#79c0ff">chat_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$TG_CHAT</span><span style="color:#a5d6ff">&#34;</span> -d <span style="color:#79c0ff">text</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;FYI&#34;</span> -d <span style="color:#79c0ff">disable_notification</span><span style="color:#ff7b72;font-weight:bold">=</span>true <span style="color:#79c0ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#79c0ff"></span>  -d <span style="color:#a5d6ff">&#39;link_preview_options={&#34;is_disabled&#34;:true}&#39;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Files: path upload, or a file_id / URL</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/sendDocument&#34;</span> -F <span style="color:#79c0ff">chat_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$TG_CHAT</span><span style="color:#a5d6ff">&#34;</span> -F <span style="color:#79c0ff">document</span><span style="color:#ff7b72;font-weight:bold">=</span>@report.pdf -F <span style="color:#79c0ff">caption</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;Report&#34;</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/sendPhoto&#34;</span>    -F <span style="color:#79c0ff">chat_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$TG_CHAT</span><span style="color:#a5d6ff">&#34;</span> -F <span style="color:#79c0ff">photo</span><span style="color:#ff7b72;font-weight:bold">=</span>@shot.png
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Edit and delete</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/editMessageText&#34;</span> -d <span style="color:#79c0ff">chat_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$TG_CHAT</span><span style="color:#a5d6ff">&#34;</span> -d <span style="color:#79c0ff">message_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">123</span> -d <span style="color:#79c0ff">text</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;Updated&#34;</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/deleteMessage&#34;</span>   -d <span style="color:#79c0ff">chat_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$TG_CHAT</span><span style="color:#a5d6ff">&#34;</span> -d <span style="color:#79c0ff">message_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">123</span>
</span></span></code></pre></div><h3 id="formatting-modes">Formatting Modes</h3>
<table>
  <thead>
      <tr>
          <th><code>parse_mode</code></th>
          <th>Bold</th>
          <th>Code</th>
          <th>Must escape</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>HTML</code></td>
          <td><code>&lt;b&gt;x&lt;/b&gt;</code></td>
          <td><code>&lt;code&gt;x&lt;/code&gt;</code></td>
          <td><code>&lt;</code>, <code>&gt;</code>, <code>&amp;</code></td>
      </tr>
      <tr>
          <td><code>MarkdownV2</code></td>
          <td><code>*x*</code></td>
          <td><code>`x`</code></td>
          <td><code>_ * [ ] ( ) ~ &gt; # + - = | { } . !</code> with <code>\</code></td>
      </tr>
      <tr>
          <td>(none)</td>
          <td>none</td>
          <td>none</td>
          <td>nothing</td>
      </tr>
  </tbody>
</table>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#ff7b72">import</span> <span style="color:#ff7b72">re</span>
</span></span><span style="display:flex;"><span><span style="color:#ff7b72">def</span> <span style="color:#d2a8ff;font-weight:bold">md2</span>(s): <span style="color:#ff7b72">return</span> re<span style="color:#ff7b72;font-weight:bold">.</span>sub(<span style="color:#79c0ff">r</span><span style="color:#a5d6ff">&#39;([_*\[\]()~`&gt;#+\-=|</span><span style="color:#a5d6ff">{}</span><span style="color:#a5d6ff">.!</span><span style="color:#79c0ff">\\</span><span style="color:#a5d6ff">])&#39;</span>, <span style="color:#79c0ff">r</span><span style="color:#a5d6ff">&#39;</span><span style="color:#79c0ff">\\</span><span style="color:#a5d6ff">\1&#39;</span>, s)    <span style="color:#8b949e;font-style:italic"># MarkdownV2 escape for plain text parts</span>
</span></span></code></pre></div><h2 id="keyboards--callbacks">Keyboards &amp; Callbacks</h2>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Inline buttons under a message</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/sendMessage&#34;</span> -d <span style="color:#79c0ff">chat_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$TG_CHAT</span><span style="color:#a5d6ff">&#34;</span> -d <span style="color:#79c0ff">text</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;Deploy to prod?&#34;</span> <span style="color:#79c0ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#79c0ff"></span>  -d <span style="color:#a5d6ff">&#39;reply_markup={&#34;inline_keyboard&#34;:[[{&#34;text&#34;:&#34;Yes&#34;,&#34;callback_data&#34;:&#34;deploy:yes&#34;},{&#34;text&#34;:&#34;No&#34;,&#34;callback_data&#34;:&#34;deploy:no&#34;}],[{&#34;text&#34;:&#34;Open build&#34;,&#34;url&#34;:&#34;https://example.com/build/42&#34;}]]}&#39;</span>
</span></span></code></pre></div><p>A press arrives as <code>update.callback_query</code> (<code>id</code>, <code>data</code>, <code>from</code>, <code>message</code>). You must answer it, or the client keeps showing a spinner:</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/answerCallbackQuery&#34;</span> -d <span style="color:#79c0ff">callback_query_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$CQ_ID</span><span style="color:#a5d6ff">&#34;</span> -d <span style="color:#79c0ff">text</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;Deploying...&#34;</span>          <span style="color:#8b949e;font-style:italic"># toast</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/editMessageText&#34;</span> -d <span style="color:#79c0ff">chat_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$TG_CHAT</span><span style="color:#a5d6ff">&#34;</span> -d <span style="color:#79c0ff">message_id</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">123</span> -d <span style="color:#79c0ff">text</span><span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;Deploying...&#34;</span>    <span style="color:#8b949e;font-style:italic"># replace the message</span>
</span></span></code></pre></div><p><code>callback_data</code> is 1-64 bytes: send an id or a short code, not a payload.</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Reply keyboard (replaces the phone keyboard) and removing it</span>
</span></span><span style="display:flex;"><span>-d <span style="color:#a5d6ff">&#39;reply_markup={&#34;keyboard&#34;:[[&#34;Status&#34;,&#34;Help&#34;]],&#34;resize_keyboard&#34;:true}&#39;</span>
</span></span><span style="display:flex;"><span>-d <span style="color:#a5d6ff">&#39;reply_markup={&#34;remove_keyboard&#34;:true}&#39;</span>
</span></span></code></pre></div><h2 id="commands">Commands</h2>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Menu shown when users type &#34;/&#34;</span>
</span></span><span style="display:flex;"><span>curl -s <span style="color:#a5d6ff">&#34;</span><span style="color:#79c0ff">$API</span><span style="color:#a5d6ff">/setMyCommands&#34;</span> -H <span style="color:#a5d6ff">&#39;Content-Type: application/json&#39;</span> <span style="color:#79c0ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#79c0ff"></span>  -d <span style="color:#a5d6ff">&#39;{&#34;commands&#34;:[{&#34;command&#34;:&#34;start&#34;,&#34;description&#34;:&#34;Start&#34;},{&#34;command&#34;:&#34;status&#34;,&#34;description&#34;:&#34;Show status&#34;}]}&#39;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Parse &#34;/status@mybot extra args&#34; in a handler</span>
</span></span><span style="display:flex;"><span>text <span style="color:#ff7b72;font-weight:bold">=</span> msg[<span style="color:#a5d6ff">&#34;text&#34;</span>]
</span></span><span style="display:flex;"><span><span style="color:#ff7b72">if</span> text<span style="color:#ff7b72;font-weight:bold">.</span>startswith(<span style="color:#a5d6ff">&#34;/&#34;</span>):
</span></span><span style="display:flex;"><span>    cmd, _, args <span style="color:#ff7b72;font-weight:bold">=</span> text<span style="color:#ff7b72;font-weight:bold">.</span>partition(<span style="color:#a5d6ff">&#34; &#34;</span>)
</span></span><span style="display:flex;"><span>    cmd <span style="color:#ff7b72;font-weight:bold">=</span> cmd<span style="color:#ff7b72;font-weight:bold">.</span>split(<span style="color:#a5d6ff">&#34;@&#34;</span>)[<span style="color:#a5d6ff">0</span>]<span style="color:#ff7b72;font-weight:bold">.</span>lstrip(<span style="color:#a5d6ff">&#34;/&#34;</span>)<span style="color:#ff7b72;font-weight:bold">.</span>lower()      <span style="color:#8b949e;font-style:italic"># strips the @botname added in groups</span>
</span></span></code></pre></div><h2 id="groups-channels--chat-ids">Groups, Channels &amp; Chat IDs</h2>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>Private chat     positive id; the user must press Start first
</span></span><span style="display:flex;"><span>Group            negative id (-123456789)
</span></span><span style="display:flex;"><span>Supergroup/chan  id starts with -100 (-1001234567890)
</span></span><span style="display:flex;"><span>Channel          add the bot as admin with &#34;Post messages&#34; to post; chat_id can be @channelusername
</span></span><span style="display:flex;"><span>Find an id       send a message in the chat, then getUpdates (webhook off), or forward it to a bot that shows ids
</span></span><span style="display:flex;"><span>Group upgraded   a group that becomes a supergroup gets a new id (error mentions migrate_to_chat_id)
</span></span></code></pre></div><p>Privacy mode is on by default: in groups the bot only sees commands, replies to it, mentions and service messages. Turn it off with <code>/setprivacy</code> in BotFather (then re-add the bot to the group), or make the bot an admin.</p>
<h2 id="limits">Limits</h2>
<table>
  <thead>
      <tr>
          <th>Limit</th>
          <th>Value</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Message text</td>
          <td>4096 characters (after entity parsing)</td>
      </tr>
      <tr>
          <td>Same chat</td>
          <td>about 1 message per second (bursts end in 429)</td>
      </tr>
      <tr>
          <td>Same group</td>
          <td>20 messages per minute</td>
      </tr>
      <tr>
          <td>Broadcast</td>
          <td>about 30 messages per second; paid broadcasts raise it</td>
      </tr>
      <tr>
          <td>Upload by the bot</td>
          <td>50 MB per file</td>
      </tr>
      <tr>
          <td>Download with <code>getFile</code></td>
          <td>20 MB per file</td>
      </tr>
      <tr>
          <td><code>callback_data</code></td>
          <td>1-64 bytes</td>
      </tr>
      <tr>
          <td>Undelivered updates kept</td>
          <td>24 hours</td>
      </tr>
  </tbody>
</table>
<p>On HTTP 429 the response has <code>parameters.retry_after</code> (seconds): sleep that long and retry.</p>
<h2 id="troubleshooting">Troubleshooting</h2>
<table>
  <thead>
      <tr>
          <th>Symptom</th>
          <th>Cause</th>
          <th>Fix</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>401 Unauthorized</code></td>
          <td>Wrong or revoked token</td>
          <td>Copy it again, or <code>/revoke</code> in BotFather</td>
      </tr>
      <tr>
          <td><code>404 Not Found</code> on every method</td>
          <td>Malformed token or URL (missing <code>bot</code> prefix, stray spaces)</td>
          <td>URL is <code>/bot&lt;TOKEN&gt;/METHOD</code>, token as BotFather gave it</td>
      </tr>
      <tr>
          <td><code>403 bot was blocked by the user</code></td>
          <td>User blocked the bot</td>
          <td>Stop sending to that chat</td>
      </tr>
      <tr>
          <td><code>400 chat not found</code></td>
          <td>User never pressed Start, or wrong id</td>
          <td>Have them start the bot; recheck the id</td>
      </tr>
      <tr>
          <td><code>400 can't parse entities</code></td>
          <td>Unescaped characters in Markdown or HTML</td>
          <td>Escape, or send without <code>parse_mode</code></td>
      </tr>
      <tr>
          <td><code>409 Conflict: terminated by other getUpdates</code></td>
          <td>Two pollers, or a webhook is set</td>
          <td>One poller only; <code>deleteWebhook</code> before polling</td>
      </tr>
      <tr>
          <td><code>getUpdates</code> returns nothing</td>
          <td>A webhook is registered</td>
          <td><code>getWebhookInfo</code>, then <code>deleteWebhook</code></td>
      </tr>
      <tr>
          <td>Webhook never fires</td>
          <td>Not HTTPS, bad port, bad certificate</td>
          <td>Check <code>last_error_message</code> in <code>getWebhookInfo</code></td>
      </tr>
      <tr>
          <td>Buttons show a spinner</td>
          <td><code>answerCallbackQuery</code> not called</td>
          <td>Call it on every callback</td>
      </tr>
      <tr>
          <td>Group messages ignored</td>
          <td>Privacy mode</td>
          <td><code>/setprivacy</code> &gt; Disable, then re-add the bot</td>
      </tr>
      <tr>
          <td><code>429 Too Many Requests</code></td>
          <td>Over the rate limit</td>
          <td>Honour <code>retry_after</code>, queue sends</td>
      </tr>
      <tr>
          <td>Token leaked</td>
          <td>Committed or pasted in chat</td>
          <td><code>/revoke</code> immediately, then update every deployment</td>
      </tr>
  </tbody>
</table>
<h2 id="quick-reference">Quick Reference</h2>
<table>
  <thead>
      <tr>
          <th>Item</th>
          <th>Value</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Base URL</td>
          <td><code>https://api.telegram.org/bot&lt;TOKEN&gt;/METHOD</code></td>
      </tr>
      <tr>
          <td>Files</td>
          <td><code>https://api.telegram.org/file/bot&lt;TOKEN&gt;/&lt;file_path&gt;</code> (path from <code>getFile</code>)</td>
      </tr>
      <tr>
          <td>Methods</td>
          <td><code>getMe</code>, <code>sendMessage</code>, <code>sendPhoto</code>, <code>sendDocument</code>, <code>editMessageText</code>, <code>deleteMessage</code>, <code>answerCallbackQuery</code>, <code>setMyCommands</code></td>
      </tr>
      <tr>
          <td>Updates</td>
          <td><code>getUpdates</code> (poll) or <code>setWebhook</code> (push), never both</td>
      </tr>
      <tr>
          <td>Webhook auth</td>
          <td>header <code>X-Telegram-Bot-Api-Secret-Token</code> equals your <code>secret_token</code></td>
      </tr>
      <tr>
          <td>Webhook ports</td>
          <td>443, 80, 88, 8443</td>
      </tr>
      <tr>
          <td>Reply from webhook</td>
          <td>JSON body with <code>&quot;method&quot;: &quot;sendMessage&quot;, ...</code></td>
      </tr>
      <tr>
          <td>New token</td>
          <td><code>/revoke</code> in @BotFather</td>
      </tr>
  </tbody>
</table>
<h2 id="see-also">See Also</h2>
<ul>
<li><a href="https://core.telegram.org/bots">Bots: an introduction for developers</a></li>
<li><a href="https://core.telegram.org/bots/api">Telegram Bot API reference</a></li>
<li><a href="https://core.telegram.org/bots/features">Bot features</a> (commands, keyboards, privacy mode, BotFather commands)</li>
<li><a href="https://core.telegram.org/bots/webhooks">Guide to webhooks</a></li>
<li><a href="https://core.telegram.org/bots/faq">Bots FAQ</a> (limits, broadcasting, files)</li>
<li><a href="https://core.telegram.org/bots/tutorial">From BotFather to Hello World</a></li>
<li><a href="https://core.telegram.org/bots/inline">Inline bots</a>, <a href="https://core.telegram.org/bots/webapps">Mini Apps</a>, <a href="https://core.telegram.org/bots/payments">Payments</a></li>
<li><a href="https://qref.eknath.dev/tools/curl/">curl</a> and <a href="https://qref.eknath.dev/tools/jq/">jq</a> for testing the API</li>
</ul>
]]></content:encoded></item></channel></rss>