From 4268f2816a03e55d5bcf6707b7769c852c59b9c4 Mon Sep 17 00:00:00 2001 From: Martial BE Date: Thu, 11 Apr 2024 15:28:02 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20escape=20markdown=20text?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/notify/channel/telegram.go | 2 +- common/utils.go | 8 ++++++++ controller/channel-test.go | 8 ++++---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/common/notify/channel/telegram.go b/common/notify/channel/telegram.go index 26889479..fe95e57d 100644 --- a/common/notify/channel/telegram.go +++ b/common/notify/channel/telegram.go @@ -61,7 +61,7 @@ func (t *Telegram) sendMessage(message string, client *requester.HTTPRequester) msg := telegramMessage{ ChatID: t.chatID, Text: message, - ParseMode: "Markdown", + ParseMode: "html", } uri := telegramURL + t.secret + "/sendMessage" diff --git a/common/utils.go b/common/utils.go index 4bae968c..ac6bafe6 100644 --- a/common/utils.go +++ b/common/utils.go @@ -240,3 +240,11 @@ func GetModelsWithMatch(modelList *[]string, modelName string) string { } return "" } + +func EscapeMarkdownText(text string) string { + chars := []string{"_", "*", "[", "]", "(", ")", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!", "`"} + for _, char := range chars { + text = strings.ReplaceAll(text, char, "\\"+char) + } + return text +} diff --git a/controller/channel-test.go b/controller/channel-test.go index 8d1fda61..7465320c 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -153,7 +153,7 @@ func testAllChannels(isNotify bool) error { time.Sleep(common.RequestInterval) isChannelEnabled := channel.Status == common.ChannelStatusEnabled - sendMessage += fmt.Sprintf("**通道 %s (#%d) [%s]** : \n\n", channel.Name, channel.Id, channel.StatusToStr()) + sendMessage += fmt.Sprintf("**通道 %s - #%d - %s** : \n\n", common.EscapeMarkdownText(channel.Name), channel.Id, channel.StatusToStr()) tik := time.Now() err, openaiErr := testChannel(channel, "") tok := time.Now() @@ -161,7 +161,7 @@ func testAllChannels(isNotify bool) error { // 通道为禁用状态,并且还是请求错误 或者 响应时间超过阈值 直接跳过,也不需要更新响应时间。 if !isChannelEnabled { if err != nil { - sendMessage += fmt.Sprintf("- 测试报错: %s \n\n- 无需改变状态,跳过\n\n", err.Error()) + sendMessage += fmt.Sprintf("- 测试报错: %s \n\n- 无需改变状态,跳过\n\n", common.EscapeMarkdownText(err.Error())) continue } if milliseconds > disableThreshold { @@ -187,13 +187,13 @@ func testAllChannels(isNotify bool) error { } if ShouldDisableChannel(openaiErr, -1) { - sendMessage += fmt.Sprintf("- 已被禁用,原因:%s\n\n", err.Error()) + sendMessage += fmt.Sprintf("- 已被禁用,原因:%s\n\n", common.EscapeMarkdownText(err.Error())) DisableChannel(channel.Id, channel.Name, err.Error(), false) continue } if err != nil { - sendMessage += fmt.Sprintf("- 测试报错: %s \n\n", err.Error()) + sendMessage += fmt.Sprintf("- 测试报错: %s \n\n", common.EscapeMarkdownText(err.Error())) continue } }