<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Telegram-Bot on Asadbek Kurbonov</title><link>https://advenn.github.io/blog/tags/telegram-bot/</link><description>Recent content in Telegram-Bot on Asadbek Kurbonov</description><generator>Hugo -- 0.148.0</generator><language>en-us</language><lastBuildDate>Tue, 22 Jul 2025 23:24:14 +0500</lastBuildDate><atom:link href="https://advenn.github.io/blog/tags/telegram-bot/index.xml" rel="self" type="application/rss+xml"/><item><title>Setting Up Telegram Bot on Apps Script</title><link>https://advenn.github.io/blog/posts/setting-up-telegram-bot-on-apps-script/</link><pubDate>Tue, 22 Jul 2025 23:24:14 +0500</pubDate><guid>https://advenn.github.io/blog/posts/setting-up-telegram-bot-on-apps-script/</guid><description>&lt;p>Here is technical side of what I did on development on Apps Script. &lt;a href="https://advenn.github.io/blog/posts/apps-script">main article&lt;/a>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-javascript" data-lang="javascript">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Main webhook handler - receives all Telegram updates
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>&lt;span style="color:#66d9ef">function&lt;/span> &lt;span style="color:#a6e22e">doPost&lt;/span>(&lt;span style="color:#a6e22e">e&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">try&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> (&lt;span style="color:#f92672">!&lt;/span>&lt;span style="color:#a6e22e">e&lt;/span> &lt;span style="color:#f92672">||&lt;/span> &lt;span style="color:#f92672">!&lt;/span>&lt;span style="color:#a6e22e">e&lt;/span>.&lt;span style="color:#a6e22e">postData&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#a6e22e">ContentService&lt;/span>.&lt;span style="color:#a6e22e">createTextOutput&lt;/span>(&lt;span style="color:#e6db74">&amp;#39;Invalid request&amp;#39;&lt;/span>).&lt;span style="color:#a6e22e">setMimeType&lt;/span>(&lt;span style="color:#a6e22e">ContentService&lt;/span>.&lt;span style="color:#a6e22e">MimeType&lt;/span>.&lt;span style="color:#a6e22e">TEXT&lt;/span>).&lt;span style="color:#a6e22e">setStatusCode&lt;/span>(&lt;span style="color:#ae81ff">400&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">update&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">JSON&lt;/span>.&lt;span style="color:#a6e22e">parse&lt;/span>(&lt;span style="color:#a6e22e">e&lt;/span>.&lt;span style="color:#a6e22e">postData&lt;/span>.&lt;span style="color:#a6e22e">contents&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> (&lt;span style="color:#a6e22e">update&lt;/span>.&lt;span style="color:#a6e22e">message&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">handleMessage&lt;/span>(&lt;span style="color:#a6e22e">update&lt;/span>.&lt;span style="color:#a6e22e">message&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// IMPORTANT: Always return OK with proper content type and mainly proper status code (200&amp;lt;=status&amp;lt;300) to prevent retries
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#a6e22e">ContentService&lt;/span>.&lt;span style="color:#a6e22e">createTextOutput&lt;/span>(&lt;span style="color:#e6db74">&amp;#39;OK&amp;#39;&lt;/span>).&lt;span style="color:#a6e22e">setMimeType&lt;/span>(&lt;span style="color:#a6e22e">ContentService&lt;/span>.&lt;span style="color:#a6e22e">MimeType&lt;/span>.&lt;span style="color:#a6e22e">TEXT&lt;/span>).&lt;span style="color:#a6e22e">setStatusCode&lt;/span>(&lt;span style="color:#ae81ff">200&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> } &lt;span style="color:#66d9ef">catch&lt;/span> (&lt;span style="color:#a6e22e">error&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">console&lt;/span>.&lt;span style="color:#a6e22e">error&lt;/span>(&lt;span style="color:#e6db74">&amp;#39;Error in doPost:&amp;#39;&lt;/span>, &lt;span style="color:#a6e22e">error&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Even on error, return OK to prevent Telegram retries
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#a6e22e">ContentService&lt;/span>.&lt;span style="color:#a6e22e">createTextOutput&lt;/span>(&lt;span style="color:#e6db74">&amp;#39;OK&amp;#39;&lt;/span>).&lt;span style="color:#a6e22e">setMimeType&lt;/span>(&lt;span style="color:#a6e22e">ContentService&lt;/span>.&lt;span style="color:#a6e22e">MimeType&lt;/span>.&lt;span style="color:#a6e22e">TEXT&lt;/span>).&lt;span style="color:#a6e22e">setStatusCode&lt;/span>(&lt;span style="color:#ae81ff">500&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>By the nature of apps script, &lt;code>doPost&lt;/code> is used when the app received POST request. Telegram sends data via POST to webhook url. So we set up POST listener.&lt;/p></description></item></channel></rss>