Split and preserve message
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -311,15 +312,19 @@ func encodeImageFromURL(imageBytes bytes.Buffer) (string, error) {
|
||||
return base64Image, nil
|
||||
}
|
||||
|
||||
// SplitText splits a long text into chunks of a specified maximum length without truncating words.
|
||||
// SplitText splits a long text into chunks of a specified maximum length without truncating words and preserves special characters.
|
||||
func splitText(text string, maxLen int) []string {
|
||||
if len(text) <= maxLen {
|
||||
return []string{text}
|
||||
}
|
||||
|
||||
var chunks []string
|
||||
words := strings.Fields(text) // Splitting the text into words
|
||||
lines := strings.Split(text, "\n") // Split text by newlines first
|
||||
whitespaceRegex := regexp.MustCompile(`\s+`)
|
||||
|
||||
for _, line := range lines {
|
||||
var chunk string
|
||||
words := whitespaceRegex.Split(line, -1) // Splitting the line into words while preserving whitespace
|
||||
|
||||
for _, word := range words {
|
||||
if len(chunk)+len(word)+1 > maxLen { // +1 for space
|
||||
@@ -336,6 +341,7 @@ func splitText(text string, maxLen int) []string {
|
||||
if chunk != "" {
|
||||
chunks = append(chunks, chunk)
|
||||
}
|
||||
}
|
||||
|
||||
return chunks
|
||||
}
|
||||
@@ -377,10 +383,10 @@ func replyWithPostMessage(finalResponse string, api *slack.Client, ev *slackeven
|
||||
}
|
||||
|
||||
func replyToUpdateMessage(finalResponse string, api *slack.Client, ev *slackevents.AppMentionEvent, msgTs string, ts string, res *types.JobResult) {
|
||||
if len(finalResponse) > 4000 {
|
||||
if len(finalResponse) > 3000 {
|
||||
// split response in multiple messages, and update the first
|
||||
|
||||
messages := splitText(finalResponse, 4000)
|
||||
messages := splitText(finalResponse, 3000)
|
||||
|
||||
_, _, _, err := api.UpdateMessage(
|
||||
ev.Channel,
|
||||
|
||||
Reference in New Issue
Block a user