Skip to content

Commit

Permalink
bugfix:replace markdown exp char
Browse files Browse the repository at this point in the history
  • Loading branch information
ivhu committed Dec 8, 2024
1 parent 70b7ee2 commit 0e58564
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 3 additions & 4 deletions handler/gemini_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"chatbot/ai"
"chatbot/ai/gemini"
"chatbot/config"
"chatbot/utils"
"regexp"
"strings"
"sync"
Expand Down Expand Up @@ -107,8 +108,7 @@ func handleGroupChat(b *gotgbot.Bot, ctx *ext.Context, ai ai.AiInterface, s *tak

resp, err := ai.HandleText(setTake(s))
a <- struct{}{}
resp = strings.ReplaceAll(resp, "* **", "- **")
resp = strings.ReplaceAll(resp, "\n* ", "\n- ")
resp = utils.EscapeMarkdownChars(resp)
if err != nil {
s.tokeListYou = append(s.tokeListYou, "nop")
log.Error().Err(err).Msg("gemini say error")
Expand Down Expand Up @@ -172,8 +172,7 @@ func handlePrivateChat(b *gotgbot.Bot, ctx *ext.Context, ai ai.AiInterface) erro
}

func sendRespond(resp string, b *gotgbot.Bot, ctx *ext.Context) error {
resp = strings.ReplaceAll(resp, "* **", "- **")
resp = strings.ReplaceAll(resp, "\n* ", "\n- ")
resp = utils.EscapeMarkdownChars(resp)
log.Debug().Msgf("gemini say in chat: %s", resp)
for i := 0; i < 3; i++ {
_, err := ctx.EffectiveMessage.Reply(b, resp, &gotgbot.SendMessageOpts{
Expand Down
15 changes: 15 additions & 0 deletions utils/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/google/uuid"
Expand Down Expand Up @@ -101,3 +102,17 @@ func convertOgaToOggOpus(inputFile, outputFile string) error {
fmt.Printf("File converted successfully to %s\n", outputFile)
return nil
}

func EscapeMarkdownChars(input string) string {
// 定义需要转义的特殊字符
specialChars := "_*[]()~`>#+-=|{}.!"
// 创建一个 Builder 来高效拼接字符串
var builder strings.Builder
for _, char := range input {
if strings.ContainsRune(specialChars, char) {
builder.WriteRune('\\') // 添加反斜杠
}
builder.WriteRune(char) // 添加字符
}
return builder.String()
}

0 comments on commit 0e58564

Please sign in to comment.