/
EasyApi.go
159 lines (134 loc) · 4.43 KB
/
EasyApi.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
159
package easy
import (
"errors"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"strings"
)
type lastMessageStorage interface {
GetByUser(user *tgbotapi.User) string
GetByUpdate(update *tgbotapi.Update) string
Store(user *tgbotapi.User, text string)
StoreByUpdate(user *tgbotapi.Update, text string)
}
type EasyApi struct {
*EasyCallback
*EasyDownloadFile
*EasyApiKeyboard
api *tgbotapi.BotAPI
lastMessageStorage lastMessageStorage
NoReplace bool
MarkdownMode string
}
func NewEasyApi(api *tgbotapi.BotAPI, lastMessageStorage lastMessageStorage) *EasyApi {
return &EasyApi{
EasyCallback: &EasyCallback{api},
EasyDownloadFile: &EasyDownloadFile{api},
EasyApiKeyboard: &EasyApiKeyboard{api, lastMessageStorage},
api: api,
lastMessageStorage: lastMessageStorage,
MarkdownMode: tgbotapi.ModeMarkdownV2,
}
}
func (a *EasyApi) Api() *tgbotapi.BotAPI {
return a.api
}
func (a *EasyApi) SendEasyMessage(easyMessage *Message) (*tgbotapi.Message, error) {
text := easyMessage.text
if easyMessage.oldReplace {
//@todo не должно быть таких замен в этом коде
text = strings.Replace(text, "(", "\\(", -1)
text = strings.Replace(text, ".", "\\.", -1)
text = strings.Replace(text, ")", "\\)", -1)
text = strings.Replace(text, "_", "\\_", -1)
text = strings.Replace(text, "-", "\\-", -1)
}
message, err := a.NewMessage(*easyMessage.update, text)
if err != nil {
return nil, err
}
if len(easyMessage.ParseMode) > 0 {
message.ParseMode = easyMessage.ParseMode
}
messageResult, err := a.SendChattableCommand(*easyMessage.update, message, easyMessage.command)
return &messageResult, err
}
func (a *EasyApi) Send(chattable tgbotapi.Chattable) (tgbotapi.Message, error) {
return a.api.Send(chattable)
}
func (a *EasyApi) SendMessage(update tgbotapi.Update, text string) (tgbotapi.Message, error) {
return a.SendMessageCommand(update, text, "")
}
func (a *EasyApi) SendMessageCommand(update tgbotapi.Update, text string, command string) (tgbotapi.Message, error) {
easyMessage :=
NewMessage(&update, text).
WithCommand(command).
WithOldReplace().
WithParseMk2()
resultMessage, err := a.SendEasyMessage(easyMessage)
if resultMessage == nil {
resultMessage = &tgbotapi.Message{}
}
return *resultMessage, err
////@todo не должно быть таких замен в этом коде
//text = strings.Replace(text, "(", "\\(", -1)
//text = strings.Replace(text, ".", "\\.", -1)
//text = strings.Replace(text, ")", "\\)", -1)
//
//newMessage, err := a.NewMessageMk2(update, text)
//if err != nil {
// return tgbotapi.Message{}, err
//}
//
//return a.SendChattableCommand(update, newMessage, command)
}
func (a *EasyApi) NewMessageMk2(update tgbotapi.Update, text string) (*tgbotapi.MessageConfig, error) {
message, err := a.NewMessage(update, text)
if err == nil {
message.ParseMode = tgbotapi.ModeMarkdownV2
}
return message, err
}
func (a *EasyApi) NewMessage(update tgbotapi.Update, text string) (*tgbotapi.MessageConfig, error) {
message := a.resolverMessage(update)
if message == nil {
return nil, errors.New("no message in update")
}
if message.Chat == nil {
return nil, errors.New("no chat in message")
}
newMessage := tgbotapi.NewMessage(message.Chat.ID, text)
return &newMessage, nil
}
func (a *EasyApi) SendChattable(update tgbotapi.Update, chattable tgbotapi.Chattable) (tgbotapi.Message, error) {
return a.SendChattableCommand(update, chattable, "")
}
func (a *EasyApi) SendChattableCommand(
update tgbotapi.Update,
chattable tgbotapi.Chattable,
command string,
) (tgbotapi.Message, error) {
sentMessage, err := a.api.Send(chattable)
if err == nil {
isPrivate := sentMessage.Chat.IsPrivate()
if fromUser := a.resolveFromUser(update); fromUser != nil && isPrivate {
a.lastMessageStorage.Store(fromUser, command)
}
}
return sentMessage, err
}
func (a *EasyApi) resolverMessage(update tgbotapi.Update) *tgbotapi.Message {
if update.CallbackQuery != nil && update.CallbackQuery.Message != nil {
return update.CallbackQuery.Message
} else if update.Message != nil {
return update.Message
}
return nil
}
func (a *EasyApi) resolveFromUser(update tgbotapi.Update) *tgbotapi.User {
if update.CallbackQuery != nil && update.CallbackQuery.From != nil {
return update.CallbackQuery.From
} else if update.Message != nil && update.Message.From != nil {
return update.Message.From
}
return nil
}