Skip to content

Commit 8a211f6

Browse files
committed
Sanitize Telegram Markdown V2
1 parent 160c17a commit 8a211f6

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

functions/server-bot/ignore-old-messages-middleware.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import { Context, NextFunction } from "grammy";
33
const threshold = 5 * 60; // 5 minutes
44
export async function ignoreOldMessageMiddleware(
55
ctx: Context,
6-
next: NextFunction
6+
next: NextFunction,
77
): Promise<void> {
88
if (ctx.message) {
99
if (new Date().getTime() / 1000 - ctx.message.date < threshold) {
1010
await next();
1111
} else {
1212
console.log(
13-
`Ignoring message from ${ctx.from?.id ?? 'Unknown ID'} at ${
14-
ctx.chat?.id ?? 'Unknown Chat ID'
15-
} (${new Date().getTime() / 1000}:${ctx.message.date})`
13+
`Ignoring message from ${ctx.from?.id ?? "Unknown ID"} at ${
14+
ctx.chat?.id ?? "Unknown Chat ID"
15+
} (${new Date().getTime() / 1000}:${ctx.message.date})`,
1616
);
1717
}
1818
} else {

functions/server-bot/on-callback-query.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export const onCallbackQuery = (envSafe: EnvSafe) => async (ctx: Context) => {
2727
throw new Error(`Deck id ${deckId} is not valid`);
2828
}
2929
const state = await userGetServerBotState(envSafe, ctx.from.id);
30-
assert(state?.type === "cardAdded", "State is not cardAdded");
30+
if (state?.type !== "cardAdded") {
31+
return;
32+
}
3133
await userSetServerBotState(envSafe, ctx.from.id, {
3234
type: "deckSelected",
3335
cardBack: state.cardBack,

functions/server-bot/send-card-create-confirm-message.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import {
77
} from "../db/user/user-set-server-bot-state.ts";
88
import { CallbackQueryType } from "./callback-query-type.ts";
99

10+
const sanitizeMarkdown = (text: string) => {
11+
return text.replace("-", "\\-");
12+
};
13+
1014
export const sendCardCreateConfirmMessage = async (
1115
envSafe: EnvSafe,
1216
ctx: Context,
@@ -25,7 +29,9 @@ export const sendCardCreateConfirmMessage = async (
2529
await ctx.deleteMessage();
2630

2731
await ctx.reply(
28-
`Confirm card creation:\n\n*Front:*\n${state.cardFront}\n\n*Back:*\n${state.cardBack}`,
32+
`Confirm card creation:\n\n*Front:*\n${sanitizeMarkdown(
33+
state.cardFront,
34+
)}\n\n*Back:*\n${sanitizeMarkdown(state.cardBack)}`,
2935
{
3036
parse_mode: "MarkdownV2",
3137
reply_markup: InlineKeyboard.from([

0 commit comments

Comments
 (0)