forked from jqs7/Jqs7Bot
/
helper.go
130 lines (114 loc) · 2.46 KB
/
helper.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
package main
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/Syfaro/telegram-bot-api"
"github.com/fatih/set"
"github.com/kylelemons/go-gypsy/yaml"
"github.com/pyk/byten"
)
func YamlList2String(config *yaml.File, text string) string {
resultGroup := YamlList2Slice(config, text)
result := strings.Join(resultGroup, "\n")
result = strings.Replace(result, "\\n", "", -1)
return result
}
func YamlList2Slice(config *yaml.File, text string) []string {
count, err := config.Count(text)
if err != nil {
loge.Error(err)
return nil
}
var result []string
for i := 0; i < count; i++ {
v, err := config.Get(text + "[" + strconv.Itoa(i) + "]")
if err != nil {
loge.Error(err)
return nil
}
result = append(result, v)
}
return result
}
type Question struct {
Q string
A set.Interface
}
func GetQuestions(config *yaml.File, text string) []Question {
var result []Question
questions := YamlList2Slice(config, text)
for _, v := range questions {
qs := strings.Split(v, "|")
question := qs[0]
answers := strings.Split(qs[1], ";")
s := set.New(set.ThreadSafe)
for _, v := range answers {
s.Add(v)
}
result = append(result, Question{question, s})
}
return result
}
func To2dSlice(in []string, x, y int) [][]string {
out := [][]string{}
var begin, end int
for i := 0; i < y; i++ {
end += x
if end >= len(in) {
out = append(out, in[begin:])
break
}
out = append(out, in[begin:end])
begin = end
}
return out
}
func GetDate(day bool, offset int) string {
now := time.Now()
year := strconv.Itoa(now.Year())
var month string
if !day {
month = (now.Month() + time.Month(offset)).String()
return year + month
}
month = now.Month().String()
dayNum := strconv.Itoa(now.Day() + offset)
return year + month + dayNum
}
func FromUserName(user tgbotapi.User) string {
userName := user.UserName
if userName != "" {
return "@" + userName
}
name := user.FirstName
lastName := user.LastName
if lastName != "" {
name += " " + lastName
}
return name
}
func humanByte(in ...interface{}) (out []interface{}) {
for _, v := range in {
switch v.(type) {
case int, uint64:
s := fmt.Sprintf("%d", v)
i, _ := strconv.ParseInt(s, 10, 64)
out = append(out, byten.Size(i))
case float64:
s := fmt.Sprintf("%.2f", v)
out = append(out, s)
default:
out = append(out, v)
}
}
return out
}
func markdownEscape(s string) string {
return strings.NewReplacer(
"_", "\\_",
"*", "\\*",
"[", "\\[",
).Replace(s)
}