Parse Mode Plugin (parse-mode)

This plugin provides a transformer for setting default parse_mode, and a middleware for hydrating Context with familiar reply variant methods - i.e. replyWithHTML, replyWithMarkdown, etc.

Usage

import { Bot } from "grammy";
import { hydrateReply, parseMode } from "@grammyjs/parse-mode";

import type { ParseModeContext } from "@grammyjs/parse-mode";

const bot = new Bot<ParseModeContext>("");

// Use the plugin.
bot.use(hydrateReply);

// Sets default parse_mode for ctx.reply
bot.api.config.use(parseMode("MarkdownV2"));

bot.command("demo", async (ctx) => {
  await ctx.reply("*This* reply uses _MarkdownV2_ as the default `formatting`");
  await ctx.replyWithHTML(
    "<b>This</b> is <i>withHTML</i> <code>formatting</code>",
  );
  await ctx.replyWithMarkdown("*This* is _withMarkdown_ `formatting`");
  await ctx.replyWithMarkdownV1("*This* is _withMarkdownV1_ `formatting`");
  await ctx.replyWithMarkdownV2("*This* is _withMarkdownV2_ `formatting`");
});

bot.start();
const { Bot } = require("grammy");
const { hydrateReply, parseMode } = require("@grammyjs/parse-mode");

const bot = new Bot("");

// Use the plugin.
bot.use(hydrateReply);

// Set the default `parse_mode` of `ctx.reply`.
bot.api.config.use(parseMode("MarkdownV2"));

bot.command("demo", async (ctx) => {
  await ctx.reply("*This* reply uses _MarkdownV2_ as the default `formatting`");
  await ctx.replyWithHTML(
    "<b>This</b> is <i>withHTML</i> <code>formatting</code>",
  );
  await ctx.replyWithMarkdown("*This* is _withMarkdown_ `formatting`");
  await ctx.replyWithMarkdownV1("*This* is _withMarkdownV1_ `formatting`");
  await ctx.replyWithMarkdownV2("*This* is _withMarkdownV2_ `formatting`");
});

bot.start();
import { Bot } from "https://deno.land/x/grammy@v1.11.2/mod.ts";
import {
  hydrateReply,
  parseMode,
} from "https://deno.land/x/grammy_parse_mode@1.5.0/mod.ts";

import type { ParseModeContext } from "https://deno.land/x/grammy_parse_mode@1.5.0/mod.ts";

const bot = new Bot<ParseModeContext>("");

// Use the plugin.
bot.use(hydrateReply);

// Set the default `parse_mode` of `ctx.reply`.
bot.api.config.use(parseMode("MarkdownV2"));

bot.command("demo", async (ctx) => {
  await ctx.reply("*This* reply uses _MarkdownV2_ as the default `formatting`");
  await ctx.replyWithHTML(
    "<b>This</b> is <i>withHTML</i> <code>formatting</code>",
  );
  await ctx.replyWithMarkdown("*This* is _withMarkdown_ `formatting`");
  await ctx.replyWithMarkdownV1("*This* is _withMarkdownV1_ `formatting`");
  await ctx.replyWithMarkdownV2("*This* is _withMarkdownV2_ `formatting`");
});

bot.start();

Plugin Summary