/
sender.go
158 lines (135 loc) · 4.05 KB
/
sender.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
151
152
153
154
155
156
157
158
package botmeans
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
// "log"
)
//OutMsgFactoryInterface allows users to create or edit messages inside ActionHandlers
type OutMsgFactoryInterface interface {
Create(templateName string, Data interface{}) error
CreateWithCustomReplyKeyboard(templateName string, Data interface{}, kdb [][]MessageButton) error
Edit(msg BotMessageInterface, templateName string, Data interface{}) error
Notify(BotMessageInterface, string, bool)
SimpleText(text string) error
}
//SenderInterface is the abstraction for the Sender
type SenderInterface interface {
OutMsgFactoryInterface
// Sendable
}
//Sendable defines something that can be Send
type Sendable interface {
Send() bool
}
type Localizer interface {
Locale() string
}
type senderFactory func(s senderSession) SenderInterface
type senderSession interface {
ChatIdentifier
Localizer
}
//Sender implements SenderInterface
type Sender struct {
msgFactory func() BotMessageInterface
session senderSession
bot *tgbotapi.BotAPI
templateDir string
}
//Create creates new telegram message from template
func (f *Sender) Create(templateName string, Data interface{}) error {
botMsg := f.msgFactory()
botMsg.SetData(Data)
params, err := renderFromTemplate(f.templateDir, templateName, f.session.Locale(), Data)
if err != nil {
return err
}
toSent := tgbotapi.NewMessage(f.session.ChatId(), params.text)
toSent.ParseMode = params.ParseMode
if params.replyKbdMarkup != nil {
toSent.ReplyMarkup = *params.replyKbdMarkup
}
if params.replyKbdHide != nil {
toSent.ReplyMarkup = params.replyKbdHide
}
if params.inlineKbdMarkup != nil {
toSent.ReplyMarkup = *params.inlineKbdMarkup
}
if f.bot != nil {
if sentMsg, err := f.bot.Send(toSent); err == nil {
botMsg.SetID(int64(sentMsg.MessageID))
} else {
return err
}
}
botMsg.Save()
return nil
}
//Create creates new telegram message from template using custom reply keyboard
func (f *Sender) CreateWithCustomReplyKeyboard(templateName string, Data interface{}, kbd [][]MessageButton) error {
botMsg := f.msgFactory()
botMsg.SetData(Data)
params, err := renderFromTemplate(f.templateDir, templateName, f.session.Locale(), Data)
if err != nil {
return err
}
toSent := tgbotapi.NewMessage(f.session.ChatId(), params.text)
toSent.ParseMode = params.ParseMode
if params.replyKbdMarkup != nil {
params.replyKbdMarkup.Keyboard = append(params.replyKbdMarkup.Keyboard, createReplyKeyboard(kbd).Keyboard...)
toSent.ReplyMarkup = *params.replyKbdMarkup
} else {
toSent.ReplyMarkup = createReplyKeyboard(kbd)
}
if params.inlineKbdMarkup != nil {
toSent.ReplyMarkup = *params.inlineKbdMarkup
}
if f.bot != nil {
if sentMsg, err := f.bot.Send(toSent); err == nil {
botMsg.SetID(int64(sentMsg.MessageID))
} else {
return nil
}
}
botMsg.Save()
return nil
}
//SimpleText creates new telegram message with given text
func (f *Sender) SimpleText(text string) error {
botMsg := f.msgFactory()
toSent := tgbotapi.NewMessage(f.session.ChatId(), text)
if f.bot != nil {
if sentMsg, err := f.bot.Send(toSent); err == nil {
botMsg.SetID(int64(sentMsg.MessageID))
} else {
return err
}
}
botMsg.Save()
return nil
}
//Notify creates notification for callback queries
func (f *Sender) Notify(msg BotMessageInterface, callbackNotification string, showAlert bool) {
f.bot.AnswerCallbackQuery(tgbotapi.CallbackConfig{
CallbackQueryID: msg.CallbackID(),
ShowAlert: showAlert,
Text: callbackNotification,
})
}
//Edit allows to edit existing messages
func (f *Sender) Edit(msg BotMessageInterface, templateName string, Data interface{}) error {
msg.SetData(Data)
params, err := renderFromTemplate(f.templateDir, templateName, f.session.Locale(), Data)
if err != nil {
return err
}
editConfig := tgbotapi.NewEditMessageText(f.session.ChatId(), int(msg.Id()), params.text)
if params.inlineKbdMarkup != nil {
editConfig.ReplyMarkup = params.inlineKbdMarkup
}
editConfig.ParseMode = params.ParseMode
if f.bot != nil {
f.bot.Send(editConfig)
}
msg.Save()
return nil
}