<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Swapnil Soni on Medium]]></title>
        <description><![CDATA[Stories by Swapnil Soni on Medium]]></description>
        <link>https://medium.com/@swapnilsoni1999?source=rss-51a3c726793a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wK0sym7ouXd7peit.jpg</url>
            <title>Stories by Swapnil Soni on Medium</title>
            <link>https://medium.com/@swapnilsoni1999?source=rss-51a3c726793a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 03:03:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@swapnilsoni1999/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ Structuring a Scalable Telegram Bot with Telegraf.js (Express-Style)]]></title>
            <link>https://swapnilsoni1999.medium.com/structuring-a-scalable-telegram-bot-with-telegraf-js-express-style-f1688b63b008?source=rss-51a3c726793a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1688b63b008</guid>
            <category><![CDATA[telegram-bot]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[telegrafjs]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Swapnil Soni]]></dc:creator>
            <pubDate>Fri, 23 May 2025 22:27:42 GMT</pubDate>
            <atom:updated>2025-05-23T22:27:42.834Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AKxE-ygbLofpseLpPL4Ejw.png" /></figure><p>If you’ve ever worked with <strong>Express.js</strong>, you know how clean and scalable its structure is. We typically organize our applications into controllers, routes, middlewares, and types, making the project easy to maintain and grow.</p><p>Recently, while building a <strong>Telegram bot</strong> using <a href="https://github.com/telegraf/telegraf">telegraf.js</a>, I found myself wanting the same clarity. After digging deeper into Telegraf&#39;s architecture — with its expressive APIs like bot.command, bot.action, and bot.use — I discovered we could structure it just like an Express app… <strong>but with even more power</strong>.</p><p>In this post, I’ll walk you through how I organized my Telegraf.js bot to mimic an Express.js app’s structure — clean, modular, and scalable.</p><h3>🧠 The Express Pattern We All Know</h3><p>A typical Express app might look like:</p><pre>src/<br>├── controllers/<br>│   ├── auth.controller.ts<br>│   └── user.controller.ts<br>├── routes/<br>│   ├── auth.routes.ts<br>│   └── user.routes.ts<br>├── middlewares/<br>│   └── auth.middleware.ts<br>└── app.ts</pre><p>You work with familiar interfaces: Request, Response, and NextFunction. Simple and beautiful.</p><h3>🤖 Telegraf: Express-like, but More Powerful</h3><p>Telegraf gives us a similar structure, but with more expressive options like:</p><ul><li>bot.command(&#39;/start&#39;, handler)</li><li>bot.action(/regex/, handler)</li><li>Scenes and Wizards for multi-step flows</li><li>bot.use(middleware) just like app.use</li></ul><p>But there’s a catch…</p><p>When you hover over ctx (context), it’s <strong>way more complex</strong> than Express’s req and res. This complexity can get overwhelming, especially when building large bots.</p><h3>🔍 Breaking Down ctx for Commands and Actions</h3><p>To tame the beast, I broke down the types for different handlers into clean reusable types.</p><p><strong>types/context.types.ts</strong></p><pre>import { Context } from &#39;telegraf&#39;<br>import {<br>  CallbackQuery,<br>  Message,<br>  Update<br>} from &#39;telegraf/typings/core/types/typegram&#39;<br>import { CommandContextExtn } from &#39;telegraf/typings/telegram-types&#39;<br><br>export type CommandContext = Context&lt;{<br>  message: Update.New &amp; Update.NonChannel &amp; Message.TextMessage<br>  update_id: number<br>}&gt; &amp;<br>  Omit&lt;Context&lt;Update&gt;, keyof Context&lt;Update&gt;&gt; &amp;<br>  CommandContextExtn<br>export type ActionContext = Context&lt;Update.CallbackQueryUpdate&lt;CallbackQuery&gt;&gt; &amp;<br>  Omit&lt;Context&lt;Update&gt;, keyof Context&lt;Update&gt;&gt; &amp; {<br>    match: RegExpExecArray<br>  }</pre><p>These give us IntelliSense-friendly types in command and action handlers. No more messy ctx overloads!</p><h3>🧱 The bot.ts = The app.ts of Telegraf</h3><p>Just like how we bootstrap an Express app via app.ts, we’ll initialize our bot in bot.ts.</p><p><strong>bot.ts</strong></p><pre>import { Context, Telegraf } from &#39;telegraf&#39;<br>import { someMiddleware } from &#39;./middlewares/something.middleware&#39;<br>import { helpCommand } from &#39;./commands/help.command&#39;<br>import { someAction } from &#39;./actions/some.action&#39;<br>import { env } from &#39;./config/env&#39;<br><br>const bot = new Telegraf&lt;Context&gt;(env.BOT_TOKEN)<br><br>bot.use(someMiddleware)<br>bot.command(&#39;help&#39;, helpCommand)<br>bot.action(/something_+/, someAction)<br><br>export default bot</pre><h3>🧩 Middleware in Telegraf</h3><p>Just like Express, Telegraf middlewares take (ctx, next) and can be chained.</p><p><strong>middlewares/something.middleware.ts</strong></p><pre>import { Context, Middleware } from &#39;telegraf&#39;<br><br>export const someMiddleware: Middleware&lt;Context&gt; = async (ctx, next) =&gt; {<br>  // Do something before<br>  await next()<br>  // Do something after<br>}</pre><h3>📚 Commands and Actions</h3><p>Here’s how you can define a clean command and action structure using your custom Context types.</p><p><strong>commands/help.command.ts</strong></p><pre>import { CommandContext } from &#39;@/types/context.types&#39;<br><br>export const helpCommand = async (ctx: CommandContext) =&gt; {<br>  await ctx.reply(&#39;Here&#39;s how I can help you...&#39;)<br>}</pre><p><strong>actions/some.action.ts</strong></p><pre>import { ActionContext } from &#39;@/types/context.types&#39;<br><br>export const someAction = async (ctx: ActionContext) =&gt; {<br>  const matchedValue = ctx.match[0]<br>  await ctx.reply(`You clicked on: ${matchedValue}`)<br>}</pre><h3>🧠 Final Thoughts</h3><p>Telegraf.js is incredibly powerful — and by borrowing concepts from Express, we can:</p><ul><li>Make the code modular and testable</li><li>Reuse middleware logic</li><li>Enhance developer experience with proper typings</li></ul><p>Once you tame ctx, everything becomes just as intuitive as Express, with even more capabilities.</p><h3>✅ TL;DR</h3><ul><li>✅ Telegraf.js can be structured like an Express app</li><li>✅ Use custom Context types for better DX</li><li>✅ Organize commands, actions, and middleware into separate folders</li><li>✅ Treat bot.ts like your app.ts</li></ul><p>If you’re building a serious Telegram bot, treat it like a real backend service — <strong>because it is one</strong>. 🔥</p><p>Feel free to reach out or drop your thoughts in the comments! If you have any questions or need any help then please connect with me on <a href="https://linkedin.com/in/swapnilsoni1999">Linkedin</a> or follow me on Github — <a href="https://github.com/SwapnilSoni1999">SwapnilSoni1999</a></p><p>Happy botting 🤖.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1688b63b008" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Anonymous posting on Facebook using Graph API, Telegram Bot API and Google Apps Script for Google…]]></title>
            <link>https://swapnilsoni1999.medium.com/anonymous-posting-on-facebook-using-graph-api-telegram-bot-api-and-google-apps-script-for-google-45b4b586b682?source=rss-51a3c726793a------2</link>
            <guid isPermaLink="false">https://medium.com/p/45b4b586b682</guid>
            <category><![CDATA[google-apps-script]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[facebook]]></category>
            <category><![CDATA[facebook-graph-api]]></category>
            <category><![CDATA[telegram]]></category>
            <dc:creator><![CDATA[Swapnil Soni]]></dc:creator>
            <pubDate>Wed, 21 Apr 2021 13:14:16 GMT</pubDate>
            <atom:updated>2021-04-21T15:43:38.710Z</atom:updated>
            <content:encoded><![CDATA[<h3>Automated Anonymous posting on Facebook using Graph API, Telegram Bot API and Google Apps Script for Google forms</h3><p>We’ve seen anonymous/confession posting Facebook or Instagram pages right. But this time we’re gonna automate it using some awesome APIs provided by Google, Telegram and Facebook.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uR8hfkwSM_wkpOfQdXqZrQ.png" /></figure><h3>Google Forms</h3><ul><li>Firstly we’re gonna create a Google form shown in the image below.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wSDLiMpoUtWIHpU_bdmLvA.png" /></figure><ul><li>After that we’re gonna use <strong>Google Apps Script </strong>simply by clicking on <em>Menu &gt; Script Editor</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/1*w7QOFPVYBijqza_9h4vUjw.png" /></figure><ul><li>After that Paste the following script in the editor.</li></ul><h4>How to obtain formId</h4><p>— — — — — — — — — — <br>Click on edit on google forms and you can see form url.</p><pre><a href="https://docs.google.com/forms/d/1om0-Lu14s1twCjTDZjmT2beVHbF8uM-L-jINkDmiiwM/edit">https://docs.google.com/forms/d/&lt;your-form-id&gt;/edit</a></pre><p>— — — — — — — — — —</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/688c889be03692c1f6361a3acb381b5d/href">https://medium.com/media/688c889be03692c1f6361a3acb381b5d/href</a></iframe><p><strong>Note</strong>: As you can see your-server-ip/domain-name is the endpoint which we will create later in this tutorial.</p><ul><li>After that save the script file and move to <strong>Triggers</strong> section in Apps Script</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*BEwTz1WU6HYv1VfDbAuCGQ.png" /></figure><ul><li>Here in triggers section, click on <strong>Add Trigger</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/1*ypca8qWyDpHnXb4VvzIxyw.png" /></figure><ul><li>Once you click on Add Trigger you will see a dialog box.</li><li>Here Select the <strong>Choose which function to run </strong>and select sendWebHookReq</li><li>Now in <strong>Select event type</strong> choose On form submit</li><li>You can keep rest of the options shown in the image.</li><li>Once done, Click on <strong>Save</strong> and we’re now done with Google form setup.</li></ul><h3>Facebook Page</h3><p>Now, We’re gonna create a Facebook page, Visit <a href="https://facebook.com/">facebook</a> and click on + icon, and create a new page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*kESbKe3pRQa_gcPpwTAGlg.png" /></figure><ul><li>Once you created your page, You can open <strong>About</strong> section of your page and obtain <strong>PAGE ID</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*_HFkl6B2NqiYb4zq2-Hf9A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/295/1*sf0WxUxJEQewMO9XnBQ3OA.jpeg" /></figure><ul><li>Once you open your About page section, scroll to bottom of the page and you can see a <strong>Page ID</strong></li></ul><p><strong>Note</strong>: Page ID will be only visible to the page owner/admin only, So make sure you’re the page owner/admin.</p><ul><li>Save this ID for later use in the tutorial.</li></ul><h4>Facebook Graph API</h4><p>Now lets move on to graph api, Visit https://developers.facebook.com/apps</p><ol><li>There, click on <strong>Create App</strong></li><li>Select <strong>Manage Business Integrations </strong>and click on<strong> Continute</strong></li><li>Follow the next page steps and Create your app.</li><li>Once done, Goto <a href="https://developers.facebook.com/tools/explorer/">Facebook Graph API Explorer</a></li><li>Below in <strong>Permissions</strong>, Choose the following:</li></ol><pre>publish_to_groups<br>pages_manage_metadata<br>pages_manage_post</pre><p>Choose your App from right sidebar and on <em>User or Page </em>Choose<em> </em><strong>Get Page Access Token</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*s_TODHapr4bR1xvLI35hCA.png" /></figure><p>Now Authorize your profile and you will see an access token visible in the top input bar.</p><h4>Extending Access Token (Long Lived Token)</h4><ul><li>After authorization, Choose your page from <strong>User and Page</strong> dropdown</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*F1XXJelX3dYqt6AMbIJLzQ.png" /></figure><ul><li>After selecting your page, You will see the page token got updated.</li><li>Now click on the <em>i </em>Button beside Access Token and click on <strong>Open in Access Token Tool</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HKnbNnBsZ7vq0umk_5lleQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/343/1*mMfvFuF-rXtUS4i2GVsRSg.png" /></figure><ul><li>Here, click on <strong>Extend Access Token</strong></li><li>After that, A new token will appear with a green text.</li><li>That is your long lived access token, The token will expire after 2 months from present.</li></ul><h3><strong>Telegram Bot API</strong></h3><p>Now open your telegram and search for <a href="https://t.me/botfather">@BotFather</a></p><p>There, create your bot using /newbot command.</p><p>Once you’re done. You will obtain a <strong>Bot Token </strong>which will look like this 1234:xxxx | 1234 = bot number, xxxx = bot hash key</p><p>Alright, Now we have following things in our cup.</p><ul><li>Google Apps Script WebHook method</li><li>Facebook Page ID</li><li>Facebook Page Access Token (Long-Lived)</li><li>Telegram Bot Token</li></ul><p>Now let’s make a Server to handle all these incoming process.</p><h3><strong>Node.js Server</strong></h3><p><strong>Note</strong>: <em>I believe you’re familiar with creating Node.js apps. I’ll roughly guide you with this. Make sure you’re having MongoDB and Node.js Installed in your machine.</em></p><ol><li>Initialize your project with npm init</li><li>Install express, telegraf, mongoose and axios using npm install express telegraf axios</li><li>Start your mongodb service</li><li>Create app.js and paste the following code. This will handle the incoming request from Google Apps Script onFormSubmit trigger.</li></ol><p><strong>app.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/642e41d3482f2b9e1367091d6b61602a/href">https://medium.com/media/642e41d3482f2b9e1367091d6b61602a/href</a></iframe><p>In the same folder create a <strong>bot.js</strong> file where we will handle our telegram requests</p><p><strong>bot.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f5896745ca19270d87fed7636a8e29f/href">https://medium.com/media/7f5896745ca19270d87fed7636a8e29f/href</a></iframe><p><strong>Note</strong>: <em>You can obtain chat_id by messaging to rosebot or any other bot which provides you id</em></p><p>Now we will create another file to handle Facebook posts</p><p><strong>page.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2958b45e5a4e83832576b0c05e1955ff/href">https://medium.com/media/2958b45e5a4e83832576b0c05e1955ff/href</a></iframe><p>We’re almost done with our automation setup we just need to create model to store information in MongoDB.</p><p>For that, We will create a model in <strong>models/confession.js</strong></p><p><strong>confession.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3dbc1281fef79e6e265aab1877a15d0/href">https://medium.com/media/c3dbc1281fef79e6e265aab1877a15d0/href</a></iframe><p>Now we’re done. Lets create a start script in <strong>package.json</strong> by adding in scripts obj,</p><p>in <strong>package.json</strong></p><pre>&quot;scripts&quot;: {<br>  &quot;start&quot;: &quot;node app.js&quot;,<br>} </pre><p>Once we are done, Deploy this to your desired cloud server. And make sure you’re having the same URL as we used in Google Apps Script.</p><p>The final thing is testing.</p><ul><li>Open your google form submission</li><li>Submit it, as soon as you will submit, The bot will message you for an approval request.</li><li>Once you approve , You can see your post on your page, Tadaaa!</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45b4b586b682" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>