### Install Auto-Thread Plugin Source: https://github.com/grammyjs/auto-thread/blob/main/README.md Installs the @grammyjs/auto-thread plugin directly from its GitHub repository using npm. This is the recommended installation method as the plugin may not have been officially released. ```sh npm install github:grammyjs/auto-thread ``` -------------------------------- ### Configure AutoThread with Custom Context in Grammy.js Source: https://context7.com/grammyjs/auto-thread/llms.txt This example shows how to integrate the `autoThread` middleware with a custom grammY context type. By passing the custom context type as a generic parameter to `autoThread`, you ensure type safety across your middleware chain, allowing for extended context functionalities like session management. ```typescript import { Bot, Context, SessionFlavor } from "grammy"; import { autoThread } from "@grammyjs/auto-thread"; // Define custom session data interface SessionData { messageCount: number; lastTopic?: number; } // Create custom context type type MyContext = Context & SessionFlavor; // Create bot with custom context const bot = new Bot("YOUR_BOT_TOKEN"); // Use autoThread with custom context type bot.use(autoThread()); // Session middleware (example) bot.use((ctx, next) => { ctx.session = { messageCount: 0 }; return next(); }); bot.on("message", async (ctx) => { ctx.session.messageCount++; ctx.session.lastTopic = ctx.msg?.message_thread_id; // Auto-thread handles the thread ID automatically await ctx.reply(`Message #${ctx.session.messageCount} in this session`); }); bot.start(); ``` -------------------------------- ### Deno Usage: Import and Use Auto Thread Plugin Source: https://context7.com/grammyjs/auto-thread/llms.txt Demonstrates how to import and use the Auto Thread plugin in a Deno environment. It shows bot initialization, middleware registration, and handling messages within forum topics. ```typescript import { Bot } from "https://deno.land/x/grammy/mod.ts"; import { autoThread } from "https://raw.githubusercontent.com/grammyjs/auto-thread/main/src/mod.ts"; const bot = new Bot(Deno.env.get("BOT_TOKEN")!); // Register middleware bot.use(autoThread()); // Handle messages in forum topics bot.command("ping", (ctx) => ctx.reply("Pong! (sent to correct thread)")); bot.on("message:photo", async (ctx) => { // Reply with a document - goes to the same thread await ctx.api.sendMessage(ctx.chat.id, "Nice photo! Here's a response."); }); bot.start(); ``` -------------------------------- ### Define and Use Router Middleware (TypeScript) Source: https://github.com/grammyjs/auto-thread/blob/main/src/README.md This snippet demonstrates how to create a Router instance, define routes with associated middleware, and register the router with the bot. It shows how to specify a routing function to determine which middleware to execute and how to handle cases where no specific route matches. ```typescript import { Bot, Context } from 'grammy'; import { Router } from '@grammyjs/router'; // Define a custom context type if needed interface MyContext extends Context { // Add custom properties here } const bot = new Bot('YOUR_BOT_TOKEN'); // Create a router instance, optionally with a custom context type const router = new Router(ctx => { // Determine the route key based on the context // For example, you could inspect ctx.message.text if (ctx.message?.text.startsWith('/start')) { return 'start'; } else if (ctx.message?.text.startsWith('/help')) { return 'help'; } return 'fallback'; // Default route if none match }); // Define middleware for specific routes router.route('start', async ctx => { await ctx.reply('Welcome! This is the start route.'); }); router.route('help', async ctx => { await ctx.reply('This is the help route. How can I assist you?'); }); // Define a fallback middleware for unmatched routes router.otherwise(async ctx => { await ctx.reply('Sorry, I did not understand that command.'); }); // Use the router middleware with the bot bot.use(router); // Start the bot bot.start(); ``` -------------------------------- ### Initialize and Use AutoThread Middleware in Grammy.js Source: https://context7.com/grammyjs/auto-thread/llms.txt This snippet demonstrates how to set up the `autoThread` middleware for a grammY bot. It ensures that all subsequent API calls, including sending messages and media, automatically include the correct `message_thread_id` when the original message originates from a forum topic. This middleware should be registered before other middleware to function correctly. ```typescript import { Bot } from "grammy"; import { autoThread } from "@grammyjs/auto-thread"; // Create your bot instance const bot = new Bot("YOUR_BOT_TOKEN"); // Register the autoThread middleware - must be registered before other middleware bot.use(autoThread()); // Now all replies automatically go to the correct topic thread bot.command("start", async (ctx) => { // This will automatically reply in the same forum topic // No need to manually specify message_thread_id await ctx.reply("Welcome! This message goes to the right topic automatically."); }); bot.on("message:text", async (ctx) => { // Even ctx.api.sendMessage calls are handled await ctx.api.sendMessage(ctx.chat.id, "This also goes to the correct thread!"); // Send media - thread ID is automatically added await ctx.api.sendPhoto(ctx.chat.id, "https://example.com/photo.jpg"); }); // Works with forwarding and copying messages too bot.command("forward", async (ctx) => { if (ctx.message.reply_to_message) { await ctx.api.forwardMessage( ctx.chat.id, ctx.chat.id, ctx.message.reply_to_message.message_id ); } }); // Start the bot bot.start(); ``` -------------------------------- ### Use Auto-Thread Plugin with Grammy.js Bot Source: https://github.com/grammyjs/auto-thread/blob/main/README.md Integrates the auto-thread plugin into a Grammy.js bot application. This enables automatic reply threading for messages sent via `ctx.api.sendMessage`. ```ts import { autoThread } from "@grammyjs/auto-thread"; bot.use(autoThread()); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.