2020-09-05 22:29:24 +00:00
|
|
|
package layout
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-09-15 16:24:29 +00:00
|
|
|
"strings"
|
2020-09-05 22:29:24 +00:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/goccy/go-yaml"
|
|
|
|
tele "gopkg.in/tucnak/telebot.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Settings struct {
|
|
|
|
URL string
|
|
|
|
Token string
|
|
|
|
Updates int
|
|
|
|
|
2020-09-15 13:39:46 +00:00
|
|
|
LocalesDir string `json:"locales_dir"`
|
|
|
|
TokenEnv string `json:"token_env"`
|
|
|
|
ParseMode string `json:"parse_mode"`
|
2020-09-05 22:29:24 +00:00
|
|
|
|
|
|
|
Webhook *tele.Webhook `json:"webhook"`
|
|
|
|
LongPoller *tele.LongPoller `json:"long_poller"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (lt *Layout) UnmarshalYAML(data []byte) error {
|
|
|
|
var aux struct {
|
|
|
|
Settings *Settings
|
|
|
|
Config map[string]interface{}
|
|
|
|
Markups yaml.MapSlice
|
|
|
|
Locales map[string]map[string]string
|
|
|
|
}
|
|
|
|
if err := yaml.Unmarshal(data, &aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-25 20:17:49 +00:00
|
|
|
lt.config = aux.Config
|
2020-09-05 22:29:24 +00:00
|
|
|
|
|
|
|
if pref := aux.Settings; pref != nil {
|
|
|
|
lt.pref = &tele.Settings{
|
|
|
|
URL: pref.URL,
|
|
|
|
Token: pref.Token,
|
|
|
|
Updates: pref.Updates,
|
|
|
|
ParseMode: pref.ParseMode,
|
|
|
|
}
|
|
|
|
|
|
|
|
if pref.TokenEnv != "" {
|
|
|
|
lt.pref.Token = os.Getenv(pref.TokenEnv)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pref.Webhook != nil {
|
|
|
|
lt.pref.Poller = pref.Webhook
|
|
|
|
} else if pref.LongPoller != nil {
|
|
|
|
lt.pref.Poller = pref.LongPoller
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 20:17:49 +00:00
|
|
|
lt.markups = make(map[string]Markup, len(aux.Markups))
|
2020-09-05 22:29:24 +00:00
|
|
|
for _, item := range aux.Markups {
|
|
|
|
k, v := item.Key.(string), item.Value
|
|
|
|
|
|
|
|
data, err := yaml.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. Normal markup.
|
|
|
|
|
|
|
|
var markup struct {
|
|
|
|
Markup `yaml:",inline"`
|
|
|
|
Resize *bool `json:"resize_keyboard"`
|
|
|
|
}
|
|
|
|
if yaml.Unmarshal(data, &markup) == nil {
|
|
|
|
data, err := yaml.Marshal(markup.ReplyKeyboard)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-25 20:17:49 +00:00
|
|
|
tmpl, err := template.New(k).Funcs(lt.funcs).Parse(string(data))
|
2020-09-05 22:29:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-25 20:17:49 +00:00
|
|
|
markup.Markup.keyboard = tmpl
|
2020-09-05 22:29:24 +00:00
|
|
|
markup.ResizeReplyKeyboard = markup.Resize == nil || *markup.Resize
|
|
|
|
|
2020-09-25 20:17:49 +00:00
|
|
|
lt.markups[k] = markup.Markup
|
2020-09-05 22:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Shortened reply markup.
|
|
|
|
|
|
|
|
var embeddedMarkup [][]string
|
|
|
|
if yaml.Unmarshal(data, &embeddedMarkup) == nil {
|
|
|
|
kb := make([][]tele.ReplyButton, len(embeddedMarkup))
|
|
|
|
for i, btns := range embeddedMarkup {
|
|
|
|
row := make([]tele.ReplyButton, len(btns))
|
|
|
|
for j, btn := range btns {
|
|
|
|
row[j] = tele.ReplyButton{Text: btn}
|
|
|
|
}
|
|
|
|
kb[i] = row
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := yaml.Marshal(kb)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-25 20:17:49 +00:00
|
|
|
tmpl, err := template.New(k).Funcs(lt.funcs).Parse(string(data))
|
2020-09-05 22:29:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-25 20:17:49 +00:00
|
|
|
markup := Markup{keyboard: tmpl}
|
2020-09-05 22:29:24 +00:00
|
|
|
markup.ResizeReplyKeyboard = true
|
2020-09-25 20:17:49 +00:00
|
|
|
lt.markups[k] = markup
|
2020-09-05 22:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Shortened inline markup.
|
|
|
|
|
|
|
|
if yaml.Unmarshal(data, &[][]tele.InlineButton{}) == nil {
|
2020-09-25 20:17:49 +00:00
|
|
|
tmpl, err := template.New(k).Funcs(lt.funcs).Parse(string(data))
|
2020-09-05 22:29:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-25 20:17:49 +00:00
|
|
|
lt.markups[k] = Markup{
|
|
|
|
keyboard: tmpl,
|
2020-09-05 22:29:24 +00:00
|
|
|
inline: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if aux.Locales == nil {
|
2020-09-15 10:37:30 +00:00
|
|
|
if aux.Settings.LocalesDir == "" {
|
|
|
|
aux.Settings.LocalesDir = "locales"
|
|
|
|
}
|
2020-09-05 22:29:24 +00:00
|
|
|
return lt.parseLocales(aux.Settings.LocalesDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lt *Layout) parseLocales(dir string) error {
|
2020-09-25 20:17:49 +00:00
|
|
|
lt.locales = make(map[string]*template.Template)
|
2020-09-15 10:31:54 +00:00
|
|
|
|
2020-09-05 22:29:24 +00:00
|
|
|
return filepath.Walk(dir, func(path string, fi os.FileInfo, _ error) error {
|
|
|
|
if fi == nil || fi.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var texts map[string]string
|
|
|
|
if err := yaml.Unmarshal(data, &texts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-15 16:24:29 +00:00
|
|
|
name := fi.Name()
|
|
|
|
name = strings.TrimSuffix(name, filepath.Ext(name))
|
2020-09-05 22:29:24 +00:00
|
|
|
|
2020-09-15 16:24:29 +00:00
|
|
|
tmpl := template.New(name)
|
2020-09-15 16:42:15 +00:00
|
|
|
for key, text := range texts {
|
|
|
|
text = strings.Trim(text, "\r\n")
|
2020-09-25 20:17:49 +00:00
|
|
|
tmpl, err = tmpl.New(key).Funcs(lt.funcs).Parse(text)
|
2020-09-05 22:29:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 20:17:49 +00:00
|
|
|
lt.locales[name] = tmpl
|
2020-09-05 22:29:24 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|