-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplate_utils.go
More file actions
152 lines (135 loc) · 3.69 KB
/
template_utils.go
File metadata and controls
152 lines (135 loc) · 3.69 KB
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
151
152
package botmeans
import (
"bytes"
"encoding/json"
"fmt"
"github.com/go-telegram-bot-api/telegram-bot-api"
"io/ioutil"
"strings"
"text/template"
"unicode"
)
func getTemplater() *template.Template {
return template.New("msg").Funcs(
template.FuncMap{
"underscore": func(s string) string {
return strings.Map(
func(r rune) rune {
if unicode.IsLetter(r) || unicode.IsNumber(r) {
return r
}
return '_'
},
strings.TrimSpace(s),
)
},
},
)
}
//MessageTemplate defines the structure of the message template
type MessageTemplate struct {
ParseMode string
Keyboard map[string][][]MessageButton
Template map[string]string
ReplyKeyboard map[string][][]MessageButton
}
//MessageButton represents a button in Telegram UI
type MessageButton struct {
Text string
Command string
Args string
}
type tgMsgParams struct {
ParseMode string
text string
inlineKbdMarkup *tgbotapi.InlineKeyboardMarkup
replyKbdMarkup *tgbotapi.ReplyKeyboardMarkup
replyKbdHide *tgbotapi.ReplyKeyboardHide
}
func renderFromTemplate(
templateDir string,
templateName string,
locale string,
Data interface{},
) (tgMsgParams, error) {
ret := tgMsgParams{}
templ := getTemplater()
msgTemplate, err := readMsgTemplate(templateDir + "/" + templateName + ".json")
if err != nil {
return ret, err
}
ret.ParseMode = msgTemplate.ParseMode
if _, ok := msgTemplate.Template[locale]; !ok {
locale = ""
}
ret.text, err = renderText(msgTemplate.Template[locale], Data, templ)
ret.inlineKbdMarkup = createInlineKeyboard(msgTemplate.Keyboard[locale])
ret.replyKbdMarkup = createReplyKeyboard(msgTemplate.ReplyKeyboard[locale])
if len(msgTemplate.ReplyKeyboard[locale]) == 0 {
h := tgbotapi.NewHideKeyboard(true)
ret.replyKbdHide = &h
}
return ret, err
}
func readMsgTemplate(path string) (ret MessageTemplate, err error) {
c := []byte{}
c, err = ioutil.ReadFile(path)
if err != nil {
err = fmt.Errorf("Template not found: %v", path)
return
}
c = []byte(strings.Replace(string(c), "\n", "", -1))
err = json.Unmarshal(c, &ret)
return
}
func createInlineKeyboard(buttons [][]MessageButton) *tgbotapi.InlineKeyboardMarkup {
var inlineKbdMarkup tgbotapi.InlineKeyboardMarkup
if len(buttons) > 0 {
kbdRows := [][]tgbotapi.InlineKeyboardButton{}
for _, row := range buttons {
r := []tgbotapi.InlineKeyboardButton{}
for _, btnData := range row {
r = append(r, tgbotapi.NewInlineKeyboardButtonData(btnData.Text, btnData.Command+" "+btnData.Args))
}
kbdRows = append(kbdRows, r)
}
inlineKbdMarkup = tgbotapi.NewInlineKeyboardMarkup(kbdRows...)
} else {
return nil
inlineKbdMarkup.InlineKeyboard = make([][]tgbotapi.InlineKeyboardButton, 0)
}
return &inlineKbdMarkup
}
func createReplyKeyboard(buttons [][]MessageButton) *tgbotapi.ReplyKeyboardMarkup {
var replyKbdMarkup tgbotapi.ReplyKeyboardMarkup
if len(buttons) > 0 {
kbdRows := [][]tgbotapi.KeyboardButton{}
for _, row := range buttons {
r := []tgbotapi.KeyboardButton{}
for _, btnData := range row {
r = append(r, tgbotapi.NewKeyboardButton(btnData.Text))
}
kbdRows = append(kbdRows, r)
}
replyKbdMarkup = tgbotapi.NewReplyKeyboard(kbdRows...)
replyKbdMarkup.OneTimeKeyboard = false
replyKbdMarkup.ResizeKeyboard = true
} else {
replyKbdMarkup.Keyboard = make([][]tgbotapi.KeyboardButton, 0)
}
return &replyKbdMarkup
}
func renderText(templateText string, data interface{}, templ *template.Template) (text string, err error) {
if t, e := templ.Parse(templateText); e != nil {
err = e
} else {
bfr := &bytes.Buffer{}
if e := t.Execute(bfr, data); e != nil {
err = e
text = templateText
} else {
text = bfr.String()
}
}
return
}