-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_text.go
69 lines (53 loc) · 1.39 KB
/
component_text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package got
import (
"strings"
"github.com/ramin0/messenger"
)
const (
// ComponentTextTextMaxLength const
ComponentTextTextMaxLength = 640
)
// ComponentText type
type ComponentText struct {
ComponentBase
Text string
Buttons []*SubComponentButton
}
var x = 0
// Execute func
func (c *ComponentText) Execute(ctx *BotContext) error {
text := ctx.Interpolate(c.Text)
chars := strings.Split(text, "")
textParts := []string{""}
for len(chars) > 0 {
lastTextPart := textParts[len(textParts)-1]
if len(lastTextPart)+len(chars[0]) <= ComponentTextTextMaxLength {
lastTextPart += chars[0]
chars = chars[1:]
if len(lastTextPart) == ComponentTextTextMaxLength && len(chars) > 0 {
lastChars := strings.Split(lastTextPart[len(lastTextPart)-3:], "")
lastTextPart = lastTextPart[:len(lastTextPart)-3] + "..."
chars = append(append(strings.Split("...", ""), lastChars...), chars...)
}
textParts[len(textParts)-1] = lastTextPart
} else {
textParts = append(textParts, "")
}
}
for i, textPart := range textParts {
if i == len(textParts)-1 && len(c.Buttons) > 0 {
buttons := []*messenger.ElmButton{}
for _, b := range c.Buttons {
buttons = append(buttons, b.Execute(ctx))
}
return c.client().SendButtons(ctx.userID,
textPart,
buttons)
}
if err := c.client().SendText(ctx.userID,
textPart); err != nil {
return err
}
}
return nil
}