Split transcription messages into smaller parts
This commit is contained in:
parent
3b2ff4d4a2
commit
6e0dde583a
28
main.go
28
main.go
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue