diff --git a/src/server/model/notification/provider/telegram.ts b/src/server/model/notification/provider/telegram.ts index 609b355..7aa4110 100644 --- a/src/server/model/notification/provider/telegram.ts +++ b/src/server/model/notification/provider/telegram.ts @@ -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 diff --git a/src/server/model/notification/token/index.ts b/src/server/model/notification/token/index.ts index e2df7c5..3dc46ce 100644 --- a/src/server/model/notification/token/index.ts +++ b/src/server/model/notification/token/index.ts @@ -9,6 +9,7 @@ import { ParagraphContentToken, TextContentToken, TitleContentToken, + UrlContentToken, } from './type'; export type { ContentToken }; @@ -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(); diff --git a/src/server/model/notification/token/tokenizer/base.ts b/src/server/model/notification/token/tokenizer/base.ts index a580600..9c9ae6a 100644 --- a/src/server/model/notification/token/tokenizer/base.ts +++ b/src/server/model/notification/token/tokenizer/base.ts @@ -5,6 +5,7 @@ import { ParagraphContentToken, TextContentToken, TitleContentToken, + UrlContentToken, } from '../type'; export class BaseContentTokenizer { @@ -28,6 +29,10 @@ export class BaseContentTokenizer { return '\n'; } + parseUrl(token: UrlContentToken) { + return token.url; + } + parse(tokens: ContentToken[]) { return tokens .map((token) => { diff --git a/src/server/model/notification/token/tokenizer/html.ts b/src/server/model/notification/token/tokenizer/html.ts index a6351d4..b12656b 100644 --- a/src/server/model/notification/token/tokenizer/html.ts +++ b/src/server/model/notification/token/tokenizer/html.ts @@ -3,6 +3,7 @@ import { NewlineContentToken, ParagraphContentToken, TitleContentToken, + UrlContentToken, } from '../type'; import { BaseContentTokenizer } from './base'; @@ -32,4 +33,8 @@ export class HTMLContentTokenizer extends BaseContentTokenizer { parseNewline(token: NewlineContentToken) { return '
'; } + + parseUrl(token: UrlContentToken): string { + return `${token.title ?? token.url}`; + } } diff --git a/src/server/model/notification/token/tokenizer/markdown.ts b/src/server/model/notification/token/tokenizer/markdown.ts index db5da75..68d9f79 100644 --- a/src/server/model/notification/token/tokenizer/markdown.ts +++ b/src/server/model/notification/token/tokenizer/markdown.ts @@ -2,6 +2,7 @@ import { ImageContentToken, ParagraphContentToken, TitleContentToken, + UrlContentToken, } from '../type'; import { BaseContentTokenizer } from './base'; @@ -27,4 +28,8 @@ export class MarkdownContentTokenizer extends BaseContentTokenizer { return `\n${token.content}\n`; } + + parseUrl(token: UrlContentToken): string { + return `[${token.title ?? ''}](${token.url})`; + } } diff --git a/src/server/model/notification/token/tokenizer/telegram.ts b/src/server/model/notification/token/tokenizer/telegram.ts index 2c8d9de..1f13366 100644 --- a/src/server/model/notification/token/tokenizer/telegram.ts +++ b/src/server/model/notification/token/tokenizer/telegram.ts @@ -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 ''; } - 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`; - } + parseText(token: TextContentToken): string { + return this.parseEntityCharacter(token.content); + } - return `\n${token.content}\n`; + parseTitle(token: TitleContentToken) { + return `\n${this.parseEntityCharacter(token.content)}\n`; + } + + parseParagraph(token: ParagraphContentToken) { + return `\n${this.parseEntityCharacter(token.content)}\n`; + } + + parseUrl(token: UrlContentToken): string { + return `${token.title ?? token.url}`; + } + + private parseEntityCharacter(input: string): string { + return input + .replaceAll('<', '<') + .replaceAll('>', '>') + .replaceAll('&', '&'); } } diff --git a/src/server/model/notification/token/type.ts b/src/server/model/notification/token/type.ts index 881538c..243bf78 100644 --- a/src/server/model/notification/token/type.ts +++ b/src/server/model/notification/token/type.ts @@ -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;