Skip to content

Commit

Permalink
refactor(telegram): make telegram notification title message having m…
Browse files Browse the repository at this point in the history
…ore rich data
  • Loading branch information
async3619 committed Dec 11, 2022
1 parent a8fd508 commit 00f2464
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"better-ajv-errors": "^1.2.0",
"boxen": "^5.1.2",
"chalk": "^4.1.2",
"change-case": "^4.1.2",
"commander": "^9.4.1",
"conventional-changelog-conventionalcommits": "^5.0.0",
"cronstrue": "^2.20.0",
Expand Down
2 changes: 1 addition & 1 deletion src/notifiers/telegram/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const TELEGRAM_FOLLOWERS_TEMPLATE = `
*🎉 {} new {}*
*🎉 {} {}*
{}
{}
Expand Down
15 changes: 11 additions & 4 deletions src/notifiers/telegram/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import pluralize from "pluralize";
import { capitalCase } from "change-case";

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

import { BaseNotifier } from "@notifiers/base";
import { BaseNotifierOption, NotifyPair } from "@notifiers/type";
import {
FORBIDDEN_CHARACTERS,
TELEGRAM_FOLLOWERS_TEMPLATE,
TELEGRAM_LOG_COUNT,
TELEGRAM_RENAMES_TEMPLATE,
TELEGRAM_UNFOLLOWERS_TEMPLATE,
} from "@notifiers/telegram/constants";
import { BaseNotifierOption, NotifyPair } from "@notifiers/type";

import { Fetcher } from "@utils/fetcher";
import { groupNotifies } from "@utils/groupNotifies";
import { Logger } from "@utils/logger";
import { HttpError } from "@utils/httpError";
import { UserLogType } from "@repositories/models/user-log";

export interface TelegramNotifierOptions extends BaseNotifierOption<TelegramNotifier> {
token: string;
Expand Down Expand Up @@ -43,7 +45,7 @@ export class TelegramNotifier extends BaseNotifier<"Telegram"> {
public async notify(logs: NotifyPair[]): Promise<void> {
const { follow, unfollow, rename } = groupNotifies(logs);
const targets: [NotifyPair[], number, string, string][] = [
[follow.slice(0, TELEGRAM_LOG_COUNT), follow.length, "follower", TELEGRAM_FOLLOWERS_TEMPLATE],
[follow.slice(0, TELEGRAM_LOG_COUNT), follow.length, "new follower", TELEGRAM_FOLLOWERS_TEMPLATE],
[unfollow.slice(0, TELEGRAM_LOG_COUNT), unfollow.length, "unfollower", TELEGRAM_UNFOLLOWERS_TEMPLATE],
[rename.slice(0, TELEGRAM_LOG_COUNT), rename.length, "rename", TELEGRAM_RENAMES_TEMPLATE],
];
Expand All @@ -66,7 +68,12 @@ export class TelegramNotifier extends BaseNotifier<"Telegram"> {
}

if (resultMessages.length > 0) {
await this.pushNotify(["*\\[🦜 Cage Report\\]*", ...resultMessages]);
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();

await this.pushNotify([title, ...resultMessages]);
}
}

Expand Down

0 comments on commit 00f2464

Please sign in to comment.