Skip to content

Commit

Permalink
chore: 添加Markdown到MarkdownV2的转换
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Sep 6, 2024
1 parent 8c99bc8 commit f9de0cc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 60 additions & 9 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/timestamp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions src/telegram/utils/send.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type * as Telegram from 'telegram-bot-api-types';
import type { TelegramBotAPI } from '../api';
import { createTelegramBotAPI } from '../api';
import { escape } from './md2tgmd';

import { ENV } from '../../config/env';

Expand Down Expand Up @@ -88,23 +89,25 @@ export class MessageSender {
};
}

private renderMessage(parse_mode: Telegram.ParseMode | null, message: string): string {
if (parse_mode === 'MarkdownV2') {
return escape(message);
}
return message;
}

private async sendLongMessage(message: string, context: MessageContext): Promise<Response> {
const chatContext = { ...context };
const originMessage = message;
const limit = 4096;
if (message.length <= limit) {
const resp = await this.sendMessage(message, chatContext);
// 原始消息长度小于限制,直接使用当前parse_mode发送
const resp = await this.sendMessage(this.renderMessage(context.parse_mode, message), chatContext);
if (resp.status === 200) {
// 发送成功,直接返回
return resp;
} else {
message = originMessage;
// 可能格式错乱导致发送失败,使用纯文本格式发送
chatContext.parse_mode = null;
return await this.sendMessage(message, chatContext);
}
}
message = originMessage;
// 拆分消息后可能导致markdown格式错乱,所以采用纯文本模式发送
// 拆分消息后可能导致markdown格式错乱,所以采用纯文本模式发送,不使用任何parse_mode
chatContext.parse_mode = null;
let lastMessageResponse = null;
for (let i = 0; i < message.length; i += limit) {
Expand Down

0 comments on commit f9de0cc

Please sign in to comment.