-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(telegram): implement telegram notifier
- Loading branch information
Showing
21 changed files
with
376 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,40 @@ | ||
import { UserLog } from "@repositories/models/user-log"; | ||
|
||
import { BaseWatcher } from "@watchers/base"; | ||
import { NotifyPair } from "@notifiers/type"; | ||
|
||
import { Loggable } from "@utils/types"; | ||
import { UserLogType } from "@repositories/models/user-log"; | ||
import { Logger } from "@utils/logger"; | ||
|
||
export type NotifyPair = [BaseWatcher<string>, UserLog]; | ||
|
||
export abstract class BaseNotifier extends Loggable { | ||
protected constructor(name: string) { | ||
export abstract class BaseNotifier<TType extends string> extends Loggable<TType> { | ||
protected constructor(name: TType) { | ||
super(name); | ||
} | ||
|
||
public getName() { | ||
return super.getName().replace("Notifier", ""); | ||
} | ||
|
||
public abstract initialize(options: Record<string, any>): Promise<void>; | ||
public abstract initialize(): Promise<void>; | ||
|
||
public abstract notify(logs: NotifyPair[]): Promise<void>; | ||
|
||
protected formatNotify(pair: NotifyPair): string { | ||
const [watcher, log] = pair; | ||
const { user } = log; | ||
|
||
if (log.type === UserLogType.RenameUserId || log.type === UserLogType.RenameDisplayName) { | ||
const tokens = [ | ||
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 = [watcher.getName(), user.displayName, user.userId, watcher.getProfileUrl(user)]; | ||
return Logger.format("[{}] [{} (@{})]({})", ...tokens); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,33 @@ | ||
import { BaseNotifier } from "@notifiers/base"; | ||
import { DiscordNotifier, DiscordNotifierOptions } from "@notifiers/discord"; | ||
import { TelegramNotifier, TelegramNotifierOptions } from "@notifiers/telegram"; | ||
import { BaseNotifierOption } from "@notifiers/type"; | ||
|
||
import { TypeMap } from "@utils/types"; | ||
|
||
export type NotifierOptions = DiscordNotifierOptions; | ||
export type NotifierClasses = DiscordNotifier | TelegramNotifier; | ||
export type NotifierTypes = Lowercase<NotifierClasses["name"]>; | ||
|
||
export type NotifierOptions = DiscordNotifierOptions | TelegramNotifierOptions; | ||
export type NotifierOptionMap = TypeMap<NotifierOptions>; | ||
|
||
export const AVAILABLE_NOTIFIERS: ReadonlyArray<BaseNotifier> = [new DiscordNotifier()]; | ||
export type NotifierMap = { | ||
[TKey in NotifierTypes]: TKey extends NotifierTypes ? Extract<NotifierClasses, { name: TKey }> : never; | ||
}; | ||
export type NotifierFactoryMap = { | ||
[TKey in NotifierTypes]: TKey extends NotifierTypes | ||
? (options: NotifierOptionMap[TKey]) => Extract<NotifierClasses, { name: Capitalize<TKey> }> | ||
: never; | ||
}; | ||
export type NotifierPair = [NotifierTypes, BaseNotifier<string>]; | ||
|
||
const AVAILABLE_NOTIFIERS: Readonly<NotifierFactoryMap> = { | ||
discord: options => new DiscordNotifier(options), | ||
telegram: options => new TelegramNotifier(options), | ||
}; | ||
|
||
export const createNotifier = (options: BaseNotifierOption): BaseNotifier<string> => { | ||
const { type } = options; | ||
|
||
return AVAILABLE_NOTIFIERS[type](options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export const TELEGRAM_FOLLOWERS_TEMPLATE = ` | ||
*🎉 {} new {}* | ||
{} | ||
{} | ||
`.trim(); | ||
|
||
export const TELEGRAM_UNFOLLOWERS_TEMPLATE = ` | ||
*❌ {} {}* | ||
{} | ||
{} | ||
`.trim(); | ||
|
||
export const TELEGRAM_RENAMES_TEMPLATE = ` | ||
*✏️ {} {}* | ||
{} | ||
{} | ||
`.trim(); | ||
|
||
export const TELEGRAM_LOG_COUNT = 25; | ||
export const FORBIDDEN_CHARACTERS = "()._-`*".split(""); |
Oops, something went wrong.