解析模式(parse-mode
)
这个插件提供了一个设置默认的 parse
的 transformer,以及一个中间件,用于将 Context
中的 reply
方法转换成常用的 reply
,reply
,等等方法。
使用方法 (改善格式化体验)
ts
import { Bot, Context } from "grammy";
import { bold, fmt, hydrateReply, italic, link } from "@grammyjs/parse-mode";
import type { ParseModeFlavor } from "@grammyjs/parse-mode";
const bot = new Bot<ParseModeFlavor<Context>>("");
// 安装插件
bot.use(hydrateReply);
bot.command("demo", async (ctx) => {
await ctx.replyFmt(fmt`${bold("bold!")}
${bold(italic("bitalic!"))}
${bold(fmt`bold ${link("blink", "example.com")} bold`)}`);
// fmt 也可以像任何其他函数一样被调用
await ctx.replyFmt(
fmt(
["", " and ", " and ", ""],
fmt`${bold("bold")}`,
fmt`${bold(italic("bitalic"))}`,
fmt`${italic("italic")}`,
),
);
});
bot.start();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
js
const { Bot, Context } = require("grammy");
const { bold, fmt, hydrateReply, italic, link } = require(
"@grammyjs/parse-mode",
);
const bot = new Bot("");
// 安装插件
bot.use(hydrateReply);
bot.command("demo", async (ctx) => {
await ctx.replyFmt(fmt`${bold("bold!")}
${bold(italic("bitalic!"))}
${bold(fmt`bold ${link("blink", "example.com")} bold`)}`);
// fmt 也可以像任何其他函数一样被调用
await ctx.replyFmt(
fmt(
["", " and ", " and ", ""],
fmt`${bold("bold")}`,
fmt`${bold(italic("bitalic"))}`,
fmt`${italic("italic")}`,
),
);
});
bot.start();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
ts
import { Bot, Context } from "https://deno.land/x/grammy@v1.27.0/mod.ts";
import {
bold,
fmt,
hydrateReply,
italic,
link,
} from "https://deno.land/x/grammy_parse_mode@1.10.0/mod.ts";
import type { ParseModeFlavor } from "https://deno.land/x/grammy_parse_mode@1.10.0/mod.ts";
const bot = new Bot<ParseModeFlavor<Context>>("");
// 安装插件
bot.use(hydrateReply);
bot.command("demo", async (ctx) => {
await ctx.replyFmt(fmt`${bold("bold!")}
${bold(italic("bitalic!"))}
${bold(fmt`bold ${link("blink", "example.com")} bold`)}`);
// fmt 也可以像任何其他函数一样被调用
await ctx.replyFmt(
fmt(
["", " and ", " and ", ""],
fmt`${bold("bold")}`,
fmt`${bold(italic("bitalic"))}`,
fmt`${italic("italic")}`,
),
);
});
bot.start();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
使用方法 (默认解析模式和回复方法)
ts
import { Bot, Context } from "grammy";
import { hydrateReply, parseMode } from "@grammyjs/parse-mode";
import type { ParseModeFlavor } from "@grammyjs/parse-mode";
const bot = new Bot<ParseModeFlavor<Context>>("");
// 安装插件
bot.use(hydrateReply);
// 设置 ctx.reply 的默认解析模式
bot.api.config.use(parseMode("MarkdownV2"));
bot.command("demo", async (ctx) => {
await ctx.reply("*This* is _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();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
js
const { Bot, Context } = require("grammy");
const { hydrateReply, parseMode } = require("@grammyjs/parse-mode");
const bot = new Bot("");
// 安装插件
bot.use(hydrateReply);
// 设置 ctx.reply 的默认解析模式
bot.api.config.use(parseMode("MarkdownV2"));
bot.command("demo", async (ctx) => {
await ctx.reply("*This* is _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();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ts
import { Bot, Context } from "https://deno.land/x/grammy@v1.27.0/mod.ts";
import {
hydrateReply,
parseMode,
} from "https://deno.land/x/grammy_parse_mode@1.10.0/mod.ts";
import type { ParseModeFlavor } from "https://deno.land/x/grammy_parse_mode@1.10.0/mod.ts";
const bot = new Bot<ParseModeFlavor<Context>>("");
// 安装插件
bot.use(hydrateReply);
// 设置 ctx.reply 的默认解析模式
bot.api.config.use(parseMode("MarkdownV2"));
bot.command("demo", async (ctx) => {
await ctx.reply("*This* is _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();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27