-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
150 lines (135 loc) · 3.95 KB
/
help.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package help
import (
"fmt"
"math"
"regexp"
"strings"
rc "github.com/grokify/go-ringcentral-client/office/v1/client"
"github.com/grokify/mogo/html/htmlutil"
"github.com/grokify/mogo/net/http/httputilmore"
"github.com/grokify/mogo/type/stringsutil"
"github.com/grokify/groupbot"
)
func NewIntent() groupbot.Intent {
return groupbot.Intent{
Type: groupbot.MatchStringLowerCase,
Strings: []string{"helpv1"},
HandleIntent: handleIntent,
}
}
func handleIntent(bot *groupbot.Groupbot, glipPostEventInfo *groupbot.GlipPostEventInfo) (*httputilmore.ResponseInfo, error) {
glipPost := buildPost(bot)
return bot.SendGlipPost(glipPostEventInfo, glipPost)
}
func buildPost(bot *groupbot.Groupbot) rc.GlipCreatePost {
reqBody := rc.GlipCreatePost{}
colNames := []string{}
haveEnums := false
exampleKey := ""
exampleVal := ""
for i, col := range bot.SheetsMap.Columns {
if i < 2 {
continue
}
colNames = append(colNames, col.Name)
attachment := rc.GlipMessageAttachmentInfoRequest{
Type: "Card",
Fields: []rc.GlipMessageAttachmentFieldsInfo{},
}
attachment.Fields = append(attachment.Fields,
rc.GlipMessageAttachmentFieldsInfo{
Title: "Field",
Value: col.Name,
Style: "Short",
})
if len(exampleKey) == 0 {
exampleKey = col.Name
}
if len(col.Enums) > 0 {
haveEnums = true
options := []string{}
for _, enum := range col.Enums {
parts := []string{}
if len(enum.Canonical) > 0 {
parts = append(parts, enum.Canonical)
if len(exampleVal) == 0 {
exampleVal = enum.Canonical
}
}
if len(enum.Aliases) > 0 {
aliasesStr := fmt.Sprintf("(%v)", strings.Join(enum.Aliases, ", "))
parts = append(parts, aliasesStr)
}
if len(parts) > 0 {
options = append(options, strings.Join(parts, " "))
}
}
if len(options) > 0 {
optionsStr := ""
if len(options) == 1 {
optionsStr = options[1]
} else {
optionsStr = "* " + strings.Join(options, "\n* ")
}
attachment.Fields = append(attachment.Fields,
rc.GlipMessageAttachmentFieldsInfo{
Title: "Values (with aliases)",
Value: optionsStr,
Style: "Short",
})
}
}
for _, infoURL := range col.InfoURLs {
attachment.Fields = append(attachment.Fields,
rc.GlipMessageAttachmentFieldsInfo{
Title: infoURL.Text,
Value: URLToMarkdownLinkHostname(infoURL.URL),
Style: "Short",
})
}
if len(attachment.Fields) > 0 {
mod := math.Mod(float64(len(reqBody.Attachments)), 2)
if mod == 0 {
attachment.Color = htmlutil.RingCentralOrangeHex
} else {
attachment.Color = htmlutil.RingCentralBlueHex
}
reqBody.Attachments = append(reqBody.Attachments, attachment)
}
}
if len(reqBody.Attachments) > 0 {
enumsText := ""
if haveEnums {
enumsText = " See more on values you can use below below."
}
exampleText := ""
if len(exampleKey) > 0 && len(exampleVal) > 0 {
exampleText = ", for example: " + bot.AppConfig.Quote(exampleKey+" "+exampleVal)
}
reqBody.Text = fmt.Sprintf("Hi there, I'm here to help you share some data. Here are some things you can do use me:\n\n* You can set the following fields: %s\n* To set a field, say %s.%s\n* Additional commands include %s, %s, %s, and %s.\n* If there are more than 2 people in our conversation, you will need to @ mention me.",
strings.Join(
stringsutil.SliceCondenseAndQuoteSpace(
colNames,
bot.AppConfig.GroupbotCharQuoteLeft,
bot.AppConfig.GroupbotCharQuoteRight), ", "),
bot.AppConfig.Quote("<field> <value>")+exampleText,
enumsText,
bot.AppConfig.Quote("me"),
bot.AppConfig.Quote("list"),
bot.AppConfig.Quote("stats"),
bot.AppConfig.Quote("about"))
}
return reqBody
}
func URLToMarkdownLinkHostname(url string) string {
rx := regexp.MustCompile(`(?i)^https?://([^/]+)(/[^/])`)
m := rx.FindStringSubmatch(url)
if len(m) > 1 {
suffix := ""
if len(m) > 2 {
suffix = "..."
}
return fmt.Sprintf("[%v%v](%v)", m[1], suffix, url)
}
return url
}