mirror of
https://github.com/songquanpeng/message-pusher.git
synced 2025-09-26 20:21:22 +08:00
fix: fix message segmentation in Telegram's markdown parse mode (close #88)
This commit is contained in:
@@ -45,7 +45,7 @@ func SendTelegramMessage(message *model.Message, user *model.User, channel_ *mod
|
||||
// we have reach the end, must be valid
|
||||
nextIdx = len(text)
|
||||
} else {
|
||||
nextIdx = getNearestValidSplit(text, nextIdx)
|
||||
nextIdx = getNearestValidSplit(text, nextIdx, messageRequest.ParseMode)
|
||||
}
|
||||
messageRequest.Text = text[idx:nextIdx]
|
||||
idx = nextIdx
|
||||
@@ -70,7 +70,15 @@ func SendTelegramMessage(message *model.Message, user *model.User, channel_ *mod
|
||||
return nil
|
||||
}
|
||||
|
||||
func getNearestValidSplit(s string, idx int) int {
|
||||
func getNearestValidSplit(s string, idx int, mode string) int {
|
||||
if mode == "markdown" {
|
||||
return getMarkdownNearestValidSplit(s, idx)
|
||||
} else {
|
||||
return getPlainTextNearestValidSplit(s, idx)
|
||||
}
|
||||
}
|
||||
|
||||
func getPlainTextNearestValidSplit(s string, idx int) int {
|
||||
if idx >= len(s) {
|
||||
return idx
|
||||
}
|
||||
@@ -81,6 +89,22 @@ func getNearestValidSplit(s string, idx int) int {
|
||||
if isStartByte {
|
||||
return idx
|
||||
} else {
|
||||
return getNearestValidSplit(s, idx-1)
|
||||
return getPlainTextNearestValidSplit(s, idx-1)
|
||||
}
|
||||
}
|
||||
|
||||
func getMarkdownNearestValidSplit(s string, idx int) int {
|
||||
if idx >= len(s) {
|
||||
return idx
|
||||
}
|
||||
if idx == 0 {
|
||||
return 0
|
||||
}
|
||||
for i := idx; i >= 0; i-- {
|
||||
if s[i] == '\n' {
|
||||
return i + 1
|
||||
}
|
||||
}
|
||||
// unable to find a '\n'
|
||||
return idx
|
||||
}
|
||||
|
Reference in New Issue
Block a user