fix: fix UTF8 characters are not encoded correctly (#79)
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"message-pusher/model"
|
"message-pusher/model"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
var TelegramMaxMessageLength = 4096
|
var TelegramMaxMessageLength = 4096
|
||||||
@@ -41,7 +42,10 @@ func SendTelegramMessage(message *model.Message, user *model.User, channel_ *mod
|
|||||||
for idx < len(text) {
|
for idx < len(text) {
|
||||||
nextIdx := idx + TelegramMaxMessageLength
|
nextIdx := idx + TelegramMaxMessageLength
|
||||||
if nextIdx > len(text) {
|
if nextIdx > len(text) {
|
||||||
|
// we have reach the end, must be valid
|
||||||
nextIdx = len(text)
|
nextIdx = len(text)
|
||||||
|
} else {
|
||||||
|
nextIdx = getNearestValidSplit(text, nextIdx)
|
||||||
}
|
}
|
||||||
messageRequest.Text = text[idx:nextIdx]
|
messageRequest.Text = text[idx:nextIdx]
|
||||||
idx = nextIdx
|
idx = nextIdx
|
||||||
@@ -65,3 +69,18 @@ func SendTelegramMessage(message *model.Message, user *model.User, channel_ *mod
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getNearestValidSplit(s string, idx int) int {
|
||||||
|
if idx >= len(s) {
|
||||||
|
return idx
|
||||||
|
}
|
||||||
|
if idx == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
isStartByte := utf8.RuneStart(s[idx])
|
||||||
|
if isStartByte {
|
||||||
|
return idx
|
||||||
|
} else {
|
||||||
|
return getNearestValidSplit(s, idx-1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user