Skip to content

Commit

Permalink
fix(telegram): now telegram notifier not escape for markdown formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
async3619 committed Dec 12, 2022
1 parent c4177dd commit c2863c4
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 44 deletions.
1 change: 0 additions & 1 deletion src/notifiers/telegram/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ export const TELEGRAM_RENAMES_TEMPLATE = `
`.trim();

export const TELEGRAM_LOG_COUNT = 25;
export const FORBIDDEN_CHARACTERS = "()._-`*".split("");
45 changes: 2 additions & 43 deletions src/notifiers/telegram/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import pluralize from "pluralize";
import { capitalCase } from "change-case";

import { UserLogType } from "@repositories/models/user-log";

import { BaseNotifier } from "@notifiers/base";
import {
FORBIDDEN_CHARACTERS,
TELEGRAM_FOLLOWERS_TEMPLATE,
TELEGRAM_LOG_COUNT,
TELEGRAM_RENAMES_TEMPLATE,
Expand Down Expand Up @@ -61,7 +58,7 @@ export class TelegramNotifier extends BaseNotifier<"Telegram"> {
count,
pluralize(word, count),
target.map(this.formatNotify).join("\n"),
count > TELEGRAM_LOG_COUNT ? `_\\.\\.\\. and ${count - TELEGRAM_LOG_COUNT} more_` : "",
count > TELEGRAM_LOG_COUNT ? `_... and ${count - TELEGRAM_LOG_COUNT} more_` : "",
).trim();

resultMessages.push(message);
Expand All @@ -71,7 +68,7 @@ export class TelegramNotifier extends BaseNotifier<"Telegram"> {
const titleItems = targets.map(([, count, word]) => {
return count > 0 ? `${count} ${pluralize(capitalCase(word), count)}\n` : "";
});
const title = Logger.format("*🦜 Cage Report*\n\n{}{}{}", ...titleItems).trim();
const title = Logger.format("_**🦜 Cage Report**_\n\n{}{}{}", ...titleItems).trim();

await this.pushNotify([title, ...resultMessages]);
}
Expand Down Expand Up @@ -124,42 +121,4 @@ export class TelegramNotifier extends BaseNotifier<"Telegram"> {
}
}
}

protected escapeTexts(texts: string[]) {
return texts.map(text => this.escapeText(text));
}
protected escapeText(text: string) {
for (const ch of FORBIDDEN_CHARACTERS) {
text = text.replace(new RegExp(`\\${ch}`, "g"), `\\${ch}`);
}

return text;
}

protected formatNotify = (pair: NotifyPair): string => {
const [watcher, log] = pair;
const { user } = log;

if (log.type === UserLogType.RenameUserId || log.type === UserLogType.RenameDisplayName) {
const tokens = this.escapeTexts([
watcher.getName(),
log.oldDisplayName || "",
log.oldUserId || "",
watcher.getProfileUrl(log.user),
log.type === UserLogType.RenameDisplayName ? "" : "@",
log.type === UserLogType.RenameUserId ? user.userId : user.displayName,
]);

return Logger.format("\\[{}\\] [{} (@{})]\\({}\\) → {}{}", ...tokens);
}

const tokens = this.escapeTexts([
watcher.getName(),
user.displayName,
user.userId,
watcher.getProfileUrl(user),
]);

return Logger.format("\\[{}\\] [{} \\(@{}\\)]({})", ...tokens);
};
}

0 comments on commit c2863c4

Please sign in to comment.