1
Fork 0

Split transcription messages into smaller parts

This commit is contained in:
Jan-Lukas Else 2024-03-30 21:38:08 +01:00
parent 3b2ff4d4a2
commit 6e0dde583a
1 changed files with 27 additions and 1 deletions

28
main.go
View File

@ -95,7 +95,9 @@ func main() {
log.Println("Error transcribing voice message:", err)
sendMessage(botToken, update.Message.Chat.ID, "Failed to transcribe voice message", update.Message.MessageID)
} else {
sendMessage(botToken, update.Message.Chat.ID, transcript, update.Message.MessageID)
for _, transcriptPart := range splitText(transcript, 3000) {
sendMessage(botToken, update.Message.Chat.ID, transcriptPart, update.Message.MessageID)
}
}
} else {
sendMessage(botToken, update.Message.Chat.ID, "Please send a voice message", update.Message.MessageID)
@ -190,3 +192,27 @@ func transcriptVoiceMessage(botToken, openaiApiKey, fileID string) (string, erro
return transcription.Text, err
}
func splitText(s string, chunkSize int) []string {
if chunkSize >= len(s) {
return []string{s}
}
var chunks []string
var b strings.Builder
b.Grow(chunkSize)
l := 0
for _, r := range s {
b.WriteRune(r)
l++
if l == chunkSize {
chunks = append(chunks, b.String())
l = 0
b.Reset()
b.Grow(chunkSize)
}
}
if l > 0 {
chunks = append(chunks, b.String())
}
return chunks
}