Skip to content

Commit

Permalink
refactor: add url token and fix telegram adapt problem
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Jan 10, 2024
1 parent 5b826ca commit e6f0267
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/server/model/notification/provider/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const telegram: NotificationProvider = {
await axios.post(`https://api.telegram.org/bot${botToken}/sendMessage`, {
chat_id: chatId,
text,
parse_mode: 'MarkdownV2',
parse_mode: 'HTML',
});

// send image
Expand Down
6 changes: 6 additions & 0 deletions src/server/model/notification/token/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ParagraphContentToken,
TextContentToken,
TitleContentToken,
UrlContentToken,
} from './type';

export type { ContentToken };
Expand All @@ -34,6 +35,11 @@ export const token = {
newline: (): NewlineContentToken => ({
type: 'newline',
}),
url: (url: string, title?: string): UrlContentToken => ({
type: 'url',
url,
title,
}),
};

export const baseContentTokenizer = new BaseContentTokenizer();
Expand Down
5 changes: 5 additions & 0 deletions src/server/model/notification/token/tokenizer/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ParagraphContentToken,
TextContentToken,
TitleContentToken,
UrlContentToken,
} from '../type';

export class BaseContentTokenizer {
Expand All @@ -28,6 +29,10 @@ export class BaseContentTokenizer {
return '\n';
}

parseUrl(token: UrlContentToken) {
return token.url;
}

parse(tokens: ContentToken[]) {
return tokens
.map((token) => {
Expand Down
5 changes: 5 additions & 0 deletions src/server/model/notification/token/tokenizer/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
NewlineContentToken,
ParagraphContentToken,
TitleContentToken,
UrlContentToken,
} from '../type';
import { BaseContentTokenizer } from './base';

Expand Down Expand Up @@ -32,4 +33,8 @@ export class HTMLContentTokenizer extends BaseContentTokenizer {
parseNewline(token: NewlineContentToken) {
return '<br />';
}

parseUrl(token: UrlContentToken): string {
return `<a href="${token.url}">${token.title ?? token.url}</a>`;
}
}
5 changes: 5 additions & 0 deletions src/server/model/notification/token/tokenizer/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ImageContentToken,
ParagraphContentToken,
TitleContentToken,
UrlContentToken,
} from '../type';
import { BaseContentTokenizer } from './base';

Expand All @@ -27,4 +28,8 @@ export class MarkdownContentTokenizer extends BaseContentTokenizer {

return `\n${token.content}\n`;
}

parseUrl(token: UrlContentToken): string {
return `[${token.title ?? ''}](${token.url})`;
}
}
41 changes: 28 additions & 13 deletions src/server/model/notification/token/tokenizer/telegram.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
import { ImageContentToken, TitleContentToken } from '../type';
import { MarkdownContentTokenizer } from './markdown';
import {
ImageContentToken,
ParagraphContentToken,
TextContentToken,
TitleContentToken,
UrlContentToken,
} from '../type';
import { BaseContentTokenizer } from './base';

export class TelegramContentTokenizer extends MarkdownContentTokenizer {
export class TelegramContentTokenizer extends BaseContentTokenizer {
parseImage(token: ImageContentToken) {
return '';
}

parseText(token: TextContentToken): string {
return this.parseEntityCharacter(token.content);
}

parseTitle(token: TitleContentToken) {
if (token.level === 1) {
return `\n\\# ${token.content}\n`;
}
if (token.level === 2) {
return `\n\\#\\# ${token.content}\n`;
}
if (token.level === 3) {
return `\n\\#\\#\\# ${token.content}\n`;
}
return `\n<b>${this.parseEntityCharacter(token.content)}</b>\n`;
}

parseParagraph(token: ParagraphContentToken) {
return `\n${this.parseEntityCharacter(token.content)}\n`;
}

parseUrl(token: UrlContentToken): string {
return `<a href="${token.url}">${token.title ?? token.url}</a>`;
}

return `\n${token.content}\n`;
private parseEntityCharacter(input: string): string {
return input
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('&', '&amp;');
}
}
9 changes: 8 additions & 1 deletion src/server/model/notification/token/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ export type NewlineContentToken = {
type: 'newline';
};

export type UrlContentToken = {
type: 'url';
url: string;
title?: string;
};

export type ContentToken =
| TextContentToken
| ImageContentToken
| TitleContentToken
| ParagraphContentToken
| NewlineContentToken;
| NewlineContentToken
| UrlContentToken;

0 comments on commit e6f0267

Please sign in to comment.